/
echo.go
105 lines (81 loc) · 3.52 KB
/
echo.go
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
package echo
import (
apihandler "github.com/devgek/webskeleton/web/api/handler"
handler2 "github.com/devgek/webskeleton/web/template/handler"
"net/http"
"github.com/devgek/webskeleton/config"
webenv "github.com/devgek/webskeleton/web/env"
"github.com/devgek/webskeleton/web/handler"
"github.com/devgek/webskeleton/web/template"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
//InitEchoApi initialize the echo web framework for serving an api
func InitEchoApi(env *webenv.Env) *echo.Echo {
e := echo.New()
e.HideBanner = true
if config.IsServerDebug() {
e.Debug = true
// e.Use(middleware.Recover())
}
e.Renderer = template.NewRenderer(env.TStore)
e.HTTPErrorHandler = handler.LoggingDefaultHTTPErrorHandler
// api
apiGroup := e.Group("/api")
apiGroup.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte("secret"),
ContextKey: "token",
Skipper: handler.JWTAuthSkipper,
}))
apiGroup.Use(handler.TokenLoggingMiddleware)
apiGroup.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete, http.MethodOptions, http.MethodHead},
}))
// OPTIONS because of CORS Preflight requests sent from axios
apiGroup.Match([]string{"OPTIONS", "POST"}, "/login", apihandler.HandleAPILogin)
apiGroup.Match([]string{"OPTIONS", "POST"}, "/entitylist:entity", apihandler.HandleAPIEntityList)
apiGroup.Match([]string{"OPTIONS", "POST"}, "/optionlist:entity", apihandler.HandleAPIOptionList)
apiGroup.Match([]string{"OPTIONS", "POST"}, "/entitynew:entity", apihandler.HandleAPICreate)
apiGroup.Match([]string{"OPTIONS", "POST"}, "/entityedit:entity", apihandler.HandleAPIUpdate)
apiGroup.Match([]string{"OPTIONS", "POST"}, "/entitydelete:entity/:id", apihandler.HandleAPIDelete)
apiGroup.PUT("/allnew:entity", apihandler.HandleAPICreateAll)
apiGroup.GET("/health", apihandler.HandleAPIHealth)
// resoure files
assetHandler := http.FileServer(env.Assets)
e.GET(webenv.AssetHandlerPattern, handler.AssetHandlerFunc(http.StripPrefix(webenv.AssetPattern, assetHandler)))
e.GET("/favicon.ico", handler2.HandleFavicon)
e.Use(handler.EnvContextMiddleware)
e.Use(handler.RequestLoggingMiddleware)
return e
}
//InitEchoWebApp initialize the echo web framework for serving a web application
func InitEchoWebApp(env *webenv.Env) *echo.Echo {
e := echo.New()
e.HideBanner = true
if config.IsServerDebug() {
e.Debug = true
// e.Use(middleware.Recover())
}
e.Renderer = template.NewRenderer(env.TStore)
e.HTTPErrorHandler = handler.LoggingDefaultHTTPErrorHandler
// resoure files
assetHandler := http.FileServer(env.Assets)
e.GET(webenv.AssetHandlerPattern, handler.AssetHandlerFunc(http.StripPrefix(webenv.AssetPattern, assetHandler)))
//
e.GET("/health", handler2.HandleHealth)
e.POST("/loginuser", handler2.HandleLogin)
e.GET("/logout", handler2.HandleLogout)
e.GET("/favicon.ico", handler2.HandleFavicon)
e.Match([]string{"GET", "POST"}, "/entitylist:entity", handler.HandleEntityList)
e.POST("/entityedit:entity", handler.HandleEntityEdit)
e.POST("/entitynew:entity", handler.HandleEntityNew)
e.POST("/entitydelete:entity", handler.HandleEntityDelete)
e.Match([]string{"GET", "POST"}, "/", handler2.HandleStartApp)
e.Match([]string{"GET", "POST"}, "/page1", handler2.HandlePage1)
e.Match([]string{"GET", "POST"}, "/:page", handler2.HandlePageDefault)
e.Use(handler.EnvContextMiddleware)
e.Use(handler.RequestLoggingMiddleware)
e.Use(handler.CookieAuthMiddleware)
return e
}