Add user signup controller

This commit is contained in:
Lucas Schumacher 2024-08-03 07:34:27 -04:00
parent 82b954af2e
commit 1156cabe05
3 changed files with 44 additions and 1 deletions

View File

@ -7,6 +7,10 @@ import (
"strings"
)
type Template interface {
Execute(w http.ResponseWriter, data interface{})
}
func StaticTemplate(templatePath ...string) http.HandlerFunc {
tpl := views.Must(views.FromFS(templates.FS, templatePath...))

37
controllers/users.go Normal file
View File

@ -0,0 +1,37 @@
package controllers
import (
"fmt"
"net/http"
"strings"
"git.kealoha.me/lks/lenslocked/templates"
"git.kealoha.me/lks/lenslocked/views"
)
type Users struct {
Templates struct {
New Template
}
}
func (u Users) New(w http.ResponseWriter, r *http.Request) {
u.Templates.New.Execute(w, nil)
}
func (u Users) Create(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "TODO! ", r.FormValue("email"))
}
func FromStaticTemplate(templatePath ...string) Users {
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
var testWriter strings.Builder
err := tpl.ExecuteWriter(&testWriter, nil)
if err != nil {
panic(err)
}
u := Users{}
u.Templates.New = tpl
return u
}

View File

@ -16,12 +16,14 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
var usersCtrlr ctrlrs.Users = ctrlrs.FromStaticTemplate("signup.gohtml", "tailwind.gohtml")
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", ctrlrs.StaticTemplate("home.gohtml", "tailwind.gohtml"))
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml", "tailwind.gohtml"))
r.Get("/faq", ctrlrs.FAQ("faq.gohtml", "tailwind.gohtml"))
r.Get("/signup", ctrlrs.StaticTemplate("signup.gohtml", "tailwind.gohtml"))
r.Get("/signup", usersCtrlr.New)
r.Post("/signup", usersCtrlr.Create)
r.NotFound(notFoundHandler)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)