Skip to content

Commit 02e04b4

Browse files
author
Motalleb Fallahnezhad
committed
switched webserver from gin to echo
1 parent eb264de commit 02e04b4

File tree

5 files changed

+68
-98
lines changed

5 files changed

+68
-98
lines changed

config.local.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
jobs:
44
- name: echo
55
tasks:
6-
- command: echo {{ .Vars.name }}; sleep 5sec
6+
- command: echo {{ .Vars.name }}; sleep 5
77
vars:
88
name: '{{ now | date "2006-01-02_15-04-05" }}'
99
env:

core/webserver/endpoint/event_dispatch.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"net/http"
77

8-
"github.com/gin-gonic/gin"
8+
"github.com/labstack/echo/v4"
99
"github.com/prometheus/client_golang/prometheus"
1010

1111
"github.com/fmotalleb/crontab-go/core/global"
@@ -17,21 +17,20 @@ func NewEventDispatchEndpoint() *EventDispatchEndpoint {
1717
return &EventDispatchEndpoint{}
1818
}
1919

20-
func (ed *EventDispatchEndpoint) Endpoint(c *gin.Context) {
20+
func (ed *EventDispatchEndpoint) Endpoint(c echo.Context) error {
2121
event := c.Param("event")
2222

2323
metaData := make(map[string]any)
24-
for key, values := range c.Request.URL.Query() {
24+
for key, values := range c.Request().URL.Query() {
2525
metaData[key] = values
2626
}
2727

2828
listeners := global.CTX().EventListeners()[event]
2929
if len(listeners) == 0 {
30-
c.String(http.StatusNotFound, fmt.Sprintf("event: '%s' not found", event))
31-
return
30+
return c.String(http.StatusNotFound, fmt.Sprintf("event: '%s' not found", event))
3231
}
3332
global.CTX().MetricCounter(
34-
c,
33+
c.Request().Context(),
3534
"webserver_events",
3635
"amount of events dispatched using webserver",
3736
prometheus.Labels{"event_name": event},
@@ -41,13 +40,13 @@ func (ed *EventDispatchEndpoint) Endpoint(c *gin.Context) {
4140
},
4241
)
4342
listenerCount := len(listeners)
44-
global.CTX().MetricCounter(c, "webserver_event_listeners_invoked", "amount of listeners invoked by `event_name` event", prometheus.Labels{"event_name": event}).Operate(
43+
global.CTX().MetricCounter(c.Request().Context(), "webserver_event_listeners_invoked", "amount of listeners invoked by `event_name` event", prometheus.Labels{"event_name": event}).Operate(
4544
func(f float64) float64 {
4645
return f + float64(listenerCount)
4746
},
4847
)
4948
for _, listener := range listeners {
5049
go listener(metaData)
5150
}
52-
c.String(http.StatusOK, fmt.Sprintf("event: '%s' emitted, %d listeners where found", event, listenerCount))
51+
return c.String(http.StatusOK, fmt.Sprintf("event: '%s' emitted, %d listeners where found", event, listenerCount))
5352
}

core/webserver/webserver.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"net/http"
88

99
"github.com/fmotalleb/go-tools/log"
10-
"github.com/gin-gonic/gin"
10+
"github.com/labstack/echo/v4"
11+
"github.com/labstack/echo/v4/middleware"
1112
"github.com/prometheus/client_golang/prometheus/promhttp"
1213
"go.uber.org/zap"
1314

@@ -46,26 +47,53 @@ func NewWebServer(ctx context.Context,
4647
}
4748

4849
func (s *WebServer) Serve() {
49-
engine := gin.New()
50-
auth := func(*gin.Context) {}
50+
engine := echo.New()
51+
auth := func(next echo.HandlerFunc) echo.HandlerFunc {
52+
return next
53+
}
5154
if s.AuthConfig != nil && s.AuthConfig.Username != "" && s.AuthConfig.Password != "" {
52-
auth = gin.BasicAuth(gin.Accounts{s.AuthConfig.Username: s.AuthConfig.Password})
55+
auth = middleware.BasicAuth(func(username, password string, _ echo.Context) (bool, error) {
56+
if username == s.AuthConfig.Username && password == s.AuthConfig.Password {
57+
return true, nil
58+
}
59+
return false, nil
60+
})
5361
} else {
5462
s.log.Warn("received no value on username or password, ignoring any authentication, if you intended to use no authentication ignore this message")
5563
}
56-
// log := gin.LoggerWithConfig(gin.LoggerConfig{
57-
// Formatter: gin.format,
58-
// })
64+
5965
engine.Use(
6066
auth,
61-
// log,
62-
gin.Recovery(),
67+
middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
68+
LogURI: true,
69+
LogStatus: true,
70+
LogError: true,
71+
HandleError: true, // forwards error to the global error handler, so it can decide appropriate status code
72+
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
73+
if v.Error == nil {
74+
s.log.Debug(
75+
"request served",
76+
zap.String("URI", v.URI),
77+
zap.Int("status", v.Status),
78+
)
79+
} else {
80+
s.log.Warn(
81+
"request failed",
82+
zap.String("URI", v.URI),
83+
zap.Int("status", v.Status),
84+
zap.Error(v.Error),
85+
)
86+
}
87+
return nil
88+
},
89+
}),
90+
middleware.Recover(),
6391
)
6492

6593
engine.GET(
6694
"/foo",
67-
func(c *gin.Context) {
68-
c.String(200, "bar")
95+
func(c echo.Context) error {
96+
return c.String(200, "bar")
6997
},
7098
)
7199

@@ -75,16 +103,17 @@ func (s *WebServer) Serve() {
75103
ed.Endpoint,
76104
)
77105
if s.serveMetrics {
78-
engine.GET("/metrics", func(ctx *gin.Context) {
79-
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
106+
engine.GET("/metrics", func(c echo.Context) error {
107+
promhttp.Handler().ServeHTTP(c.Response().Writer, c.Request())
108+
return nil
80109
})
81110
} else {
82-
engine.GET("/metrics", func(ctx *gin.Context) {
83-
ctx.String(http.StatusNotFound, "Metrics are disabled, please enable metrics using `WEBSERVER_METRICS=true`")
111+
engine.GET("/metrics", func(c echo.Context) error {
112+
return c.String(http.StatusNotFound, "Metrics are disabled, please enable metrics using `WEBSERVER_METRICS=true`")
84113
})
85114
}
86115

87-
err := engine.Run(fmt.Sprintf("%s:%d", s.address, s.port))
116+
err := engine.Start(fmt.Sprintf("%s:%d", s.address, s.port))
88117
helpers.FatalOnErr(
89118
s.log,
90119
func() error {

go.mod

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require (
1212
github.com/alecthomas/assert/v2 v2.11.0
1313
github.com/docker/docker v28.5.2+incompatible
1414
github.com/fmotalleb/go-tools v0.1.55
15-
github.com/gin-gonic/gin v1.11.0
1615
github.com/joho/godotenv v1.5.1
16+
github.com/labstack/echo/v4 v4.13.4
1717
github.com/prometheus/client_golang v1.23.2
1818
github.com/robfig/cron/v3 v3.0.1
1919
github.com/spf13/cobra v1.10.1
@@ -56,16 +56,13 @@ require (
5656
github.com/breml/errchkjson v0.4.0 // indirect
5757
github.com/butuzov/ireturn v0.3.1 // indirect
5858
github.com/butuzov/mirror v1.3.0 // indirect
59-
github.com/bytedance/sonic v1.14.0 // indirect
60-
github.com/bytedance/sonic/loader v0.3.0 // indirect
6159
github.com/catenacyber/perfsprint v0.8.2 // indirect
6260
github.com/ccojocar/zxcvbn-go v1.0.2 // indirect
6361
github.com/cespare/xxhash/v2 v2.3.0 // indirect
6462
github.com/charithe/durationcheck v0.0.10 // indirect
6563
github.com/chavacava/garif v0.1.0 // indirect
6664
github.com/ckaznocha/intrange v0.3.0 // indirect
6765
github.com/client9/misspell v0.3.4 // indirect
68-
github.com/cloudwego/base64x v0.1.6 // indirect
6966
github.com/containerd/errdefs v1.0.0 // indirect
7067
github.com/containerd/errdefs/pkg v0.3.0 // indirect
7168
github.com/containerd/log v0.1.0 // indirect
@@ -83,15 +80,10 @@ require (
8380
github.com/firefart/nonamedreturns v1.0.5 // indirect
8481
github.com/fsnotify/fsnotify v1.9.0 // indirect
8582
github.com/fzipp/gocyclo v0.6.0 // indirect
86-
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
8783
github.com/ghostiam/protogetter v0.3.9 // indirect
88-
github.com/gin-contrib/sse v1.1.0 // indirect
8984
github.com/go-critic/go-critic v0.12.0 // indirect
9085
github.com/go-logr/logr v1.4.3 // indirect
9186
github.com/go-logr/stdr v1.2.2 // indirect
92-
github.com/go-playground/locales v0.14.1 // indirect
93-
github.com/go-playground/universal-translator v0.18.1 // indirect
94-
github.com/go-playground/validator/v10 v10.27.0 // indirect
9587
github.com/go-toolsmith/astcast v1.1.0 // indirect
9688
github.com/go-toolsmith/astcopy v1.1.0 // indirect
9789
github.com/go-toolsmith/astequal v1.2.0 // indirect
@@ -102,8 +94,6 @@ require (
10294
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
10395
github.com/go-xmlfmt/xmlfmt v1.1.3 // indirect
10496
github.com/gobwas/glob v0.2.3 // indirect
105-
github.com/goccy/go-json v0.10.2 // indirect
106-
github.com/goccy/go-yaml v1.18.0 // indirect
10797
github.com/gofrs/flock v0.12.1 // indirect
10898
github.com/golangci/dupl v0.0.0-20250308024227-f665c8d69b32 // indirect
10999
github.com/golangci/go-printf-func-name v0.1.0 // indirect
@@ -129,21 +119,19 @@ require (
129119
github.com/jgautheron/goconst v1.7.1 // indirect
130120
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
131121
github.com/jjti/go-spancheck v0.6.4 // indirect
132-
github.com/json-iterator/go v1.1.12 // indirect
133122
github.com/julz/importas v0.2.0 // indirect
134123
github.com/karamaru-alpha/copyloopvar v1.2.1 // indirect
135124
github.com/kisielk/errcheck v1.9.0 // indirect
136125
github.com/kkHAIKE/contextcheck v1.1.6 // indirect
137-
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
138126
github.com/kulti/thelper v0.6.3 // indirect
139127
github.com/kunwardeep/paralleltest v1.0.10 // indirect
128+
github.com/labstack/gommon v0.4.2 // indirect
140129
github.com/lasiar/canonicalheader v1.1.2 // indirect
141130
github.com/ldez/exptostd v0.4.2 // indirect
142131
github.com/ldez/gomoddirectives v0.6.1 // indirect
143132
github.com/ldez/grignotin v0.9.0 // indirect
144133
github.com/ldez/tagliatelle v0.7.1 // indirect
145134
github.com/ldez/usetesting v0.4.2 // indirect
146-
github.com/leodido/go-urn v1.4.0 // indirect
147135
github.com/leonklingele/grouper v1.1.2 // indirect
148136
github.com/macabu/inamedparam v0.1.3 // indirect
149137
github.com/maratori/testableexamples v1.0.0 // indirect
@@ -159,8 +147,6 @@ require (
159147
github.com/moby/docker-image-spec v1.3.1 // indirect
160148
github.com/moby/sys/atomicwriter v0.1.0 // indirect
161149
github.com/moby/term v0.5.2 // indirect
162-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
163-
github.com/modern-go/reflect2 v1.0.2 // indirect
164150
github.com/moricho/tparallel v0.3.2 // indirect
165151
github.com/morikuni/aec v1.0.0 // indirect
166152
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
@@ -183,8 +169,6 @@ require (
183169
github.com/quasilyte/gogrep v0.5.0 // indirect
184170
github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727 // indirect
185171
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
186-
github.com/quic-go/qpack v0.5.1 // indirect
187-
github.com/quic-go/quic-go v0.54.0 // indirect
188172
github.com/raeperd/recvcheck v0.2.0 // indirect
189173
github.com/rivo/uniseg v0.4.7 // indirect
190174
github.com/rogpeppe/go-internal v1.14.1 // indirect
@@ -217,12 +201,12 @@ require (
217201
github.com/timonwong/loggercheck v0.10.1 // indirect
218202
github.com/tomarrell/wrapcheck/v2 v2.10.0 // indirect
219203
github.com/tommy-muehle/go-mnd/v2 v2.5.1 // indirect
220-
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
221-
github.com/ugorji/go/codec v1.3.0 // indirect
222204
github.com/ultraware/funlen v0.2.0 // indirect
223205
github.com/ultraware/whitespace v0.2.0 // indirect
224206
github.com/uudashr/gocognit v1.2.0 // indirect
225207
github.com/uudashr/iface v1.3.1 // indirect
208+
github.com/valyala/bytebufferpool v1.0.0 // indirect
209+
github.com/valyala/fasttemplate v1.2.2 // indirect
226210
github.com/xen0n/gosmopolitan v1.2.2 // indirect
227211
github.com/yagipy/maintidx v1.0.0 // indirect
228212
github.com/yeya24/promlinter v0.3.0 // indirect
@@ -238,11 +222,9 @@ require (
238222
go.opentelemetry.io/otel/sdk v1.38.0 // indirect
239223
go.opentelemetry.io/otel/trace v1.38.0 // indirect
240224
go.uber.org/automaxprocs v1.6.0 // indirect
241-
go.uber.org/mock v0.5.0 // indirect
242225
go.uber.org/multierr v1.10.0 // indirect
243226
go.yaml.in/yaml/v2 v2.4.2 // indirect
244227
go.yaml.in/yaml/v3 v3.0.4 // indirect
245-
golang.org/x/arch v0.20.0 // indirect
246228
golang.org/x/crypto v0.41.0 // indirect
247229
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
248230
golang.org/x/mod v0.26.0 // indirect
@@ -251,6 +233,7 @@ require (
251233
golang.org/x/sys v0.35.0 // indirect
252234
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b // indirect
253235
golang.org/x/text v0.28.0 // indirect
236+
golang.org/x/time v0.11.0 // indirect
254237
golang.org/x/tools v0.35.0 // indirect
255238
golang.org/x/tools/go/expect v0.1.1-deprecated // indirect
256239
golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated // indirect

0 commit comments

Comments
 (0)