Nice html

This commit is contained in:
T T 2018-11-06 18:23:29 +01:00
parent 62bf727cf1
commit 9360aeea76
3 changed files with 54 additions and 26 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/you/hello module github.com/tilde-cat/register
require github.com/satori/go.uuid v1.2.0 require github.com/satori/go.uuid v1.2.0

23
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -18,7 +17,13 @@ const (
ErrorUrl = "/error" ErrorUrl = "/error"
) )
var statusRE = regexp.MustCompile(RequestStatusUrlPrefix + `(.+)$`) var config = struct {
Title string
}{
Title: "~🐱 Sign up!",
}
var statusRE = regexp.MustCompile("^" + RequestStatusUrlPrefix + `(.+)$`)
type Id uuid.UUID 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) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
fmt.Fprintf(w, "Status: %v", req.Status) config := map[string]interface{}{
} "Global": config,
"Status": req.Status,
func (s *Server) IncorrectRequest(w http.ResponseWriter, r *http.Request) { }
statusTemplate.ExecuteTemplate(w, "status", config)
} }
func (s *Server) FormPostHandler(w http.ResponseWriter, r *http.Request) { 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) { func (s *Server) FormPage(w http.ResponseWriter, r *http.Request) {
formTemplate.Execute(w, nil) formTemplate.Execute(w, config)
} }
func main() { func main() {
var io FsIo var io FsIo
server := Server{Io: &io} server := Server{Io: &io}
http.HandleFunc(RequestStatusUrlPrefix, server.RequestPage) http.HandleFunc(RequestStatusUrlPrefix, server.RequestPage)
http.HandleFunc(ErrorUrl, server.IncorrectRequest)
http.HandleFunc(FormPostUrl, server.FormPostHandler) http.HandleFunc(FormPostUrl, server.FormPostHandler)
http.HandleFunc(FormUrl, server.FormPage) http.HandleFunc(FormUrl, server.FormPage)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe("localhost:5678", nil))
} }

View File

@ -2,21 +2,44 @@ package main
import "html/template" import "html/template"
var formTemplate = template.Must(template.New("form").Parse(` var header = template.Must(template.New("header").Parse(`<html>
<html> <head>
<title>{{.Title}}</title>
<link rel="stylesheet" href="http://tilde.cat/style.css">
</head>
<body> <body>
<h1>~🐱 signup form</h1> `))
<form action="/post" method="post">
Username: var footer = template.Must(header.New("footer").Parse(`</body>
<input type="text" name="username"><br/> </html>`))
Email:
<input type="email" name="email"><br/> var statusTemplate = template.Must(footer.New("status").Parse(`
Why would you want an account here? {{ template "header" .Global }}
<textarea name="why" cols=50 rows=10></textarea><br/> Status: {{ .Status }}
SSH key: {{ template "footer" .Global }}`))
<textarea name="sshpublickey" cols=50 rows=10></textarea><br/>
<input type="submit" value="Submit"> var formTemplate = template.Must(header.New("form").Parse(`{{ template "header" . }}
</form> <h1>~🐱 Sign up form</h1>
</body> <form action="/post" method="post">
</html> <table>
<tr>
<th>Username:</th>
<th><input type="text" name="username"><br/></th>
</tr>
<tr>
<th>Email:</th>
<th><input type="email" name="email"><br/></th>
</tr>
<tr>
<th>Why would you want an account here?</th>
<th><textarea name="why" cols=50 rows=10></textarea><br/></th>
</tr>
<tr>
<th>SSH key:</th>
<th><textarea name="sshpublickey" cols=50 rows=10></textarea><br/></th>
</tr>
<tr><th colspan="2"><input type="submit" value="Submit"></th></tr>
</table>
</form>
{{ template "footer" . }}
`)) `))