Feat: better logs; login page
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.aider*
|
||||
golias
|
||||
store.json
|
||||
|
@ -8,6 +8,8 @@
|
||||
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id=ancestor>
|
||||
{{template "login"}}
|
||||
<h1>Word Color Cards</h1>
|
||||
<div style="display: flex; gap: 1rem; flex-wrap: wrap; padding: 1rem;">
|
||||
{{range $word, $color := .}}
|
||||
@ -19,12 +21,13 @@
|
||||
min-width: 100px;
|
||||
text-align: center;
|
||||
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}}
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
|
16
components/login.html
Normal file
16
components/login.html
Normal 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}}
|
@ -8,7 +8,6 @@ import (
|
||||
"golias/models"
|
||||
"golias/utils"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@ -24,7 +23,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
|
||||
username := r.PostFormValue("username")
|
||||
if username == "" {
|
||||
msg := "username not provided"
|
||||
slog.Error(msg)
|
||||
log.Error(msg)
|
||||
abortWithError(w, msg)
|
||||
return
|
||||
}
|
||||
@ -34,7 +33,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
|
||||
// login user
|
||||
cookie, err := makeCookie(cleanName, r.RemoteAddr)
|
||||
if err != nil {
|
||||
slog.Error("failed to login", "error", err)
|
||||
log.Error("failed to login", "error", err)
|
||||
abortWithError(w, err.Error())
|
||||
return
|
||||
}
|
||||
@ -75,13 +74,13 @@ func makeCookie(username string, remote string) (*http.Cookie, error) {
|
||||
SameSite: http.SameSiteNoneMode,
|
||||
Domain: cfg.ServerConfig.Host,
|
||||
}
|
||||
slog.Info("check remote addr for cookie set",
|
||||
log.Info("check remote addr for cookie set",
|
||||
"remote", remote, "session", session)
|
||||
if strings.Contains(remote, "192.168.0") {
|
||||
// no idea what is going on
|
||||
// cookie.Domain = "192.168.0.15"
|
||||
cookie.Domain = "home.host"
|
||||
slog.Info("changing cookie domain", "domain", cookie.Domain)
|
||||
log.Info("changing cookie domain", "domain", cookie.Domain)
|
||||
}
|
||||
// set ctx?
|
||||
// set user in session
|
||||
|
@ -32,13 +32,9 @@ func (w *responseWriterWrapper) WriteHeader(status int) {
|
||||
func LogRequests(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
log.Info("request started", "method", r.Method, "path", r.URL.Path)
|
||||
|
||||
// Wrap response writer to capture status code
|
||||
ww := &responseWriterWrapper{ResponseWriter: w}
|
||||
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
duration := time.Since(start)
|
||||
log.Debug("request completed",
|
||||
"method", r.Method,
|
||||
|
4
main.go
4
main.go
@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"golias/handlers"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
func ListenToRequests(port string) error {
|
||||
mux := http.NewServeMux()
|
||||
server := &http.Server{
|
||||
Handler: mux,
|
||||
Handler: handlers.LogRequests(handlers.GetSession(mux)),
|
||||
Addr: port,
|
||||
ReadTimeout: time.Second * 5,
|
||||
WriteTimeout: time.Second * 5,
|
||||
|
Reference in New Issue
Block a user