aboutsummaryrefslogtreecommitdiff
path: root/cmd/app/main.go
blob: e414bf2c45eb87f6a0a0879b2c154e5f4f5d4c4e (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main

import (
	"fmt"
	"path"
	"strings"
	"time"

	"immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/api"
	"immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/jloup/utils"
)

var log = utils.StandardL().WithField("module", "api")

type AppConfig struct {
	PublicDir string `toml:"public_dir"`
}

type ApiConfig struct {
	api.Config
}

type Config struct {
	App   AppConfig
	Api   ApiConfig
	Mail  api.MailConfig
	Db    db.DBConfig
	Redis db.RedisConfig

	utils.LogConfiguration
	Address string
	Port    string
	Mode    string
}

func (c *Config) SetToDefaults() {
	*c = Config{
		Address: "localhost",
		Port:    "8000",
		Mode:    "dev",
		App: AppConfig{
			PublicDir: "./public",
		},
	}

	c.LogConfiguration.SetToDefaults()
}

var C Config

func init() {
	utils.MustParseStdConfigFile(&C)

	err := utils.ConfigureStdLogger(C.LogConfiguration)
	if err != nil {
		panic(err)
	}

	api.SetConfig(C.Api.Config)
	api.SetMailConfig(C.Mail)

	db.Init(C.Db, C.Redis)

	if C.Mode == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	log.Infof("CONFIG:")
	log.Infof("LISTEN: %s", strings.Join([]string{C.Address, C.Port}, ":"))
	log.Infof("PUBLIC_DIR: %s", C.App.PublicDir)
}

func SetApiRoute(router *gin.RouterGroup, route api.Route) {
	switch route.Method {
	case "GET":
		router.GET(route.Path, route.Handlers...)
	case "POST":
		router.POST(route.Path, route.Handlers...)
	case "OPTIONS":
		router.OPTIONS(route.Path, route.Handlers...)
	default:
		panic(fmt.Errorf("%s method not handled", route.Method))
	}
}

func SetGroup(router *gin.RouterGroup, group api.Group) {
	var r *gin.RouterGroup
	if group.Root == "" {
		r = router
	} else {
		r = router.Group(group.Root)
	}

	if group.Middlewares != nil {
		for _, middleware := range group.Middlewares {
			r.Use(api.M(middleware))
		}
	}

	for _, route := range group.Routes {
		SetApiRoute(r, route)
	}
}

func main() {
	engine := gin.New()

	apiGroup := engine.Group("/api")
	appGroup := engine

	engine.Use(gin.Recovery())

	if C.Mode == "production" {
		engine.Use(api.Logger())
		apiGroup.Use(api.Logger())
	} else {
		engine.Use(gin.Logger())
		apiGroup.Use(gin.Logger())
	}

	apiGroup.Use(cors.New(cors.Config{
		AllowOrigins:     []string{fmt.Sprintf("https://%s", C.Api.Domain)},
		AllowMethods:     []string{"POST", "GET", "OPTIONS"},
		AllowHeaders:     []string{"Authorization"},
		ExposeHeaders:    []string{"Authorization"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))

	for _, group := range api.Groups {
		SetGroup(apiGroup, group)
	}

	appGroup.Static("/public", C.App.PublicDir)
	availableRoutes := []string{
		"/",
		"/signup",
		"/signin",
		"/confirm",
		"/reset-password",
		"/change-password",
		"/signout",
		"/me",
		"/account",
		"/otp/enroll",
		"/otp/validate",
		"/not_confirmed",
	}

	for _, route := range availableRoutes {
		appGroup.StaticFile(route, path.Join(C.App.PublicDir, "/index.html"))
	}

	engine.Run(strings.Join([]string{C.Address, C.Port}, ":"))
}