Feat: better logs; login page

This commit is contained in:
Grail Finder
2025-05-02 10:54:51 +03:00
parent 043026de75
commit d18855cd49
6 changed files with 31 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.aider* .aider*
golias golias
store.json

View File

@ -8,6 +8,8 @@
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script> <script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
</head> </head>
<body> <body>
<div id=ancestor>
{{template "login"}}
<h1>Word Color Cards</h1> <h1>Word Color Cards</h1>
<div style="display: flex; gap: 1rem; flex-wrap: wrap; padding: 1rem;"> <div style="display: flex; gap: 1rem; flex-wrap: wrap; padding: 1rem;">
{{range $word, $color := .}} {{range $word, $color := .}}
@ -19,12 +21,13 @@
min-width: 100px; min-width: 100px;
text-align: center; text-align: center;
color: white; color: white;
text-shadow: 0 2px 4px rgba(0,0,0,0.5); text-shadow: 0 2px 4px rgba(0,0,0,0.8);
"> ">
{{$word}} {{$word}}
</div> </div>
{{end}} {{end}}
</div> </div>
</div>
</body> </body>
</html> </html>
{{end}} {{end}}

16
components/login.html Normal file
View File

@ -0,0 +1,16 @@
{{define "login"}}
<div id="logindiv">
<form class="space-y-6" hx-post="/login" hx-target="#ancestor">
<div>
<label For="username" class="block text-sm font-medium leading-6 text-white-900">tell us your username</label>
<div class="mt-2">
<input id="username" name="username" hx-target="#login_notice" hx-swap="outerHTML" hx-post="/check/name" hx-trigger="input changed delay:400ms" autocomplete="username" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 text-center"/>
</div>
<div id="login_notice">this name looks available</div>
</div>
<div>
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Sign in</button>
</div>
</form>
</div>
{{end}}

View File

@ -8,7 +8,6 @@ import (
"golias/models" "golias/models"
"golias/utils" "golias/utils"
"html/template" "html/template"
"log/slog"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -24,7 +23,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
username := r.PostFormValue("username") username := r.PostFormValue("username")
if username == "" { if username == "" {
msg := "username not provided" msg := "username not provided"
slog.Error(msg) log.Error(msg)
abortWithError(w, msg) abortWithError(w, msg)
return return
} }
@ -34,7 +33,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
// login user // login user
cookie, err := makeCookie(cleanName, r.RemoteAddr) cookie, err := makeCookie(cleanName, r.RemoteAddr)
if err != nil { if err != nil {
slog.Error("failed to login", "error", err) log.Error("failed to login", "error", err)
abortWithError(w, err.Error()) abortWithError(w, err.Error())
return return
} }
@ -75,13 +74,13 @@ func makeCookie(username string, remote string) (*http.Cookie, error) {
SameSite: http.SameSiteNoneMode, SameSite: http.SameSiteNoneMode,
Domain: cfg.ServerConfig.Host, Domain: cfg.ServerConfig.Host,
} }
slog.Info("check remote addr for cookie set", log.Info("check remote addr for cookie set",
"remote", remote, "session", session) "remote", remote, "session", session)
if strings.Contains(remote, "192.168.0") { if strings.Contains(remote, "192.168.0") {
// no idea what is going on // no idea what is going on
// cookie.Domain = "192.168.0.15" // cookie.Domain = "192.168.0.15"
cookie.Domain = "home.host" cookie.Domain = "home.host"
slog.Info("changing cookie domain", "domain", cookie.Domain) log.Info("changing cookie domain", "domain", cookie.Domain)
} }
// set ctx? // set ctx?
// set user in session // set user in session

View File

@ -32,13 +32,9 @@ func (w *responseWriterWrapper) WriteHeader(status int) {
func LogRequests(next http.Handler) http.Handler { func LogRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
log.Info("request started", "method", r.Method, "path", r.URL.Path)
// Wrap response writer to capture status code // Wrap response writer to capture status code
ww := &responseWriterWrapper{ResponseWriter: w} ww := &responseWriterWrapper{ResponseWriter: w}
next.ServeHTTP(ww, r) next.ServeHTTP(ww, r)
duration := time.Since(start) duration := time.Since(start)
log.Debug("request completed", log.Debug("request completed",
"method", r.Method, "method", r.Method,

View File

@ -2,16 +2,16 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"golias/handlers" "golias/handlers"
"net/http"
"time" "time"
) )
// TODO: add config as param // TODO: add config as param
func ListenToRequests(port string) error{ func ListenToRequests(port string) error {
mux := http.NewServeMux() mux := http.NewServeMux()
server := &http.Server{ server := &http.Server{
Handler: mux, Handler: handlers.LogRequests(handlers.GetSession(mux)),
Addr: port, Addr: port,
ReadTimeout: time.Second * 5, ReadTimeout: time.Second * 5,
WriteTimeout: time.Second * 5, WriteTimeout: time.Second * 5,