2025-11-03 12:38:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-05 17:15:33 +00:00
|
|
|
"errors"
|
2025-11-05 14:15:06 +00:00
|
|
|
"log/slog"
|
2025-11-03 12:38:47 +00:00
|
|
|
"net/http"
|
2025-11-05 17:15:33 +00:00
|
|
|
"strings"
|
2025-11-03 12:38:47 +00:00
|
|
|
)
|
|
|
|
|
func getFavicon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-type", "image/x-icon")
|
|
|
|
|
|
|
|
|
|
http.ServeFile(w, r, "static/favicon.ico")
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 21:05:10 +00:00
|
|
|
func getReport(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
//org := r.URL.Query().Get("org")
|
|
|
|
|
err := htmlReport(w)
|
|
|
|
|
if err != nil {
|
|
|
|
|
respondError(w, "Failed to generate report page", err, http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 12:38:47 +00:00
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
2025-11-05 17:15:33 +00:00
|
|
|
user, err := getAuthenticatedUser(r)
|
|
|
|
|
if err != nil && !errors.Is(err, &NoCredentialsError{}) {
|
|
|
|
|
respondError(w, "Failed to get root", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if user == nil {
|
|
|
|
|
errorCode := r.URL.Query().Get("error")
|
|
|
|
|
err = htmlSignin(w, errorCode)
|
|
|
|
|
} else {
|
|
|
|
|
err = htmlDashboard(w, user)
|
|
|
|
|
}
|
2025-11-03 12:38:47 +00:00
|
|
|
if err != nil {
|
2025-11-05 17:15:33 +00:00
|
|
|
respondError(w, "Failed to render root", err, http.StatusInternalServerError)
|
2025-11-03 12:38:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-04 00:02:51 +00:00
|
|
|
func getSignup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
err := htmlSignup(w, r.URL.Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-04 23:21:13 +00:00
|
|
|
|
2025-11-05 14:15:06 +00:00
|
|
|
func respondError(w http.ResponseWriter, m string, e error, s int) {
|
|
|
|
|
slog.Error(m, slog.Int("status", s), slog.String("err", e.Error()))
|
|
|
|
|
http.Error(w, m, http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 17:15:33 +00:00
|
|
|
func postSignin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
respondError(w, "Could not parse form", err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username := r.FormValue("username")
|
|
|
|
|
password := r.FormValue("password")
|
|
|
|
|
|
|
|
|
|
slog.Info("Signin",
|
|
|
|
|
slog.String("username", username),
|
|
|
|
|
slog.String("password", strings.Repeat("*", len(password))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := signinUser(r, username, password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, InvalidCredentials{}) {
|
|
|
|
|
http.Redirect(w, r, "/?error=invalid-credentials", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
respondError(w, "Failed to signin user", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 23:21:13 +00:00
|
|
|
func postSignup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2025-11-05 14:15:06 +00:00
|
|
|
respondError(w, "Could not parse form", err, http.StatusBadRequest)
|
2025-11-04 23:21:13 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 14:15:06 +00:00
|
|
|
username := r.FormValue("username")
|
2025-11-04 23:21:13 +00:00
|
|
|
name := r.FormValue("name")
|
2025-11-05 14:15:06 +00:00
|
|
|
password := r.FormValue("password")
|
2025-11-04 23:21:13 +00:00
|
|
|
terms := r.FormValue("terms")
|
|
|
|
|
|
2025-11-05 14:15:06 +00:00
|
|
|
slog.Info("Signup",
|
|
|
|
|
slog.String("username", username),
|
2025-11-05 17:15:33 +00:00
|
|
|
slog.String("name", name),
|
|
|
|
|
slog.String("password", strings.Repeat("*", len(password))))
|
2025-11-04 23:21:13 +00:00
|
|
|
|
2025-11-05 14:15:06 +00:00
|
|
|
if terms != "on" {
|
|
|
|
|
slog.Error("Terms not agreed", slog.String("terms", terms))
|
|
|
|
|
http.Error(w, "You must agree to the terms to register", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 17:15:33 +00:00
|
|
|
user, err := signupUser(username, name, password)
|
|
|
|
|
if err != nil {
|
2025-11-05 14:15:06 +00:00
|
|
|
respondError(w, "Failed to signup user", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 17:15:33 +00:00
|
|
|
addUserSession(r, user)
|
|
|
|
|
|
2025-11-05 14:20:56 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2025-11-04 23:21:13 +00:00
|
|
|
}
|