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

@ -3,50 +3,50 @@ testdata_dir = "testdata"
tmp_dir = "tmp" tmp_dir = "tmp"
[build] [build]
args_bin = [] args_bin = []
bin = "./tmp/main" bin = "./tmp/main"
cmd = "go build -o ./tmp/main ." cmd = "go build -o ./tmp/main ./cmd/server/main.go"
delay = 1000 delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"] exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = [] exclude_file = []
exclude_regex = ["_test.go"] exclude_regex = ["_test.go"]
exclude_unchanged = false exclude_unchanged = false
follow_symlink = false follow_symlink = false
full_bin = "" full_bin = ""
include_dir = [] include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"] include_ext = ["go", "tpl", "tmpl", "html"]
include_file = [] include_file = []
kill_delay = "0s" kill_delay = "0s"
log = "build-errors.log" log = "build-errors.log"
poll = false poll = false
poll_interval = 0 poll_interval = 0
post_cmd = [] post_cmd = []
pre_cmd = [] pre_cmd = []
rerun = false rerun = false
rerun_delay = 500 rerun_delay = 500
send_interrupt = false send_interrupt = false
stop_on_error = false stop_on_error = false
[color] [color]
app = "" app = ""
build = "yellow" build = "yellow"
main = "magenta" main = "magenta"
runner = "green" runner = "green"
watcher = "cyan" watcher = "cyan"
[log] [log]
main_only = false main_only = false
silent = false silent = false
time = false time = false
[misc] [misc]
clean_on_exit = false clean_on_exit = false
[proxy] [proxy]
app_port = 0 app_port = 0
enabled = false enabled = false
proxy_port = 0 proxy_port = 0
[screen] [screen]
clear_on_rebuild = false clear_on_rebuild = false
keep_scroll = true keep_scroll = true

View file

@ -1,56 +1,17 @@
package main package main
import ( import (
"html/template"
"log" "log"
"net/http" "net/http"
"github.com/rjnemo/auth/web" "github.com/rjnemo/auth/internal/server"
)
var (
loggedIn = false
templates = template.Must(template.ParseFS(web.Templates, "templates/index.html", "templates/in.html", "templates/unauthorized.html"))
) )
func main() { func main() {
mux := http.NewServeMux() srv := server.New()
mux.HandleFunc("GET /", handleIndex)
mux.HandleFunc("GET /in", handleIn)
mux.HandleFunc("POST /login", handleLogin)
log.Println("Starting server on http://localhost:8000") 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) 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 module github.com/rjnemo/auth
go 1.25.1 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
}