2024-07-31 04:09:22 +01:00
|
|
|
package global
|
|
|
|
|
|
|
|
import (
|
2024-07-31 13:45:34 +01:00
|
|
|
"fmt"
|
2024-07-31 04:09:22 +01:00
|
|
|
"net/http"
|
2024-07-31 13:45:34 +01:00
|
|
|
"strconv"
|
2024-07-31 04:09:22 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-07-31 19:17:58 +01:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2024-07-31 04:09:22 +01:00
|
|
|
var LAST_MODIFIED = time.Now()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
request_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0])
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if request_time.Before(last_modified) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-31 19:17:58 +01:00
|
|
|
type LoggingResponseWriter struct {
|
2024-07-31 13:45:34 +01:00
|
|
|
http.ResponseWriter
|
2024-07-31 19:17:58 +01:00
|
|
|
Code int
|
2024-07-31 13:45:34 +01:00
|
|
|
}
|
|
|
|
|
2024-07-31 19:17:58 +01:00
|
|
|
func (lrw *LoggingResponseWriter) WriteHeader(code int) {
|
|
|
|
lrw.Code = code
|
|
|
|
lrw.ResponseWriter.WriteHeader(code)
|
2024-07-31 13:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func HTTPLog(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
start := time.Now()
|
|
|
|
|
2024-07-31 19:17:58 +01:00
|
|
|
lrw := LoggingResponseWriter{w, http.StatusOK}
|
2024-07-31 13:45:34 +01:00
|
|
|
|
2024-07-31 19:17:58 +01:00
|
|
|
next.ServeHTTP(&lrw, r)
|
2024-07-31 13:45:34 +01:00
|
|
|
|
|
|
|
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,
|
2024-07-31 19:17:58 +01:00
|
|
|
lrw.Code,
|
2024-07-31 13:45:34 +01:00
|
|
|
elapsed,
|
|
|
|
r.Header["User-Agent"][0])
|
|
|
|
})
|
|
|
|
}
|