frontend-foundation: GOAT front-end Phase 1 — auth, tag management, public tag page (22/22 spec scenarios pass)
This commit is contained in:
71
frontend/main.go
Normal file
71
frontend/main.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"wherewoof/frontend/internal/auth"
|
||||
"wherewoof/frontend/internal/db"
|
||||
"wherewoof/frontend/internal/handlers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn == "" {
|
||||
log.Fatal("DATABASE_URL not set")
|
||||
}
|
||||
secret := os.Getenv("SESSION_SECRET")
|
||||
if secret == "" {
|
||||
log.Fatal("SESSION_SECRET not set")
|
||||
}
|
||||
addr := os.Getenv("ADDR")
|
||||
if addr == "" {
|
||||
addr = ":3020"
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
log.Fatal("connect:", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
auth.InitSessionStore(secret)
|
||||
|
||||
tpl, err := handlers.LoadTemplates()
|
||||
if err != nil {
|
||||
log.Fatal("templates:", err)
|
||||
}
|
||||
|
||||
app := handlers.New(db.New(pool), tpl)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
mux.HandleFunc("GET /{$}", app.Home)
|
||||
mux.HandleFunc("/{path...}", http.NotFound)
|
||||
mux.HandleFunc("GET /t/{tag_code}", app.PublicTag)
|
||||
mux.HandleFunc("GET /register", app.RegisterPage)
|
||||
mux.HandleFunc("POST /register", app.Register)
|
||||
mux.HandleFunc("GET /login", app.LoginPage)
|
||||
mux.HandleFunc("POST /login", app.Login)
|
||||
mux.HandleFunc("POST /logout", app.Logout)
|
||||
mux.Handle("GET /account", app.RequireAuth(app.Account))
|
||||
mux.Handle("POST /account/tags", app.RequireAuth(app.AddTag))
|
||||
mux.Handle("GET /account/tags/{id}/edit", app.RequireAuth(app.EditTagPage))
|
||||
mux.Handle("POST /account/tags/{id}/edit", app.RequireAuth(app.EditTag))
|
||||
mux.Handle("POST /account/tags/{id}/delete", app.RequireAuth(app.DeleteTag))
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
}
|
||||
log.Println("wherewoof listening on", addr)
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
Reference in New Issue
Block a user