diff --git a/controllers/static.go b/controllers/static.go index 8d6f009..7a591a5 100644 --- a/controllers/static.go +++ b/controllers/static.go @@ -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...)) diff --git a/controllers/users.go b/controllers/users.go new file mode 100644 index 0000000..f22c149 --- /dev/null +++ b/controllers/users.go @@ -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 +} diff --git a/main.go b/main.go index 8a51cfc..347616c 100644 --- a/main.go +++ b/main.go @@ -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)