-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (75 loc) · 2.7 KB
/
main.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
package main
import (
"ConnectServer/Frameworks/CoreData"
"ConnectServer/Frameworks/Performance"
"ConnectServer/Frameworks/Security"
"ConnectServer/Helpers"
"ConnectServer/RouteHandlers/Account"
"ConnectServer/RouteHandlers/Conversation"
"ConnectServer/RouteHandlers/Identity"
"ConnectServer/Types"
"context"
"encoding/json"
"log"
"net/http"
"os"
"github.com/bwmarrin/snowflake"
"github.com/joho/godotenv"
)
func main() {
appEnvPath := os.Getenv("APP_ENV_PATH")
if appEnvPath == "" {
appEnvPath = ".env"
}
err := godotenv.Load(appEnvPath)
if err != nil {
log.Fatal("error loading .env file")
}
snowflakeNode, err := snowflake.NewNode(0)
if err != nil {
log.Fatal(err)
}
Security.SnowflakeNode = snowflakeNode
CoreData.ConnectUserServices()
//CoreData.ConnectStorageServices()
router := http.NewServeMux()
router.HandleFunc("POST /Account/SignIn", Account.SignInHandler)
router.HandleFunc("POST /Account/SignUp", Performance.ExecutionTimeMeasurementMiddleware(Account.SignUpHandler, "SignUp"))
router.HandleFunc("GET /Account/RefreshSession", Account.RefreshSessionHandler)
router.HandleFunc("POST /Identity", Identity.GetIdentityHandler)
router.HandleFunc("GET /Conversations", AuthGuard(Performance.ExecutionTimeMeasurementMiddleware(Conversation.FetchManyConversationsHandler, "FetchManyConversations (authGuard ommited)")))
err = http.ListenAndServe(os.Getenv("NETWORK_ADDR"), router)
if err != nil {
log.Fatal(err)
}
}
// AuthGuard is a middleware wrapper that guards the wrapped route handler preventing any unauthorized requests. It validates the access token, derives the identity of the token owner, and finally passes it into the request context. On validation failure it returns, thus preventing the call of the route handler function.
//
// Usage: AuthGuard(Account.SignInRouteHandler))
func AuthGuard(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
encoder := *json.NewEncoder(w)
log.Println("Before handler")
accessToken := Security.RetrieveBearerTokenFromAuthHeader(r.Header.Get("Authorization"))
subject, err := Security.VerifyAccessTokenAndDeriveOwnerId(accessToken)
if err != nil || subject == "" {
if err != nil {
log.Print(err)
}
Helpers.JSONError(encoder, w, Types.HttpErrorResponse{
Error: "Access Token Invalid",
HttpResponse: Types.HttpResponse{
Success: false,
},
}, http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), "tokenSubject", subject)
// ctx = context.WithValue(ctx, "userRole", userRole)
// ctx = context.WithValue(ctx, "userID", userID)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
// Logic after the handler
log.Println("After handler")
}
}