-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
133 lines (121 loc) · 3.31 KB
/
server.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
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
package web
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/vshn/odootools/pkg/odoo"
"github.com/vshn/odootools/pkg/odoo/model"
"github.com/vshn/odootools/pkg/web/controller"
)
type Server struct {
odooClient *odoo.Client
Echo *echo.Echo
cookieStore *sessions.CookieStore
dbName string
versionInfo VersionInfo
}
func NewServer(
odoo *odoo.Client,
secretKey string,
dbName string,
versionInfo VersionInfo,
) *Server {
key, err := base64.StdEncoding.DecodeString(secretKey)
if err != nil {
log.Fatalf("Error decoding secret key. Generate with `openssl rand -base64 32`. %v", err)
}
s := Server{
odooClient: odoo,
dbName: dbName,
Echo: echo.New(),
cookieStore: sessions.NewCookieStore(key, key),
versionInfo: versionInfo,
}
e := s.Echo
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: s.skipAccessLogs,
Format: middleware.DefaultLoggerConfig.Format,
CustomTimeFormat: middleware.DefaultLoggerConfig.CustomTimeFormat,
}))
e.Use(session.MiddlewareWithConfig(session.Config{
Store: s.cookieStore,
}))
authMiddleware := middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
KeyLookup: "cookie:" + SessionCookieID,
Validator: func(s string, context echo.Context) (bool, error) {
// 's' contains already the encrypted cookie value, which means we likely have a valid odoo session
return true, nil
},
ErrorHandler: func(err error, context echo.Context) error {
return context.Redirect(http.StatusTemporaryRedirect, "/login")
},
Skipper: s.unprotectedRoutes(),
})
e.Renderer = controller.NewRenderer()
s.setupRoutes(authMiddleware)
return &s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.Echo.ServeHTTP(w, r)
}
func (s *Server) newControllerContext(e echo.Context) *controller.BaseController {
sess := s.GetOdooSession(e)
data := s.GetSessionData(e)
logCtx := logr.NewContext(e.Request().Context(), funcr.NewJSON(func(obj string) {
// TODO: Integrate with echo logger?
fmt.Println(obj)
}, funcr.Options{Verbosity: 2}))
return &controller.BaseController{Echo: e, OdooClient: model.NewOdoo(sess), OdooSession: sess, SessionData: data, RequestContext: logCtx}
}
func (s *Server) ShowError(e echo.Context, err error) error {
return e.Render(http.StatusInternalServerError, "error", controller.AsError(err))
}
var publicRoutes = []string{
"/favicon.ico",
"/robots.txt",
"/static/*",
"/healthz",
}
func (s *Server) skipAccessLogs(e echo.Context) bool {
for _, path := range publicRoutes {
if path == e.Path() {
return true
}
}
return false
}
func (s *Server) unprotectedRoutes() middleware.Skipper {
return func(e echo.Context) bool {
for _, path := range publicRoutes {
if path == e.Path() {
return true
}
}
for _, path := range []string{
"/login",
"/logout",
"/",
} {
if path == e.Path() {
return true
}
}
return false
}
}
func (s *Server) helpPage(e echo.Context) error {
return e.Render(http.StatusOK, "help",
controller.Values{
"Nav": controller.Values{
"LoggedIn": true,
},
})
}