-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
111 lines (89 loc) · 2.84 KB
/
router.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
package main
import (
"github.com/gorilla/securecookie"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/sessions"
"github.com/mehulgohil/go-bffauth.git/authenticator"
"github.com/mehulgohil/go-bffauth.git/config"
"github.com/mehulgohil/go-bffauth.git/controller"
"github.com/mehulgohil/go-bffauth.git/interfaces"
"github.com/mehulgohil/go-bffauth.git/middleware"
"io"
"os"
"sync"
)
var (
irisRouter *router
routerOnce sync.Once
cookieNameForSessionID = "mycookiesessionnameid"
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
)
type IRouter interface {
InitRouter(auth *authenticator.Authenticator, redis interfaces.IRedisLayer) *iris.Application
}
type router struct{}
func (router *router) InitRouter(auth *authenticator.Authenticator, redis interfaces.IRedisLayer) *iris.Application {
app := iris.New()
ac := makeAccessLog()
app.UseRouter(ac.Handler)
app.Use(sess.Handler())
app.Use(useSecureCookies())
// Our custom CORS middleware.
crs := func(ctx iris.Context) {
ctx.Header("Access-Control-Allow-Origin", config.EnvVariables.FrontendURL)
ctx.Header("Access-Control-Allow-Credentials", "true")
if ctx.Method() == iris.MethodOptions {
ctx.Header("Access-Control-Methods",
"POST, PUT, PATCH, GET, DELETE")
ctx.Header("Access-Control-Allow-Headers",
"Access-Control-Allow-Origin,Content-Type,Authorization")
ctx.Header("Access-Control-Max-Age",
"86400")
ctx.StatusCode(iris.StatusNoContent)
return
}
ctx.Next()
}
app.UseRouter(crs)
loginHandler := controller.LoginHandler{Auth: auth}
callbackHandler := controller.CallbackHandler{Auth: auth, RedisClient: redis}
logoutHandler := controller.LogoutHandler{RedisClient: redis}
backendApiHandler := controller.BackendApiHandler{RedisClient: redis}
middlewareHandler := middleware.MiddlewareHandler{RedisClient: redis}
app.Get("/login", loginHandler.Login)
app.Get("/callback", callbackHandler.Callback)
app.Get("/logout", logoutHandler.Logout)
// Backend Api
app.Post("/shorten", middlewareHandler.IsAuthenticated, backendApiHandler.WriterRedirect)
return app
}
func Router() IRouter {
if irisRouter == nil {
routerOnce.Do(func() {
irisRouter = &router{}
})
}
return irisRouter
}
// This helps to log the request and its metadata
func makeAccessLog() *accesslog.AccessLog {
ac := accesslog.New(io.MultiWriter(os.Stdout))
ac.Delim = ' '
ac.ResponseBody = false
ac.RequestBody = false
ac.BytesReceived = true
ac.BytesSent = true
return ac
}
func useSecureCookies() iris.Handler {
var (
hashKey = securecookie.GenerateRandomKey(64)
blockKey = securecookie.GenerateRandomKey(32)
s = securecookie.New(hashKey, blockKey)
)
return func(ctx iris.Context) {
ctx.AddCookieOptions(iris.CookieEncoding(s))
ctx.Next()
}
}