Refactor, bold index.gmi in subfolders
alex wennerberg alex@alexwennerberg.com
Sun, 24 Jan 2021 21:41:42 -0800
3 files changed,
22 insertions(+),
22 deletions(-)
M
http.go
→
http.go
@@ -436,26 +436,6 @@ const ok = "-0123456789abcdefghijklmnopqrstuvwxyz"
var bannedUsernames = []string{"www", "proxy", "grafana"} -func isOkUsername(s string) error { - if len(s) < 1 { - return fmt.Errorf("Username is too short") - } - if len(s) > 32 { - return fmt.Errorf("Username is too long. 32 char max.") - } - for _, char := range s { - if !strings.Contains(ok, strings.ToLower(string(char))) { - return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.") - } - } - for _, username := range bannedUsernames { - if username == s { - return fmt.Errorf("Username is not allowed.") - } - } - return nil -} - func registerHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { data := struct {@@ -765,7 +745,7 @@ }
func runHTTPServer() { log.Printf("Running http server with hostname %s on port %d.", c.Host, c.HttpPort) var err error - t = template.New("main").Funcs(template.FuncMap{"parent": path.Dir}) + t = template.New("main").Funcs(template.FuncMap{"parent": path.Dir, "hasSuffix": strings.HasSuffix}) t, err = t.ParseGlob(path.Join(c.TemplatesDirectory, "*.html")) if err != nil { log.Fatal(err)
M
templates/my_site.html
→
templates/my_site.html
@@ -30,7 +30,7 @@ </td>
{{ else }} <td> <a href="//{{.Creator}}.{{.Host}}/{{.Name}}"> - {{ if eq .Name "index.gmi" }} + {{ if hasSuffix .Name "index.gmi" }} <b>{{ .Name }}</b> {{ else }} {{ .Name }}
M
utils.go
→
utils.go
@@ -27,6 +27,26 @@ }
return result } +func isOkUsername(s string) error { + if len(s) < 1 { + return fmt.Errorf("Username is too short") + } + if len(s) > 32 { + return fmt.Errorf("Username is too long. 32 char max.") + } + for _, char := range s { + if !strings.Contains(ok, strings.ToLower(string(char))) { + return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.") + } + } + for _, username := range bannedUsernames { + if username == s { + return fmt.Errorf("Username is not allowed.") + } + } + return nil +} + // Check if it is a text file, first by checking mimetype, then by reading bytes // Stolen from https://github.com/golang/tools/blob/master/godoc/util/util.go func isTextFile(fullPath string) bool {