package web
import (
"fmt"
"net/http"
"nugitea/internal/auth"
"nugitea/internal/user"
)
type Server struct {
userStore *user.Store
auth *auth.Authenticator
}
func NewServer() *Server {
return &Server{
userStore: user.NewStore(),
}
}
func (s *Server) SetupRoutes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", HomeHandler)
mux.HandleFunc("/login", s.LoginHandler)
mux.HandleFunc("/logout", s.LogoutHandler)
mux.Handle("/dashboard", auth.RequireAuth(auth.MockAuthenticate)(s.DashboardHandler))
mux.Handle("/users", auth.RequireAuth(auth.MockAuthenticate)(s.UsersHandler))
return mux
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Welcome to Nugitea</h1><p>This is a minimal Git server.</p>")
}
func (s *Server) LoginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
fmt.Fprintf(w, `
<!DOCTYPE html>
<html>
<head>
<title>Login - Nugitea</title>
</head>
<body>
<h1>Login to Nugitea</h1>
<form method="post" action="/login">
<p>
<label>Username:</label><br>
<input type="text" name="username" required>
</p>
<p>
<label>Password:</label><br>
<input type="password" name="password" required>
</p>
<p>
<button type="submit">Login</button>
</p>
</form>
</body>
</html>
`)
} else {
w.Header().Set("Location", "/dashboard")
w.WriteHeader(http.StatusFound)
}
}
func (s *Server) LogoutHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (s *Server) DashboardHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Dashboard - Nugitea</h1><p>Welcome to your dashboard.</p>")
}
func (s *Server) UsersHandler(w http.ResponseWriter, r *http.Request) {
users := s.userStore.GetAllUsers()
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "<h1>Users</h1><ul>")
for _, user := range users {
fmt.Fprintf(w, "<li>%s (%s)</li>", user.Name, user.Email)
}
fmt.Fprintf(w, "</ul>")
}
func (s *Server) Start(addr string) error {
handler := s.SetupRoutes()
return http.ListenAndServe(addr, handler)
}