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

23
main.go
View File

@ -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))
}

View File

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