refactor: modularize server routes

This commit is contained in:
Ruidy 2025-09-19 23:46:40 +02:00
parent 2ff7bc57be
commit 6180b24cf6
No known key found for this signature in database
GPG key ID: 705C24D202990805
9 changed files with 143 additions and 79 deletions

View file

@ -5,7 +5,7 @@ tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
cmd = "go build -o ./tmp/main ./cmd/server/main.go"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []

View file

@ -1,56 +1,17 @@
package main
import (
"html/template"
"log"
"net/http"
"github.com/rjnemo/auth/web"
)
var (
loggedIn = false
templates = template.Must(template.ParseFS(web.Templates, "templates/index.html", "templates/in.html", "templates/unauthorized.html"))
"github.com/rjnemo/auth/internal/server"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /", handleIndex)
mux.HandleFunc("GET /in", handleIn)
mux.HandleFunc("POST /login", handleLogin)
srv := server.New()
log.Println("Starting server on http://localhost:8000")
if err := http.ListenAndServe(":8000", mux); err != nil {
if err := http.ListenAndServe(":8000", srv.Router()); err != nil {
log.Fatalf("listen: %v", err)
}
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
err := templates.ExecuteTemplate(w, "index.html", nil)
if err != nil {
http.Error(w, "template render failed", http.StatusInternalServerError)
}
}
func handleIn(w http.ResponseWriter, r *http.Request) {
if !loggedIn {
w.WriteHeader(http.StatusUnauthorized)
err := templates.ExecuteTemplate(w, "unauthorized.html", nil)
if err != nil {
http.Error(w, "template render failed", http.StatusInternalServerError)
}
return
}
err := templates.ExecuteTemplate(w, "in.html", nil)
if err != nil {
http.Error(w, "template render failed", http.StatusInternalServerError)
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
log.Println("Login request received")
loggedIn = true
http.Redirect(w, r, "/in", http.StatusSeeOther)
}

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/rjnemo/auth
go 1.25.1
require github.com/go-chi/chi/v5 v5.2.3 // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=

View file

@ -0,0 +1,14 @@
package server
import (
"log"
"net/http"
)
func (s *Server) loginHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Login request received")
s.loggedIn = true
http.Redirect(w, r, "/in", http.StatusSeeOther)
}
}

View file

@ -0,0 +1,24 @@
package server
import (
"log"
"net/http"
)
func (s *Server) dashboardHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !s.loggedIn {
w.WriteHeader(http.StatusUnauthorized)
if err := s.templates.ExecuteTemplate(w, "unauthorized.html", nil); err != nil {
log.Printf("render unauthorized: %v", err)
http.Error(w, "template render failed", http.StatusInternalServerError)
}
return
}
if err := s.templates.ExecuteTemplate(w, "in.html", nil); err != nil {
log.Printf("render dashboard: %v", err)
http.Error(w, "template render failed", http.StatusInternalServerError)
}
}
}

View file

@ -0,0 +1,15 @@
package server
import (
"log"
"net/http"
)
func (s *Server) indexHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := s.templates.ExecuteTemplate(w, "index.html", nil); err != nil {
log.Printf("render index: %v", err)
http.Error(w, "template render failed", http.StatusInternalServerError)
}
}
}

View file

@ -0,0 +1,9 @@
package server
import "github.com/go-chi/chi/v5"
func (s *Server) registerRoutes(r chi.Router) {
r.Get("/", s.indexHandler())
r.Get("/in", s.dashboardHandler())
r.Post("/login", s.loginHandler())
}

37
internal/server/server.go Normal file
View file

@ -0,0 +1,37 @@
package server
import (
"html/template"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/rjnemo/auth/web"
)
// Server holds HTTP dependencies for the application.
type Server struct {
templates *template.Template
loggedIn bool
}
// New constructs a Server with parsed templates and default state.
func New() *Server {
tmpl := template.Must(template.ParseFS(
web.Templates,
"templates/index.html",
"templates/in.html",
"templates/unauthorized.html",
))
return &Server{
templates: tmpl,
}
}
// Router returns the configured HTTP router.
func (s *Server) Router() http.Handler {
r := chi.NewRouter()
s.registerRoutes(r)
return r
}