Added database support and update login to utilize db

This commit is contained in:
2025-02-08 19:39:44 +11:00
parent a842d09f96
commit 597fc6f072
8 changed files with 151 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
package server
import (
"database/sql"
"net/http"
"projectreshoot/handlers"
"projectreshoot/view/page"
@@ -9,6 +10,7 @@ import (
func addRoutes(
mux *http.ServeMux,
config *Config,
conn *sql.DB,
) {
// Static files
mux.Handle("GET /static/", http.StripPrefix("/static/", handlers.HandleStatic()))
@@ -21,5 +23,5 @@ func addRoutes(
// Login page and handlers
mux.Handle("GET /login", handlers.HandleLoginPage(config.TrustedHost))
mux.Handle("POST /login", handlers.HandleLoginRequest())
mux.Handle("POST /login", handlers.HandleLoginRequest(conn))
}

View File

@@ -1,19 +1,23 @@
package server
import (
"database/sql"
"errors"
"fmt"
"net/http"
"os"
"projectreshoot/middleware"
"github.com/joho/godotenv"
)
type Config struct {
TrustedHost string
Host string
Port string
TrustedHost string
TursoURL string
TursoToken string
}
func GetConfig() (*Config, error) {
@@ -26,6 +30,8 @@ func GetConfig() (*Config, error) {
Host: os.Getenv("HOST"),
Port: os.Getenv("PORT"),
TrustedHost: os.Getenv("TRUSTED_HOST"),
TursoURL: os.Getenv("TURSO_DATABASE_URL"),
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
}
if config.Host == "" {
return nil, errors.New("Envar not set: HOST")
@@ -36,15 +42,22 @@ func GetConfig() (*Config, error) {
if config.TrustedHost == "" {
return nil, errors.New("Envar not set: TRUSTED_HOST")
}
if config.TursoURL == "" {
return nil, errors.New("Envar not set: TURSO_DATABASE_URL")
}
if config.TursoToken == "" {
return nil, errors.New("Envar not set: TURSO_AUTH_TOKEN")
}
return config, nil
}
func NewServer(config *Config) http.Handler {
func NewServer(config *Config, conn *sql.DB) http.Handler {
mux := http.NewServeMux()
addRoutes(
mux,
config,
conn,
)
var handler http.Handler = mux
handler = middleware.Logging(handler)