From 625994d1e9b92848a0bf8728a3d60bc367576c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 24 May 2020 21:42:15 +0200 Subject: [PATCH] feat: add basic sign up instructions --- models/config.go | 19 +++++++++++++++++++ models/user.go | 6 ++++++ routes/public.go | 4 ++-- routes/routes.go | 3 +++ version.txt | 2 +- views/footer.tpl.html | 12 ++++++++++-- views/index.tpl.html | 4 ++-- views/signup.tpl.html | 19 ++++++++++++++----- 8 files changed, 57 insertions(+), 12 deletions(-) diff --git a/models/config.go b/models/config.go index 245cc92..9b21468 100644 --- a/models/config.go +++ b/models/config.go @@ -16,6 +16,7 @@ var cfg *Config type Config struct { Env string + Version string Port int Addr string BasePath string @@ -56,11 +57,28 @@ func LookupFatal(key string) string { return v } +func readVersion() string { + file, err := os.Open("version.txt") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + bytes, err := ioutil.ReadAll(file) + if err != nil { + log.Fatal(err) + } + + return string(bytes) +} + func readConfig() *Config { if err := godotenv.Load(); err != nil { log.Fatal(err) } + version := readVersion() + env := LookupFatal("ENV") dbType := LookupFatal("WAKAPI_DB_TYPE") dbUser := LookupFatal("WAKAPI_DB_USER") @@ -131,6 +149,7 @@ func readConfig() *Config { return &Config{ Env: env, + Version: version, Port: port, Addr: addr, BasePath: basePath, diff --git a/models/user.go b/models/user.go index e1cf662..208d5e6 100644 --- a/models/user.go +++ b/models/user.go @@ -16,3 +16,9 @@ type Signup struct { Password string `schema:"password"` PasswordRepeat string `schema:"password_repeat"` } + +func (s *Signup) IsValid() bool { + return len(s.Username) >= 3 && + len(s.Password) >= 6 && + s.Password == s.PasswordRepeat +} diff --git a/routes/public.go b/routes/public.go index 798cb57..8498efc 100644 --- a/routes/public.go +++ b/routes/public.go @@ -164,8 +164,8 @@ func (h *IndexHandler) handlePostSignup(w http.ResponseWriter, r *http.Request) return } - if signup.Password != signup.PasswordRepeat { - respondAlert(w, "passwords do not match", "", "signup.tpl.html", http.StatusBadRequest) + if !signup.IsValid() { + respondAlert(w, "invalid parameters", "", "signup.tpl.html", http.StatusBadRequest) return } diff --git a/routes/routes.go b/routes/routes.go index bba9963..2731dc1 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -27,6 +27,9 @@ func loadTemplates() { "getBasePath": func() string { return models.GetConfig().BasePath }, + "getVersion": func() string { + return models.GetConfig().Version + }, }) templates = make(map[string]*template.Template) diff --git a/version.txt b/version.txt index e21e727..13175fd 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.4.0 \ No newline at end of file +1.4.1 \ No newline at end of file diff --git a/views/footer.tpl.html b/views/footer.tpl.html index 0a3a918..4cfc190 100644 --- a/views/footer.tpl.html +++ b/views/footer.tpl.html @@ -1,3 +1,11 @@ -