|
1
|
|
|
package v2 |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net/http" |
|
5
|
|
|
"sync" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/labstack/echo/v4" |
|
8
|
|
|
"github.com/labstack/echo/v4/middleware" |
|
9
|
|
|
"github.com/memnix/memnix-rest/cmd/v1/config" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
var ( |
|
13
|
|
|
instance *InstanceSingleton //nolint:gochecknoglobals //Singleton |
|
14
|
|
|
once sync.Once //nolint:gochecknoglobals //Singleton |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
type InstanceSingleton struct { |
|
18
|
|
|
echoInstance *echo.Echo |
|
19
|
|
|
config ServerConfig |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// ServerConfig holds the configuration for the server. |
|
23
|
|
|
type ServerConfig struct { |
|
24
|
|
|
Port string |
|
25
|
|
|
AppVersion string |
|
26
|
|
|
JaegerURL string |
|
27
|
|
|
Host string |
|
28
|
|
|
FrontendURL string |
|
29
|
|
|
LogLevel string |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// New returns a new Echo instance. |
|
33
|
|
|
func GetEchoInstance() *echo.Echo { |
|
34
|
|
|
return instance.echoInstance |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
func GetEchoSingleton() *InstanceSingleton { |
|
38
|
|
|
once.Do(func() { |
|
39
|
|
|
instance = &InstanceSingleton{} |
|
40
|
|
|
instance.echoInstance = echo.New() |
|
41
|
|
|
instance.registerMiddlewares(instance.echoInstance) |
|
42
|
|
|
|
|
43
|
|
|
instance.registerStaticRoutes(instance.echoInstance) |
|
44
|
|
|
|
|
45
|
|
|
instance.registerRoutes(instance.echoInstance) |
|
46
|
|
|
}) |
|
47
|
|
|
return instance |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
func CreateEchoInstance(config ServerConfig) *InstanceSingleton { |
|
51
|
|
|
return GetEchoSingleton().WithConfig(config) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
func (i *InstanceSingleton) Start() error { |
|
55
|
|
|
if err := i.echoInstance.Start(":" + i.config.Port); err != nil { |
|
56
|
|
|
return err |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return nil |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
func (i *InstanceSingleton) WithConfig(config ServerConfig) *InstanceSingleton { |
|
63
|
|
|
i.config = config |
|
64
|
|
|
return i |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
func (i *InstanceSingleton) registerMiddlewares(e *echo.Echo) { |
|
68
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ |
|
69
|
|
|
AllowOrigins: []string{"http://localhost", i.config.FrontendURL, i.config.Host}, |
|
70
|
|
|
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept}, |
|
71
|
|
|
})) |
|
72
|
|
|
|
|
73
|
|
|
// if debug |
|
74
|
|
|
if config.IsDevelopment() { |
|
75
|
|
|
e.Use(middleware.Logger()) |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// e.Use(middleware.Recover()) |
|
79
|
|
|
|
|
80
|
|
|
e.Use(middleware.Secure()) |
|
81
|
|
|
|
|
82
|
|
|
middleware.CSRFWithConfig(middleware.CSRFConfig{ |
|
83
|
|
|
TokenLookup: "cookie:_csrf", |
|
84
|
|
|
CookiePath: "/", |
|
85
|
|
|
CookieDomain: i.config.Host, |
|
86
|
|
|
CookieSecure: true, |
|
87
|
|
|
CookieHTTPOnly: true, |
|
88
|
|
|
CookieSameSite: http.SameSiteStrictMode, |
|
89
|
|
|
}) |
|
90
|
|
|
} |
|
91
|
|
|
|