From 7fdb0bf0f4489169bb0ec9d54131089c80e8c050 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Sun, 13 Oct 2024 17:33:59 +0300 Subject: [PATCH] Improve config and comments --- auth/auth.go | 6 +++--- cmd/find_user.go | 3 +-- cmd/verify.go | 1 - config/config.go | 23 +++++++++++++---------- storage/sqlite/sqlite.go | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 19d1582..f38c586 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -27,9 +27,9 @@ func Register(w http.ResponseWriter, r *http.Request) { password = strings.TrimSpace(password) if !(validEmail(email) && validPass(password) && !storage.Data.Taken(email)) { slog.Debug("outcome", - "email", validEmail(email), - "pass", validPass(password), - "taken", !storage.Data.Taken(email)) + "valid_email", validEmail(email), + "valid_pass", validPass(password), + "taken_email", storage.Data.Taken(email)) http.Error(w, "invalid auth credentials", http.StatusBadRequest) return } diff --git a/cmd/find_user.go b/cmd/find_user.go index 1dfba10..c852ea0 100644 --- a/cmd/find_user.go +++ b/cmd/find_user.go @@ -19,7 +19,6 @@ var findUserCmd = &cobra.Command{ Run: findUser, } -// TODO: Better name. func findUser(cmd *cobra.Command, args []string) { var user storage.User var ok bool @@ -28,7 +27,7 @@ func findUser(cmd *cobra.Command, args []string) { } else if args[0] == "uuid" { user, ok = storage.Data.ById(args[1]) } else { - fmt.Println("expected email or uuid") + fmt.Println("Expected email or uuid") return } if !ok { diff --git a/cmd/verify.go b/cmd/verify.go index 59e118a..8492897 100644 --- a/cmd/verify.go +++ b/cmd/verify.go @@ -30,7 +30,6 @@ var verifyCmd = &cobra.Command{ Run: verifyFunc, } -// TODO: Needs a better name? func verifyFunc(cmd *cobra.Command, args []string) { if verifyToken == "" { fmt.Println("Empty token supplied!") diff --git a/config/config.go b/config/config.go index b9859fd..11c3abc 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "os" ) +// Config stores app configuration type Config struct { Port int `json:"port"` KeyFile string `json:"key-file"` @@ -27,25 +28,27 @@ var ( Cfg Config ) -func Load(filename string) error { +// Load parses a JSON-formatted configuration file +func load(filename string) (Config, error) { data, err := os.ReadFile(filename) if err != nil { - return err + return Config{}, err } - temp_config := DefaultConfig - err = json.Unmarshal(data, &temp_config) + conf := DefaultConfig + err = json.Unmarshal(data, &conf) if err != nil { - return err + return Config{}, err } - Cfg = temp_config - slog.Debug("Loaded config", "file", filename, "config", Cfg) - return nil + slog.Debug("Loaded config", "file", filename, "config", conf) + return conf, nil } +// MustLoad handles initial configuration loading func MustLoad(filename string) { - err := Load(filename) + conf, err := load(filename) if err != nil { slog.Error("error initially loading config", "error", err) os.Exit(1) } -} \ No newline at end of file + Cfg = conf +} diff --git a/storage/sqlite/sqlite.go b/storage/sqlite/sqlite.go index 8a93756..e10ce79 100644 --- a/storage/sqlite/sqlite.go +++ b/storage/sqlite/sqlite.go @@ -18,7 +18,7 @@ const create string = ` PRIMARY KEY("uuid") );` -// SQLiteStorage is a storage.Storage implementation with SQLite3 +// SQLiteStorage is a Storage implementation with SQLite3 type SQLiteStorage struct { db *sql.DB }