installed logger
This commit is contained in:
@@ -59,12 +59,12 @@ func (b bodyRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) getStatus() (int, bool) {
|
||||
func (b bodyRes) GetStatus() (int, bool) {
|
||||
if b.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return b.res.getStatus()
|
||||
return b.res.GetStatus()
|
||||
}
|
||||
|
||||
func (b bodyRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
|
||||
@@ -58,12 +58,12 @@ func (c cookieRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(c)
|
||||
}
|
||||
|
||||
func (c cookieRes) getStatus() (int, bool) {
|
||||
func (c cookieRes) GetStatus() (int, bool) {
|
||||
if c.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return c.res.getStatus()
|
||||
return c.res.GetStatus()
|
||||
}
|
||||
|
||||
func (c cookieRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
|
||||
@@ -60,12 +60,12 @@ func (h htmlRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(h)
|
||||
}
|
||||
|
||||
func (h htmlRes) getStatus() (int, bool) {
|
||||
func (h htmlRes) GetStatus() (int, bool) {
|
||||
if h.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return h.res.getStatus()
|
||||
return h.res.GetStatus()
|
||||
}
|
||||
|
||||
func (h htmlRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
|
||||
@@ -61,12 +61,12 @@ func (j jsonRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(j)
|
||||
}
|
||||
|
||||
func (j jsonRes) getStatus() (int, bool) {
|
||||
func (j jsonRes) GetStatus() (int, bool) {
|
||||
if j.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return j.res.getStatus()
|
||||
return j.res.GetStatus()
|
||||
}
|
||||
|
||||
func (j jsonRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
|
||||
@@ -99,12 +99,12 @@ func (r redirectRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(r)
|
||||
}
|
||||
|
||||
func (r redirectRes) getStatus() (int, bool) {
|
||||
func (r redirectRes) GetStatus() (int, bool) {
|
||||
if r.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return r.res.getStatus()
|
||||
return r.res.GetStatus()
|
||||
}
|
||||
|
||||
func (r redirectRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
|
||||
@@ -16,7 +16,7 @@ type (
|
||||
JSON(any) Response
|
||||
Cookie(http.Cookie) Response
|
||||
|
||||
getStatus() (code int, ok bool)
|
||||
GetStatus() (code int, ok bool)
|
||||
getBody() (body io.ReadCloser, ok bool, err error)
|
||||
getRedirect() (code redirect.Code, to string, ok bool)
|
||||
getCookies() []http.Cookie
|
||||
|
||||
@@ -60,7 +60,7 @@ func (s statusRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(s)
|
||||
}
|
||||
|
||||
func (s statusRes) getStatus() (int, bool) {
|
||||
func (s statusRes) GetStatus() (int, bool) {
|
||||
return s.code, true
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
|
||||
return
|
||||
}
|
||||
|
||||
if status, ok := res.getStatus(); ok {
|
||||
if status, ok := res.GetStatus(); ok {
|
||||
w.WriteHeader(status)
|
||||
}
|
||||
if bodySet {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/angelbeltran/templater"
|
||||
|
||||
@@ -36,6 +38,7 @@ type Server struct {
|
||||
}
|
||||
|
||||
func NewServer(
|
||||
ctx context.Context,
|
||||
logger *slog.Logger,
|
||||
contentDir string,
|
||||
rawEvents *raw_events.Store,
|
||||
@@ -43,14 +46,49 @@ func NewServer(
|
||||
etsy *etsy_platform.Platform,
|
||||
auth *authentication.Authenticator,
|
||||
) *Server {
|
||||
reqIDCh := newRequestIDProvider(ctx)
|
||||
|
||||
reqLog := logger.WithGroup("request")
|
||||
mux := response.NewMux(func(fn response.HandlerFunc) response.HandlerFunc {
|
||||
return func(r *http.Request) (response.Response, error) {
|
||||
res, err := fn(r)
|
||||
if err != nil {
|
||||
status := response.GetStatusFromError(err)
|
||||
start := time.Now()
|
||||
|
||||
// TODO: get better logger
|
||||
fmt.Printf("[ERROR]: %d: %s; %s\n", status, r.URL, err)
|
||||
args := []any{
|
||||
"id", <-reqIDCh,
|
||||
"url", r.URL,
|
||||
}
|
||||
|
||||
reqLog.Debug("Request", args...)
|
||||
|
||||
res, err := fn(r)
|
||||
end := time.Now()
|
||||
|
||||
var status int
|
||||
if err != nil {
|
||||
status = response.GetStatusFromError(err)
|
||||
} else {
|
||||
var ok bool
|
||||
if status, ok = res.GetStatus(); !ok {
|
||||
status = http.StatusOK
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args,
|
||||
"status", status,
|
||||
"elapsed", time.Duration(end.UnixNano()-start.UnixNano()),
|
||||
)
|
||||
|
||||
switch status / 100 {
|
||||
case 1:
|
||||
reqLog.Debug("Response", args...)
|
||||
case 2:
|
||||
reqLog.Debug("Response", args...)
|
||||
case 3:
|
||||
reqLog.Debug("Response", args...)
|
||||
case 4:
|
||||
reqLog.Warn("Response", args...)
|
||||
default:
|
||||
reqLog.Error("Response", args...)
|
||||
}
|
||||
|
||||
return res, err
|
||||
@@ -223,3 +261,24 @@ func mapConstantErrorsToHTTPErrors(err error) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func newRequestIDProvider(ctx context.Context) <-chan int {
|
||||
reqIDCh := make(chan int)
|
||||
|
||||
go func() {
|
||||
defer close(reqIDCh)
|
||||
|
||||
nextReqID := 1
|
||||
|
||||
for {
|
||||
select {
|
||||
case reqIDCh <- nextReqID:
|
||||
nextReqID += 1
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return reqIDCh
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user