Skip to content

Commit

Permalink
sync w/ frafos_mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
burgesQ committed Aug 28, 2023
2 parents 94b1e26 + 7bbb364 commit 665559f
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- fasthttp

jobs:
build:

Expand Down
2 changes: 1 addition & 1 deletion .gitlab-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ workflow:
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_TAG

image: docker.frafos.net/go-ci:1.19
image: docker.frafos.net/go-ci:1.21

stages:
- test
Expand Down
17 changes: 12 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ linters-settings:
cyclop:
max-complexity: 15
# package-average: 0.0
depguard:
rules:
ioutil:
deny:
- pkg: "io/ioutl"
desc: use io or os instead
logging:
deny:
- pkg: "log"
desc: not allowed use log/slog instead
funlen:
lines: 90
statements: 50
Expand All @@ -24,9 +34,6 @@ linters-settings:
- shadow
misspell:
locale: US
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
revive:
ignore-generated-header: true
severity: warning
Expand All @@ -41,7 +48,7 @@ linters-settings:
- c webfmwk.Context
- t testing.T
- e error

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
Expand All @@ -55,7 +62,7 @@ linters:
- contextcheck
- cyclop
- decorder
- depguard
# - depguard
- dogsled
- dupl
- dupword
Expand Down
18 changes: 16 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantipush c Versioning](https://semver.org/spec/v2.0.0.html).

## [5.3.1] (upcoming)
## [6.0.1] (upcoming)

### Added
### Channged
### Changed
### Fixed
### Removed

## [6.0.0] (Mon Aug 28 17:27:46 2023)

### Added
- http2 support
- slog support

### Changed
- bumped toolchain to go 1.21

### Removed
- old logger interface and implementation

### Fixed

## [5.3.0] (Wed Jul 5 13:3136 2023)

### Added
Expand Down
20 changes: 10 additions & 10 deletions TODOs.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
# to archive to file: C-c C-x C-a
# open archive sibling: C-c C-tab

* TODO v5 [6/11] [54%]
DEADLINE: <2023-06-11 Sun>
* TODO v5 [9/11] [81%]
DEADLINE: <2023-07-28 Fri>

** DONE [#A] http2 [2/2] [100%]
CLOSED: [2023-07-07 Fri 14:30] DEADLINE: <2023-07-07 Fri>
Expand All @@ -17,16 +17,16 @@ CLOSED: [2023-07-06 Thu 13:21]
*** DONE stream payload
CLOSED: [2023-07-07 Fri 14:30]

** IDEA read header for translator
DEADLINE: <2024-05-11 Sat>
** CANCELLED read header for translator
CLOSED: [2023-07-24 Mon 11:06] DEADLINE: <2024-05-11 Sat>
** TODO update the doc
DEADLINE: <2023-05-18 Thu>
** TODO run some fuzzing
DEADLINE: <2023-05-13 Sat>
** TODO run some benchmark
DEADLINE: <2023-05-13 Sat>
DEADLINE: <2023-07-28 Fri>
** CANCELLED run some fuzzing
CLOSED: [2023-07-24 Mon 11:06] DEADLINE: <2023-05-13 Sat>
** CANCELLED run some benchmark
CLOSED: [2023-07-24 Mon 11:07] DEADLINE: <2023-05-13 Sat>
** WIP coverage [1/9]
DEADLINE: <2023-05-12 Fri>
DEADLINE: <2023-07-28 Fri>
- [ ] test CORS
- [ ] test DumpRoutes
- [ ] test ctrl+c
Expand Down
34 changes: 17 additions & 17 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"errors"
"log/slog"
"net/http"

"github.com/burgesQ/log"
validator "github.com/go-playground/validator/v10"
"github.com/gorilla/schema"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -37,11 +37,11 @@ type (

// ContextLogger interface implement the context Logger needs.
ContextLogger interface {
// SetLogger set the logger of the ctx.
SetLogger(logger log.Log) Context
// SetStructuredLogger set context' structured logger.
SetStructuredLogger(logger *slog.Logger) Context

// GetLogger return the logger of the ctx.
GetLogger() log.Log
// GetStructuredLogger return context' structured logger.
GetStructuredLogger() *slog.Logger
}

// Context interface implement the context used in this project.
Expand Down Expand Up @@ -70,8 +70,8 @@ type (
// It hold the data used by the request
icontext struct {
*fasthttp.RequestCtx
log log.Log
ctx context.Context //nolint:containedctx
slog *slog.Logger
ctx context.Context //nolint:containedctx
}
)

Expand All @@ -91,26 +91,26 @@ func (c *icontext) GetVar(key string) string {
return v
}

// GetQuery implement Context
// GetQuery implement Context.
func (c *icontext) GetQuery() *fasthttp.Args {
return c.QueryArgs()
}

// GetFastContext implement Context
// GetFastContext implement Context.
func (c *icontext) GetFastContext() *fasthttp.RequestCtx {
return c.RequestCtx
}

// SetLogger implement Context
func (c *icontext) SetLogger(logger log.Log) Context {
c.log = logger
// SetStructuredLogger implement Context.
func (c *icontext) SetStructuredLogger(logger *slog.Logger) Context {
c.slog = logger

return c
}

// GetLogger implement Context
func (c *icontext) GetLogger() log.Log {
return c.log
func (c *icontext) GetStructuredLogger() *slog.Logger {
return c.slog
}

// GetContext implement Context
Expand All @@ -124,7 +124,7 @@ func (c *icontext) FetchContent(dest interface{}) ErrorHandled {
b := c.PostBody()

if e := json.Unmarshal(b, &dest); e != nil {
c.log.Errorf("fetching payload: %s", e.Error())
c.slog.Error("fetching payload", "error", e)

return errUnprocessablePayload
}
Expand All @@ -136,7 +136,7 @@ func (c *icontext) FetchContent(dest interface{}) ErrorHandled {
// this implemtation use validator to anotate & check struct
func (c *icontext) Validate(dest interface{}) ErrorHandled {
if e := validate.Struct(dest); e != nil {
c.log.Errorf("validating : %s", e.Error())
c.slog.Error("validating form or query param", "error", e)

var ev validator.ValidationErrors

Expand Down Expand Up @@ -172,7 +172,7 @@ func (c *icontext) DecodeQP(dest interface{}) ErrorHandled {
})

if e := decoder.Decode(dest, m); e != nil {
c.log.Errorf("validating qp : %s", e.Error())
c.slog.Error("validating query params", "error", e)

return NewUnprocessable(NewErrorFromError(e))
}
Expand Down
10 changes: 4 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package webfmwk

import (
"context"
"log/slog"
"net/http"
"testing"

"github.com/burgesQ/gommon/webtest"
"github.com/burgesQ/webfmwk/v6/log"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,8 +16,6 @@ var (
)

func TestParam(t *testing.T) {
log.SetLogLevel(log.LogDebug)

wrapperGet(t, "/test/{id}", "/test/tutu", func(c Context) error {
id := c.GetVar("id")
if id != "tutu" {
Expand All @@ -35,11 +33,11 @@ func TestParam(t *testing.T) {
func TestLogger(t *testing.T) {
var (
c = icontext{}
logger = log.GetLogger()
logger = slog.Default()
)

c.SetLogger(logger)
assert.True(t, logger == c.GetLogger(), "context logger should be the setted one")
c.SetStructuredLogger(logger)
assert.True(t, logger == c.GetStructuredLogger(), "context logger should be the setted one")
// assert.True(t, logger == GetLogger(), "default logger should be the setted one")
}

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/burgesQ/webfmwk/v6

go 1.19
go 1.21

require (
github.com/burgesQ/gommon v1.2.4
github.com/burgesQ/log v0.0.0-20230331103157-ecfef450adac
github.com/dgrr/http2 v0.3.5
github.com/fasthttp/router v1.4.19
github.com/go-playground/locales v0.14.1
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/burgesQ/gommon v1.2.4 h1:KpPzDjtaSZ20Tas75siJNASglDINXG0N20HoGKhsTbY=
github.com/burgesQ/gommon v1.2.4/go.mod h1:JXuiXVcuwJ/gxtjb9Lnpp0zDIdCDASVW0uv6cWy0COs=
github.com/burgesQ/log v0.0.0-20230331103157-ecfef450adac h1:Fz/u0vMnu9tUhlydXudpmdg57UgBroPwa+FhKgGKFbk=
github.com/burgesQ/log v0.0.0-20230331103157-ecfef450adac/go.mod h1:0mqbA+jgzeuo2IcjkgOHi/NKCmjWVrkLS/7DEmAkCng=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -16,6 +14,7 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
Expand Down
11 changes: 6 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package webfmwk

import (
"bytes"
"log/slog"

"github.com/segmentio/encoding/json"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -40,7 +41,7 @@ func GetIPFromRequest(fc *fasthttp.RequestCtx) string {
func handleHandlerError(next HandlerFunc) HandlerFunc {
return HandlerFunc(func(c Context) error {
if e := next(c); e != nil {
c.GetLogger().Errorf("catched from handler (%T) : %s", e, e.Error())
c.GetStructuredLogger().Error("catch'd from handler", "error", e)
HandleError(c, e)
}

Expand Down Expand Up @@ -71,17 +72,17 @@ func contentIsJSON(next HandlerFunc) HandlerFunc {
func handleNotFound(c Context) error {
fc := c.GetFastContext()

c.GetLogger().Infof("[!] 404 reached for [%s] %s %s",
GetIPFromRequest(fc), fc.Method(), fc.RequestURI())
c.GetStructuredLogger().Info("[!] 404 reached", slog.Group("request",
"ip", GetIPFromRequest(fc), "method", fc.Method(), "uri", fc.RequestURI()))

return c.JSONNotFound(json.RawMessage(`{"status":404,"message":"not found"}`))
}

func handleNotAllowed(c Context) error {
fc := c.GetFastContext()

c.GetLogger().Infof("[!] 405 reached for [%s] %s %s",
GetIPFromRequest(fc), fc.Method(), fc.RequestURI())
c.GetStructuredLogger().Info("[!] 405 reached", slog.Group("request",
"ip", GetIPFromRequest(fc), "method", fc.Method(), "uri", fc.RequestURI()))

return c.JSONMethodNotAllowed(json.RawMessage(`{"status":405,"message":"method not allowed"}`))
}
2 changes: 1 addition & 1 deletion handler/recover/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Handler(next webfmwk.HandlerFunc) webfmwk.HandlerFunc {
webfmwk.HandleError(c, e)

default:
c.GetLogger().Errorf("catched %T %#v", e, e)
c.GetStructuredLogger().Error("catched exit", "error", e)
_ = c.JSONInternalError(webfmwk.NewError(
fmt.Sprintf("internal error: %T %v", e, e)))
}
Expand Down
21 changes: 14 additions & 7 deletions handler/logging/logging.go → handler/slogging/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logging
package slogging

import (
"log/slog"
"strconv"
"time"
"unicode/utf8"
Expand Down Expand Up @@ -30,10 +31,13 @@ func Handler(next webfmwk.HandlerFunc) webfmwk.HandlerFunc {
}
c.SetHeader(HeaderRequestID, rid)

lg := c.GetLogger().SetPrefix("[" + rid + "]: ")
lg := c.GetStructuredLogger().With("request id", rid, slog.Group("request",
"ip", webfmwk.GetIPFromRequest(fc),
"method", fc.Method(),
"uri", fc.RequestURI()))
c.SetStructuredLogger(lg)

c.SetLogger(lg)
lg.Infof("--> %q [%s]%s ", webfmwk.GetIPFromRequest(fc), fc.Method(), fc.RequestURI())
lg.Info("--> new request")

e := next(c)
elapsed := time.Since(start)
Expand All @@ -42,13 +46,16 @@ func Handler(next webfmwk.HandlerFunc) webfmwk.HandlerFunc {

if utf8.Valid(content) {
if l > _limitOutput {
lg.Debugf(">%s<", content[:_limitOutput])
lg.Debug("trunkated response", "body",
content[:_limitOutput],
"lim", _limitOutput)
} else {
lg.Debugf(">%s<", content)
lg.Debug("full response", "body", content)
}
}

lg.Infof("<-- [%d]: took %s", fc.Response.Header.StatusCode(), elapsed)
lg.Info("<-- request done", "code", fc.Response.Header.StatusCode(),
"elapsed", elapsed)

return e
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package logging
package slogging

import (
"encoding/json"
Expand Down
Loading

0 comments on commit 665559f

Please sign in to comment.