From 9360aeea76e515adbfc1310fab2e96506a690df3 Mon Sep 17 00:00:00 2001 From: T T Date: Tue, 6 Nov 2018 18:23:29 +0100 Subject: [PATCH] Nice html --- go.mod | 2 +- main.go | 23 +++++++++++++--------- templates.go | 55 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 156e9dd..2c1782c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/you/hello +module github.com/tilde-cat/register require github.com/satori/go.uuid v1.2.0 diff --git a/main.go b/main.go index d1985c4..1af8452 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "io/ioutil" "log" "net/http" @@ -18,7 +17,13 @@ const ( ErrorUrl = "/error" ) -var statusRE = regexp.MustCompile(RequestStatusUrlPrefix + `(.+)$`) +var config = struct { + Title string +}{ + Title: "~🐱 Sign up!", +} + +var statusRE = regexp.MustCompile("^" + RequestStatusUrlPrefix + `(.+)$`) type Id uuid.UUID @@ -91,10 +96,11 @@ func (s *Server) RequestPage(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - fmt.Fprintf(w, "Status: %v", req.Status) -} - -func (s *Server) IncorrectRequest(w http.ResponseWriter, r *http.Request) { + config := map[string]interface{}{ + "Global": config, + "Status": req.Status, + } + statusTemplate.ExecuteTemplate(w, "status", config) } func (s *Server) FormPostHandler(w http.ResponseWriter, r *http.Request) { @@ -116,15 +122,14 @@ func (s *Server) FormPostHandler(w http.ResponseWriter, r *http.Request) { } func (s *Server) FormPage(w http.ResponseWriter, r *http.Request) { - formTemplate.Execute(w, nil) + formTemplate.Execute(w, config) } func main() { var io FsIo server := Server{Io: &io} http.HandleFunc(RequestStatusUrlPrefix, server.RequestPage) - http.HandleFunc(ErrorUrl, server.IncorrectRequest) http.HandleFunc(FormPostUrl, server.FormPostHandler) http.HandleFunc(FormUrl, server.FormPage) - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe("localhost:5678", nil)) } diff --git a/templates.go b/templates.go index 77b973f..9707983 100644 --- a/templates.go +++ b/templates.go @@ -2,21 +2,44 @@ package main import "html/template" -var formTemplate = template.Must(template.New("form").Parse(` - +var header = template.Must(template.New("header").Parse(` + +{{.Title}} + + -

~🐱 signup form

-
-Username: -
-Email: -
-Why would you want an account here? -
-SSH key: -
- -
- - +`)) + +var footer = template.Must(header.New("footer").Parse(` +`)) + +var statusTemplate = template.Must(footer.New("status").Parse(` +{{ template "header" .Global }} +Status: {{ .Status }} +{{ template "footer" .Global }}`)) + +var formTemplate = template.Must(header.New("form").Parse(`{{ template "header" . }} +

~🐱 Sign up form

+
+ + + + + + + + + + + + + + + + + + +
Username:
Email:
Why would you want an account here?
SSH key:
+
+{{ template "footer" . }} `))