Download this file
| 1 | package gate |
| 2 | |
| 3 | import ( |
| 4 | _ "embed" |
| 5 | "html/template" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | ) |
| 9 | |
| 10 | //go:embed login.html |
| 11 | var loginHTML string |
| 12 | |
| 13 | var loginTmpl = template.Must(template.New("login").Parse(loginHTML)) |
| 14 | |
| 15 | const ( |
| 16 | msgWrong = "That password is not right." |
| 17 | msgTooMany = "Too many attempts. Try again in a minute." |
| 18 | ) |
| 19 | |
| 20 | type pageData struct { |
| 21 | Host string |
| 22 | Error string |
| 23 | Next string |
| 24 | Action string |
| 25 | } |
| 26 | |
| 27 | // writePage renders the login page at status. Every page this package |
| 28 | // writes is no-store and noindex: it is a curtain, not content. |
| 29 | func writePage(w http.ResponseWriter, r *http.Request, host string, status int, msg, next string) { |
| 30 | h := w.Header() |
| 31 | h.Set("Cache-Control", "no-store") |
| 32 | h.Set("X-Robots-Tag", "noindex, nofollow") |
| 33 | h.Set("Content-Type", "text/html; charset=utf-8") |
| 34 | w.WriteHeader(status) |
| 35 | if r.Method == http.MethodHead { |
| 36 | return |
| 37 | } |
| 38 | if err := loginTmpl.Execute(w, pageData{Host: host, Error: msg, Next: next, Action: LoginPath}); err != nil { |
| 39 | log.Printf("gate: render login page for %s: %v", host, err) |
| 40 | } |
| 41 | } |
| 42 | |