41 lines
762 B
Go
41 lines
762 B
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
t := time.Now()
|
|
logRequest(t, r, 418)
|
|
w.WriteHeader(http.StatusTeapot)
|
|
fmt.Fprint(w, "Bip Boop I'm a teapot")
|
|
}
|
|
|
|
func HealthHandler(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = io.WriteString(w, "OK")
|
|
}
|
|
|
|
func NotFound(w http.ResponseWriter, r *http.Request) {
|
|
t := time.Now()
|
|
logRequest(t, r, 404)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
|
|
func logRequest(t time.Time, r *http.Request, s int) {
|
|
log.Printf("%s - - %s \"%s %s %s\" %d %d \"-\" \"%s\" %d\n",
|
|
r.Host,
|
|
t.Format("[02/Jan/2006:15:04:05 -0700]"),
|
|
r.Method,
|
|
r.URL.Path,
|
|
r.Proto,
|
|
s,
|
|
r.ContentLength,
|
|
r.UserAgent(),
|
|
time.Since(t).Milliseconds(),
|
|
)
|
|
}
|