we are so back 🎉

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-07-31 19:17:58 +01:00
parent a26bdfa646
commit c684f0c7ae
2 changed files with 109 additions and 55 deletions

View file

@ -7,6 +7,18 @@ import (
"time"
)
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",
"txt": "text/plain",
"js": "application/javascript",
}
var LAST_MODIFIED = time.Now()
func IsModified(req *http.Request, last_modified time.Time) bool {
@ -23,23 +35,23 @@ func IsModified(req *http.Request, last_modified time.Time) bool {
return false
}
type loggingResponseWriter struct {
type LoggingResponseWriter struct {
http.ResponseWriter
code int
Code int
}
func (lw *loggingResponseWriter) WriteHeader(code int) {
lw.code = code
lw.ResponseWriter.WriteHeader(code)
func (lrw *LoggingResponseWriter) WriteHeader(code int) {
lrw.Code = code
lrw.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}
lrw := LoggingResponseWriter{w, http.StatusOK}
next.ServeHTTP(&lw, r)
next.ServeHTTP(&lrw, r)
after := time.Now()
difference := (after.Nanosecond() - start.Nanosecond()) / 1_000_000
@ -52,7 +64,7 @@ func HTTPLog(next http.Handler) http.Handler {
after.Format(time.UnixDate),
r.Method,
r.URL.Path,
lw.code,
lrw.Code,
elapsed,
r.Header["User-Agent"][0])
})