-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (103 loc) · 3.05 KB
/
Copy pathmain.go
File metadata and controls
123 lines (103 loc) · 3.05 KB
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
package main
import (
"fmt"
"net/http"
"os"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/config"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/logging"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/net"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/net/middleware"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/net/routes"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/persist"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/repository"
"github.com/BEOpenSourceCollabs/EventManagementCore/pkg/service"
)
func main() {
envConfig := config.NewEnvironmentConfiguration()
// initialize log writer that outs logs as either json or text to stdout
var lw logging.LogWriter
if envConfig.Env.IsProduction() {
lw = logging.NewJsonLogWriter(os.Stdout, logging.INFO)
} else {
lw = logging.NewTextLogWriter(os.Stdout, logging.INFO)
}
// ContextLogger for main funcition, uses default log writer to print logs with Context
mainLogger := logging.NewContextLogger(lw, "Main")
mainLogger.Infof("loaded environment configuration for %s", envConfig.Env)
// Create a static handler to serve contents of 'static' folder.
fs := http.FileServer(http.Dir("./static"))
// initialize database from environment configuration
database, err := persist.NewDatabase(envConfig.Database)
if err != nil {
mainLogger.Fatal(err, "database connection error")
}
// Create a new instance of the appRouter
router := net.NewAppRouter(lw)
// Apply default middleware
router.Use(
middleware.RequestLoggerMiddleware{
Logger: logging.NewContextLogger(lw, "RequestLoggerMiddleware"),
},
)
// Apply development middleware(s)
if !envConfig.Env.IsProduction() {
router.Use(
middleware.CorsMiddleware{
Options: middleware.WideOpen,
},
)
}
// Register static files handler
router.Get("/", fs)
// Register a GET route for the root URL ("/")
router.Get("/api/health", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Ok"))
},
)) // Use the WideOpen CORS options to allow unrestricted access
userRepo := repository.NewSQLUserRepository(
database,
)
jwtService := service.NewJsonWebTokenService(
&envConfig.Security.JsonWebToken,
lw,
)
authService := service.NewJsonWebTokenAuthenticationService(
userRepo,
jwtService,
lw,
&service.AuthenticationServiceConfiguration{
IsProduction: envConfig.Env.IsProduction(),
},
)
// initialize and mount routes
routes.NewJsonWebTokenUserRoutes(
router,
userRepo,
&jwtService,
lw,
)
routes.NewJsonWebTokenAuthenticationRoutes(
router,
authService,
&jwtService,
lw,
)
routes.NewGoogleAuthenticationRoutes(
router,
service.NewGoogleAuthenticationService(
&envConfig.Security.Google,
userRepo,
jwtService,
lw,
),
authService,
lw,
)
address := fmt.Sprintf(":%d", envConfig.Port)
mainLogger.Infof("Starting server in %s mode on %s", envConfig.Env, address)
err = http.ListenAndServe(address, router)
if err != nil {
mainLogger.Fatal(err, "failed to start http server")
}
}