Compare commits

...

3 Commits

Author SHA1 Message Date
brian 095b35f410 i'm getting...something...in the cookie now 2026-05-22 16:38:44 -06:00
brian 50546ae4d4 adding dummy avatars 2026-05-22 16:38:29 -06:00
brian 2d34202c9e adding scs for auth layer 2026-05-22 16:17:08 -06:00
4 changed files with 31 additions and 17 deletions
+26 -15
View File
@@ -7,15 +7,13 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/alexedwards/scs/v2"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
func handler(writer http.ResponseWriter, request *http.Request) { var sessionManager *scs.SessionManager
templ := template.Must(template.ParseFiles("./web/dist/index.html"))
templ.Execute(writer, nil)
}
func main() { func main() {
err := godotenv.Load() err := godotenv.Load()
@@ -23,20 +21,27 @@ func main() {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
} }
// handle authentication
sessionManager = scs.New()
sessionManager.Lifetime = 24 * time.Hour
// handle frontend views
webDir := "./web/dist" webDir := "./web/dist"
fs := http.FileServer(http.Dir(webDir)) fs := http.FileServer(http.Dir(webDir))
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { http.Handle("/", sessionManager.LoadAndSave(
if request.URL.Path != "/" { http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
path := filepath.Join(webDir, filepath.Clean(request.URL.Path)) if request.URL.Path != "/" {
_, err := os.Stat(path) path := filepath.Join(webDir, filepath.Clean(request.URL.Path))
if err == nil { _, err := os.Stat(path)
fs.ServeHTTP(writer, request) if err == nil {
return fs.ServeHTTP(writer, request)
return
}
} }
} handler(writer, request)
handler(writer, request) }),
}) ))
port := os.Getenv("HTTP_PORT") port := os.Getenv("HTTP_PORT")
if port == "" { if port == "" {
@@ -48,3 +53,9 @@ func main() {
log.Fatalf("server error: %v", err) log.Fatalf("server error: %v", err)
} }
} }
func handler(writer http.ResponseWriter, request *http.Request) {
templ := template.Must(template.ParseFiles("./web/dist/index.html"))
templ.Execute(writer, nil)
}
+1
View File
@@ -3,6 +3,7 @@ module solopm.com/solopm-server
go 1.26.3 go 1.26.3
require ( require (
github.com/alexedwards/scs/v2 v2.9.0 // indirect
github.com/golang-migrate/migrate/v4 v4.19.1 // indirect github.com/golang-migrate/migrate/v4 v4.19.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
) )
+2
View File
@@ -1,3 +1,5 @@
github.com/alexedwards/scs/v2 v2.9.0 h1:xa05mVpwTBm1iLeTMNFfAWpKUm4fXAW7CeAViqBVS90=
github.com/alexedwards/scs/v2 v2.9.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/golang-migrate/migrate/v4 v4.19.1 h1:OCyb44lFuQfYXYLx1SCxPZQGU7mcaZ7gH9yH4jSFbBA= github.com/golang-migrate/migrate/v4 v4.19.1 h1:OCyb44lFuQfYXYLx1SCxPZQGU7mcaZ7gH9yH4jSFbBA=
github.com/golang-migrate/migrate/v4 v4.19.1/go.mod h1:CTcgfjxhaUtsLipnLoQRWCrjYXycRz/g5+RWDuYgPrE= github.com/golang-migrate/migrate/v4 v4.19.1/go.mod h1:CTcgfjxhaUtsLipnLoQRWCrjYXycRz/g5+RWDuYgPrE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@@ -30,7 +30,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "idx_users_provider"
INSERT INTO users INSERT INTO users
(username,is_admin,name,email,avatar_url) (username,is_admin,name,email,avatar_url)
VALUES VALUES
('system', true, 'System User', 'system@localhost', '') ('system', true, 'System User', 'system@localhost', '/uploads/avatars/system.png')
; ;
-- ------------------------------------------------------------- -- -------------------------------------------------------------
@@ -38,6 +38,6 @@ VALUES
INSERT INTO users INSERT INTO users
(username,is_admin,name,email,avatar_url) (username,is_admin,name,email,avatar_url)
VALUES VALUES
('zombie', false, 'Zombie', 'zombie@localhost', '') ('zombie', false, 'Zombie', 'zombie@localhost', '/uploads/avatars/zombie.png')
; ;
-- ------------------------------------------------------------- -- -------------------------------------------------------------