Add user signup controller
This commit is contained in:
parent
82b954af2e
commit
1156cabe05
@ -7,6 +7,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Template interface {
|
||||||
|
Execute(w http.ResponseWriter, data interface{})
|
||||||
|
}
|
||||||
|
|
||||||
func StaticTemplate(templatePath ...string) http.HandlerFunc {
|
func StaticTemplate(templatePath ...string) http.HandlerFunc {
|
||||||
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
||||||
|
|
||||||
|
|||||||
37
controllers/users.go
Normal file
37
controllers/users.go
Normal 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
|
||||||
|
}
|
||||||
4
main.go
4
main.go
@ -16,12 +16,14 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var usersCtrlr ctrlrs.Users = ctrlrs.FromStaticTemplate("signup.gohtml", "tailwind.gohtml")
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Get("/", ctrlrs.StaticTemplate("home.gohtml", "tailwind.gohtml"))
|
r.Get("/", ctrlrs.StaticTemplate("home.gohtml", "tailwind.gohtml"))
|
||||||
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml", "tailwind.gohtml"))
|
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml", "tailwind.gohtml"))
|
||||||
r.Get("/faq", ctrlrs.FAQ("faq.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)
|
r.NotFound(notFoundHandler)
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user