-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
153 lines (118 loc) · 3.13 KB
/
server.go
File metadata and controls
153 lines (118 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package echoext
import (
"net/http"
"os"
"strings"
"github.com/labstack/gommon/color"
echoSwagger "github.com/swaggo/echo-swagger"
validator "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
emiddleware "github.com/labstack/echo/v4/middleware"
)
type EchoMode string
const (
TestMode EchoMode = "test"
StandardMode EchoMode = "standard"
)
type Server interface {
Start() error
Group(string, setupfn, ...MiddlewareFunc) *Group
Engine() *echo.Echo
}
type Mountable interface {
Mount(*echo.Group)
}
type extServer struct {
*echo.Echo
config ServerConfig
colorer *color.Color
appEnv string
root *Group
mode string
}
func New(cl ...ServerConfig) Server {
c := ServerConfig{}
if len(cl) > 0 {
c = cl[0]
}
s := echo.New()
s.HideBanner = true
s.Validator = &Validator{
v: validator.New(
validator.WithRequiredStructEnabled(),
),
}
c.HealthcheckPath = c.escapeHealthcheckSuffix()
c.PathPrefix = c.escapePrefix()
s.Use(CustomLogger(c))
s.Use(CustomRecovery)
s.Use(CustomCORS(c))
root := s.Group(c.PathPrefix)
root.GET(c.escapeHealthcheckSuffix(), func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, echo.Map{"status": "ok"})
})
colorer := color.New()
env := os.Getenv("APP_ENV")
if env == "" {
env = "local"
}
colorer.Printf("[%s] app enviroment: %s\n", colorer.Green("echoext"), colorer.Blue(env))
colorer.Printf("[%s] server prefix: %s\n", colorer.Green("echoext"), colorer.Blue(c.PathPrefix))
colorer.Printf("[%s] healthcheck path: %s\n", colorer.Green("echoext"), colorer.Blue(c.healthcheckFullPath()))
if env != "production" {
sp := c.swaggerPath()
colorer.Printf("[%s] swagger docs: %s\n", colorer.Green("echoext"), colorer.Blue("http://"+c.escapeHost()+sp+"/index.html"))
swaggerCreds := os.Getenv("SWAGGER_CREDENTIALS")
swaggerUser := ""
swaggerPass := ""
parts := strings.Split(swaggerCreds, ":")
if len(parts) == 2 {
swaggerUser = parts[0]
swaggerPass = parts[1]
}
swaggerAuth := emiddleware.BasicAuthWithConfig(emiddleware.BasicAuthConfig{
Skipper: nil,
Validator: func(u string, p string, ctx echo.Context) (bool, error) {
return u == swaggerUser && p == swaggerPass, nil
},
Realm: "",
})
s.GET(sp+"/*", echoSwagger.EchoWrapHandler(), swaggerAuth)
}
colorer.Println()
return extServer{
Echo: s,
config: c,
colorer: colorer,
appEnv: env,
root: &Group{root},
}
}
type setupfn func(*Group)
func escapePath(path string) string {
p := strings.TrimSpace(path)
// empty case
if p == "" {
p = "/"
} else if p[0] != '/' { // ensure leading /
p = "/" + p
}
// remove trailing /
if len(p) > 0 && p[len(p)-1] == '/' {
p = p[:len(p)-1]
}
return strings.ToLower(p)
}
func (s extServer) Group(prefix string, mount setupfn, middlewares ...MiddlewareFunc) *Group {
p := escapePath(prefix)
s.colorer.Printf("[%s] group prefix: %s\n", s.colorer.Green("echoext"), s.colorer.Blue(s.config.PathPrefix+p))
g := s.root.NewGroup(p, middlewares...)
mount(g)
return g
}
func (s extServer) Start() error {
return s.Echo.Start(s.config.escapeHost())
}
func (s extServer) Engine() *echo.Echo {
return s.Echo
}