slowly but surely fixing routing and layout handling

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-07-31 13:45:34 +01:00
parent 4b58a27fdc
commit a26bdfa646
5 changed files with 80 additions and 170 deletions

View file

@ -1,23 +1,14 @@
package global
import (
"fmt"
"net/http"
"strconv"
"time"
)
var LAST_MODIFIED = time.Now()
var MimeTypes = map[string]string{
"css": "text/css; charset=utf-8",
"png": "image/png",
"jpg": "image/jpg",
"webp": "image/webp",
"html": "text/html",
"asc": "text/plain",
"pub": "text/plain",
"js": "application/javascript",
}
func IsModified(req *http.Request, last_modified time.Time) bool {
if len(req.Header["If-Modified-Since"]) == 0 || len(req.Header["If-Modified-Since"][0]) == 0 {
return true
@ -32,3 +23,37 @@ func IsModified(req *http.Request, last_modified time.Time) bool {
return false
}
type loggingResponseWriter struct {
http.ResponseWriter
code int
}
func (lw *loggingResponseWriter) WriteHeader(code int) {
lw.code = code
lw.ResponseWriter.WriteHeader(code)
}
func HTTPLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lw := loggingResponseWriter{w, http.StatusOK}
next.ServeHTTP(&lw, r)
after := time.Now()
difference := (after.Nanosecond() - start.Nanosecond()) / 1_000_000
elapsed := "<1"
if difference >= 1 {
elapsed = strconv.Itoa(difference)
}
fmt.Printf("[%s] %s %s - %d (%sms) (%s)\n",
after.Format(time.UnixDate),
r.Method,
r.URL.Path,
lw.code,
elapsed,
r.Header["User-Agent"][0])
})
}