aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-14 01:24:40 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-14 01:24:40 +0100
commit08a3fb8a2b0281c3c329b33215ec7f8866add606 (patch)
treeff8a5413449ea198bc063bf0099fc025ea49c82b /main.go
parent684787bcb72aece2aa914597a3bc8788432e66f7 (diff)
Add authentication and ability to change host
Diffstat (limited to 'main.go')
-rw-r--r--main.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/main.go b/main.go
index 7a38d82..5be14da 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "crypto/rand"
"fmt"
"log/slog"
"net/http"
@@ -8,6 +9,7 @@ import (
"path/filepath"
"github.com/LMBishop/scrapbook/api"
+ "github.com/LMBishop/scrapbook/pkg/auth"
"github.com/LMBishop/scrapbook/pkg/config"
"github.com/LMBishop/scrapbook/pkg/constants"
"github.com/LMBishop/scrapbook/pkg/index"
@@ -41,10 +43,19 @@ func main() {
slog.Warn("command interface host is empty - neither api or web interface will be accessible")
}
+ if cfg.Command.Secret == "" {
+ slog.Warn("command interface secret is empty - neither api or web interface will be accessible")
+ }
+
+ secretKey := make([]byte, 32)
+ rand.Read(secretKey)
+
+ authenticator := auth.NewAuthenticator(secretKey)
+
mux := http.NewServeMux()
mux.Handle(fmt.Sprintf("%s/api/", cfg.Command.Host), http.StripPrefix("/api/", api.NewMux(&cfg, siteIndex)))
- mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex))
+ mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex, authenticator))
mux.HandleFunc("/", server.ServeSite(siteIndex))
err = http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port), mux)