/
server.go
90 lines (76 loc) · 2.46 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
package server
import (
"encoding/json"
"fmt"
"github.com/GenerateNU/sac/backend/src/auth"
"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/server/routes"
"github.com/GenerateNU/sac/backend/src/types"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
)
// @title SAC API
// @version 1.0
// @description Backend Server for SAC App
// @contact.name David Oduneye and Garrett Ladley
// @contact.email oduneye.d@northeastern.edu and ladley.g@northeastern.edu
// @host 127.0.0.1:8080
// @BasePath /
// @schemes http
func Init(db *gorm.DB, dependencies types.Dependencies, settings config.Settings) *fiber.App {
app := newFiberApp(settings.Application)
validate, err := utilities.RegisterCustomValidators()
if err != nil {
panic(fmt.Sprintf("Error registering custom validators: %s", err))
}
jwt := auth.NewJWTClient(settings.Auth, jwt.SigningMethodHS256)
authMiddleware := middleware.NewAuthAuthMiddlewareService(db, validate, settings.Auth)
apiv1 := app.Group("/api/v1")
apiv1.Use(authMiddleware.Authenticate)
routeParams := types.RouteParams{
Router: apiv1,
AuthMiddleware: authMiddleware,
ServiceParams: types.ServiceParams{
DB: db,
Validate: validate,
AuthSettings: &settings.Auth,
JWT: jwt,
Deps: dependencies,
},
}
allRoutes(app, routeParams)
return app
}
func allRoutes(app *fiber.App, routeParams types.RouteParams) {
routes.Utility(app)
routes.Auth(routeParams)
routes.UserRoutes(routeParams)
routes.ClubRoutes(routeParams)
routes.Contact(routeParams)
routes.PointOfContact(routeParams)
routes.Tag(routeParams)
routes.CategoryRoutes(routeParams)
routes.Event(routeParams)
routes.File(routeParams)
}
func newFiberApp(appSettings config.ApplicationSettings) *fiber.App {
app := fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
})
app.Use(cors.New(cors.Config{
AllowOrigins: fmt.Sprintf("http://%s:%d", appSettings.Host, appSettings.Port),
AllowCredentials: true,
}))
app.Use(requestid.New())
app.Use(logger.New(logger.Config{
Format: "[${time}] ${ip}:${port} ${pid} ${locals:requestid} ${status} - ${latency} ${method} ${path}\n",
}))
return app
}