Compare commits
3 Commits
a7d42f515d
...
095b35f410
| Author | SHA1 | Date | |
|---|---|---|---|
|
095b35f410
|
|||
|
50546ae4d4
|
|||
|
2d34202c9e
|
+26
-15
@@ -7,15 +7,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func handler(writer http.ResponseWriter, request *http.Request) {
|
||||
templ := template.Must(template.ParseFiles("./web/dist/index.html"))
|
||||
|
||||
templ.Execute(writer, nil)
|
||||
}
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
@@ -23,20 +21,27 @@ func main() {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
// handle authentication
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Lifetime = 24 * time.Hour
|
||||
|
||||
// handle frontend views
|
||||
webDir := "./web/dist"
|
||||
fs := http.FileServer(http.Dir(webDir))
|
||||
|
||||
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Path != "/" {
|
||||
path := filepath.Join(webDir, filepath.Clean(request.URL.Path))
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
fs.ServeHTTP(writer, request)
|
||||
return
|
||||
http.Handle("/", sessionManager.LoadAndSave(
|
||||
http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Path != "/" {
|
||||
path := filepath.Join(webDir, filepath.Clean(request.URL.Path))
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
fs.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
handler(writer, request)
|
||||
})
|
||||
handler(writer, request)
|
||||
}),
|
||||
))
|
||||
|
||||
port := os.Getenv("HTTP_PORT")
|
||||
if port == "" {
|
||||
@@ -48,3 +53,9 @@ func main() {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ module solopm.com/solopm-server
|
||||
go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/alexedwards/scs/v2 v2.9.0 // indirect
|
||||
github.com/golang-migrate/migrate/v4 v4.19.1 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
)
|
||||
|
||||
@@ -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/go.mod h1:CTcgfjxhaUtsLipnLoQRWCrjYXycRz/g5+RWDuYgPrE=
|
||||
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
|
||||
(username,is_admin,name,email,avatar_url)
|
||||
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
|
||||
(username,is_admin,name,email,avatar_url)
|
||||
VALUES
|
||||
('zombie', false, 'Zombie', 'zombie@localhost', '')
|
||||
('zombie', false, 'Zombie', 'zombie@localhost', '/uploads/avatars/zombie.png')
|
||||
;
|
||||
-- -------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user