Improve config and comments
This commit is contained in:
parent
78056d8b48
commit
7fdb0bf0f4
5 changed files with 18 additions and 17 deletions
|
@ -27,9 +27,9 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
||||||
password = strings.TrimSpace(password)
|
password = strings.TrimSpace(password)
|
||||||
if !(validEmail(email) && validPass(password) && !storage.Data.Taken(email)) {
|
if !(validEmail(email) && validPass(password) && !storage.Data.Taken(email)) {
|
||||||
slog.Debug("outcome",
|
slog.Debug("outcome",
|
||||||
"email", validEmail(email),
|
"valid_email", validEmail(email),
|
||||||
"pass", validPass(password),
|
"valid_pass", validPass(password),
|
||||||
"taken", !storage.Data.Taken(email))
|
"taken_email", storage.Data.Taken(email))
|
||||||
http.Error(w, "invalid auth credentials", http.StatusBadRequest)
|
http.Error(w, "invalid auth credentials", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ var findUserCmd = &cobra.Command{
|
||||||
Run: findUser,
|
Run: findUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Better name.
|
|
||||||
func findUser(cmd *cobra.Command, args []string) {
|
func findUser(cmd *cobra.Command, args []string) {
|
||||||
var user storage.User
|
var user storage.User
|
||||||
var ok bool
|
var ok bool
|
||||||
|
@ -28,7 +27,7 @@ func findUser(cmd *cobra.Command, args []string) {
|
||||||
} else if args[0] == "uuid" {
|
} else if args[0] == "uuid" {
|
||||||
user, ok = storage.Data.ById(args[1])
|
user, ok = storage.Data.ById(args[1])
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("expected email or uuid")
|
fmt.Println("Expected email or uuid")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -30,7 +30,6 @@ var verifyCmd = &cobra.Command{
|
||||||
Run: verifyFunc,
|
Run: verifyFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Needs a better name?
|
|
||||||
func verifyFunc(cmd *cobra.Command, args []string) {
|
func verifyFunc(cmd *cobra.Command, args []string) {
|
||||||
if verifyToken == "" {
|
if verifyToken == "" {
|
||||||
fmt.Println("Empty token supplied!")
|
fmt.Println("Empty token supplied!")
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config stores app configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
KeyFile string `json:"key-file"`
|
KeyFile string `json:"key-file"`
|
||||||
|
@ -27,25 +28,27 @@ var (
|
||||||
Cfg Config
|
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)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return Config{}, err
|
||||||
}
|
}
|
||||||
temp_config := DefaultConfig
|
conf := DefaultConfig
|
||||||
err = json.Unmarshal(data, &temp_config)
|
err = json.Unmarshal(data, &conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return Config{}, err
|
||||||
}
|
}
|
||||||
Cfg = temp_config
|
slog.Debug("Loaded config", "file", filename, "config", conf)
|
||||||
slog.Debug("Loaded config", "file", filename, "config", Cfg)
|
return conf, nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustLoad handles initial configuration loading
|
||||||
func MustLoad(filename string) {
|
func MustLoad(filename string) {
|
||||||
err := Load(filename)
|
conf, err := load(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error initially loading config", "error", err)
|
slog.Error("error initially loading config", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Cfg = conf
|
||||||
}
|
}
|
|
@ -18,7 +18,7 @@ const create string = `
|
||||||
PRIMARY KEY("uuid")
|
PRIMARY KEY("uuid")
|
||||||
);`
|
);`
|
||||||
|
|
||||||
// SQLiteStorage is a storage.Storage implementation with SQLite3
|
// SQLiteStorage is a Storage implementation with SQLite3
|
||||||
type SQLiteStorage struct {
|
type SQLiteStorage struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue