Add basic flags

This commit is contained in:
Andrew-71 2024-03-26 14:00:32 +03:00
parent 94952b5766
commit 1bd41cad43
7 changed files with 28 additions and 5 deletions

4
.gitignore vendored
View file

@ -18,10 +18,10 @@
go.work
# Executable
hibiscus
hibiscus-txt
.idea/
# Because currently it's used in "prod"
data/
/config/log.txt
config/log.txt

View file

@ -5,7 +5,6 @@ List of things to add to this project
* Check export function for improvements
* Add missing frontend pages
* More slog.Debug?
* Make the CLI multi-functional - export to path, edit file etc.
* Think about timezones
* Consider more secure auth methods
* *Go* dependency-less? <-- this is a terrible idea

View file

@ -19,6 +19,7 @@ type failedLogin struct {
var failedLogins []failedLogin
// NoteLoginFail attempts to counteract bruteforce/spam attacks
func NoteLoginFail(username string, password string, r *http.Request) {
slog.Warn("failed auth", "username", username, "password", password, "address", r.RemoteAddr)
NotifyTelegram(fmt.Sprintf("Failed auth attempt in hibiscus:\nusername=%s\npassword=%s\nremote=%s", username, password, r.RemoteAddr))

View file

@ -23,7 +23,7 @@ func Export(filename string) error {
defer w.Close()
walker := func(path string, info os.FileInfo, err error) error {
if path == filename { // Do not add export .zip itself
if path == filename || filepath.Ext(path) == ".zip" { //Ignore export file itself and .zip archives
return nil
}
slog.Debug("export crawling", "path", path)

22
flags.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"flag"
)
func FlagInit() {
username := flag.String("user", "", "override username")
password := flag.String("pass", "", "override password")
port := flag.Int("port", 0, "override port")
flag.Parse()
if *username != "" {
Cfg.Username = *username
}
if *password != "" {
Cfg.Password = *password
}
if *port != 0 {
Cfg.Port = *port
}
}

View file

@ -4,5 +4,6 @@ var Cfg = ConfigInit()
func main() {
LogInit()
FlagInit()
Serve()
}

View file

@ -12,7 +12,7 @@ import (
func Serve() {
r := chi.NewRouter()
r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes)
r.Use(BasicAuth) // TODO: is this good enough?
r.Use(BasicAuth) // Is this good enough? Sure hope so
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
http.ServeFile(w, r, "./pages/error/404.html")