refactor: reorganize project structure following go standards

This commit is contained in:
2026-04-20 15:54:35 +02:00
parent 055ec1fca9
commit 6df8788749
70 changed files with 43 additions and 187 deletions

76
pkg/middleware/logging.go Normal file
View File

@@ -0,0 +1,76 @@
package middleware
import (
"bufio"
"fmt"
"log"
"net"
"net/http"
"time"
)
type statusRecorder struct {
http.ResponseWriter
statusCode int
wroteHeader bool
}
func newStatusRecorder(w http.ResponseWriter) *statusRecorder {
return &statusRecorder{
ResponseWriter: w,
statusCode: http.StatusOK,
}
}
func (rw *statusRecorder) WriteHeader(code int) {
if rw.wroteHeader {
return
}
rw.statusCode = code
rw.wroteHeader = true
rw.ResponseWriter.WriteHeader(code)
}
func (rw *statusRecorder) Write(b []byte) (int, error) {
if !rw.wroteHeader {
rw.WriteHeader(http.StatusOK)
}
return rw.ResponseWriter.Write(b)
}
func (rw *statusRecorder) Flush() {
if flusher, ok := rw.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func (rw *statusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := rw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("response writer does not support hijacking")
}
return hijacker.Hijack()
}
func (rw *statusRecorder) Push(target string, opts *http.PushOptions) error {
pusher, ok := rw.ResponseWriter.(http.Pusher)
if !ok {
return http.ErrNotSupported
}
return pusher.Push(target, opts)
}
func (rw *statusRecorder) Unwrap() http.ResponseWriter {
return rw.ResponseWriter
}
func RequestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
recorder := newStatusRecorder(w)
next.ServeHTTP(recorder, r)
log.Printf("%s %s %d %s", r.Method, r.URL.Path, recorder.statusCode, time.Since(start))
})
}