add log middleware (just for testing)
This commit is contained in:
+9
-8
@@ -1,9 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
@@ -11,15 +11,16 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
connectDB()
|
connectDB()
|
||||||
|
|
||||||
r := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
|
|
||||||
r.Use(authMiddleware)
|
router.Use(logMiddleware)
|
||||||
|
router.Use(authMiddleware)
|
||||||
|
|
||||||
// routes
|
// routes
|
||||||
r.Get("/quotes", getQuotes)
|
router.Get("/quotes", getQuotes)
|
||||||
r.Post("/quotes", createQuote)
|
router.Post("/quotes", createQuote)
|
||||||
r.Put("/quotes/{id}", updateQuote)
|
router.Put("/quotes/{id}", updateQuote)
|
||||||
r.Delete("/quotes/{id}", deleteQuote)
|
router.Delete("/quotes/{id}", deleteQuote)
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@@ -27,5 +28,5 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("started and listening on localhost:" + port)
|
fmt.Println("started and listening on localhost:" + port)
|
||||||
http.ListenAndServe(":"+port, r)
|
http.ListenAndServe(":"+port, router)
|
||||||
}
|
}
|
||||||
+36
-1
@@ -1,8 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"crypto/subtle"
|
||||||
)
|
)
|
||||||
|
|
||||||
func authMiddleware(next http.Handler) http.Handler {
|
func authMiddleware(next http.Handler) http.Handler {
|
||||||
@@ -18,7 +24,7 @@ func authMiddleware(next http.Handler) http.Handler {
|
|||||||
|
|
||||||
expectedHeader := "Bearer " + expectedKey
|
expectedHeader := "Bearer " + expectedKey
|
||||||
|
|
||||||
if authHeader != expectedHeader {
|
if subtle.ConstantTimeCompare([]byte(authHeader), []byte(expectedHeader)) != 1 {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -26,3 +32,32 @@ func authMiddleware(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
reqMethod := r.Method
|
||||||
|
reqUrl := r.URL.String()
|
||||||
|
reqIp := r.RemoteAddr
|
||||||
|
reqHeaderContentType := r.Header.Get("Content-Type")
|
||||||
|
defer r.Body.Close()
|
||||||
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "[log] Failed to read body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n[%s] %s request on %s\n", reqIp, reqMethod, reqUrl)
|
||||||
|
fmt.Println("Timestamp (UTC): " + time.Now().UTC().String())
|
||||||
|
fmt.Println("Header Content-Type: " + reqHeaderContentType)
|
||||||
|
if len(bodyBytes) == 0 {
|
||||||
|
fmt.Println("Body: <empty>")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Body: " + string(bodyBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore the request body for later
|
||||||
|
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user