diff --git a/CHANGELOG.md b/CHANGELOG.md index b870f1d..5087a06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,9 @@ This file keeps track of changes in a human-readable fashion These changes are not yet released -* Fully refactored app internally +* Fully refactored the project internally +* Log *directory* is now specified as opposed to *file*. +Files in that directory are generated as `hibiscus_YYYY-MM-DD_HH:MM:SS.log` * Adjusted default theme * Error pages are now translated diff --git a/README.md b/README.md index de0c9b6..78fd897 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Simple plaintext diary. This project is *very* opinionated and minimal, and is designed primarily for my usage. -As a result, I can't guarantee that it's either secure or stable. +As a result, neither security nor stability are guaranteed. ## Features: diff --git a/TODO.md b/TODO.md index 1245c97..87335e5 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,12 @@ # TODO List of things to add to this project -## Urgent (1.1.5-2.0.0) +## Priority 2.0.0 +* (pre-release) re-write README * `style.css` in config instead of theme (provide themes as examples in repo) * Auth improvement so it DOESN'T ASK ME FOR PASSWORD EVERY DAY UGH XD +* Specify data directory in config or with flags +* Configure more of the app with flags, possibly move to `cobra` for this ## Nice to have * Forward/backward buttons for days diff --git a/internal/config/info.go b/internal/config/info.go index e9fa4fe..45383ff 100644 --- a/internal/config/info.go +++ b/internal/config/info.go @@ -1,12 +1,22 @@ package config type AppInfo struct { - Version string - SourceLink string + version string + source string } // Info contains app information. var Info = AppInfo{ - Version: "2.0.0", - SourceLink: "https://git.a71.su/Andrew71/hibiscus", + version: "2.0.0", + source: "https://git.a71.su/Andrew71/hibiscus", +} + +// Version returns the current app version +func (i AppInfo) Version() string { + return i.version +} + +// Source returns app's git repository +func (i AppInfo) Source() string { + return i.source } \ No newline at end of file diff --git a/internal/config/main.go b/internal/config/main.go index 0fe8397..1700092 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -29,7 +29,7 @@ type Config struct { Theme string `config:"theme" type:"string"` Title string `config:"title" type:"string"` LogToFile bool `config:"log_to_file" type:"bool"` - LogFile string `config:"log_file" type:"string"` + LogDir string `config:"log_dir" type:"string"` Scram bool `config:"enable_scram" type:"bool"` TelegramToken string `config:"tg_token" type:"string"` @@ -47,7 +47,7 @@ var DefaultConfig = Config{ Theme: "", Title: "🌺 Hibiscus.txt", LogToFile: false, - LogFile: "config/log.txt", + LogDir: "logs", Scram: false, TelegramToken: "", diff --git a/internal/logging/logger.go b/internal/logging/logger.go index d004e50..6b8c18a 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -5,6 +5,8 @@ import ( "log" "log/slog" "os" + "path" + "time" "git.a71.su/Andrew71/hibiscus-txt/internal/config" "github.com/go-chi/chi/v5/middleware" @@ -12,19 +14,30 @@ import ( var DebugMode = false +// File returns the appropriate filename for log +// (log_dir/hibiscus_YYYY-MM-DD_HH:MM:SS.log) +func File() string { + return config.Cfg.LogDir + "/hibiscus_" + time.Now().In(config.Cfg.Timezone).Format("2006-01-02_15:04:05") + ".log" +} + // LogInit makes slog output to both os.Stdout and a file if needed, and sets slog.LevelDebug if enabled. func LogInit() { - var w io.Writer + logFile := File() + var w io.Writer = os.Stdout if config.Cfg.LogToFile { - f, err := os.OpenFile(config.Cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + // Create dir in case it doesn't exist yet to avoid errors + err := os.MkdirAll(path.Dir(logFile), 0755) if err != nil { - slog.Error("error opening log file, logging to stdout only", "path", config.Cfg.LogFile, "error", err) - return + slog.Error("error creating log dir, logging to stdout only", "path", path.Dir(logFile), "error", err) + } else { + f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + slog.Error("error opening log file, logging to stdout only", "path", logFile, "error", err) + return + } + // No defer f.Close() because that breaks the MultiWriter + w = io.MultiWriter(f, os.Stdout) } - // No defer f.Close() because that breaks the MultiWriter - w = io.MultiWriter(f, os.Stdout) - } else { - w = os.Stdout } // Make slog and chi use intended format @@ -34,4 +47,5 @@ func LogInit() { } slog.SetDefault(slog.New(slog.NewTextHandler(w, opts))) middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true}) + slog.Debug("Debug mode enabled") // This string is only shown if debugging } diff --git a/internal/server/info.go b/internal/server/info.go index 9cfd09d..b451ac0 100644 --- a/internal/server/info.go +++ b/internal/server/info.go @@ -20,6 +20,6 @@ func GetInfo(w http.ResponseWriter, r *http.Request) { // GetVersionApi returns current app version. func GetVersionApi(w http.ResponseWriter, r *http.Request) { - HandleWrite(w.Write([]byte(config.Info.Version))) + HandleWrite(w.Write([]byte(config.Info.Version()))) w.WriteHeader(http.StatusOK) } diff --git a/internal/server/serve.go b/internal/server/serve.go index 7691f73..7ad8c25 100644 --- a/internal/server/serve.go +++ b/internal/server/serve.go @@ -74,6 +74,5 @@ func Serve() { r.Handle("/public/*", fs) slog.Info("🌺 Website working", "port", config.Cfg.Port) - slog.Debug("Debug mode enabled") log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.Cfg.Port), r)) } diff --git a/internal/templates/pages/info.html b/internal/templates/pages/info.html index 8143709..46e060b 100644 --- a/internal/templates/pages/info.html +++ b/internal/templates/pages/info.html @@ -1,7 +1,7 @@ {{ define "main" }}