-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (46 loc) · 1.27 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
package main
import (
"go-keycloak/src/config"
"go-keycloak/src/controllers"
"go-keycloak/src/services"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
func main() {
log.Println("MODE:", os.Getenv("MODE"))
if os.Getenv("MODE") != "development" &&
os.Getenv("MODE") != "staging" &&
os.Getenv("MODE") != "sandbox" &&
os.Getenv("MODE") != "production" {
err := godotenv.Load(".env.local")
if err != nil {
log.Fatal("Error loading .env file", err)
}
}
run()
}
func run() {
config.DbConnect()
services.InitializeOauthServer()
router := mux.NewRouter().StrictSlash(true)
// use middleware globally
router.Use(commonMiddleware)
registerRoutes(router)
log.Fatal(http.ListenAndServe(":8081", router))
}
func registerRoutes(router *mux.Router) {
registerControllerRoutes(controllers.EventController{}, router)
}
func registerControllerRoutes(controller controllers.Controller, router *mux.Router) {
controller.RegisterRoutes(router)
}
// a middleware that sets the Content-Type header for all request to application/json
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}