-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (91 loc) · 3.18 KB
/
Copy pathmain.go
File metadata and controls
107 lines (91 loc) · 3.18 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
package main
import (
"github.com/MathisBurger/crypto-simulator/auth"
"github.com/MathisBurger/crypto-simulator/controller"
"github.com/MathisBurger/crypto-simulator/database/actions"
"github.com/MathisBurger/crypto-simulator/services"
"github.com/MathisBurger/crypto-simulator/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/joho/godotenv"
"os"
"time"
)
func main() {
err := godotenv.Load()
if err != nil {
panic(err.Error())
}
// initialize the database tables
// only if they are not existing
actions.InitTables()
// generates keys for JWT
// only if they are not existing
utils.GenerateKeys()
// init fiber app
app := fiber.New(fiber.Config{
Prefork: false,
})
// initialize logger and cors
app.Use(logger.New())
app.Use(cors.New(cors.Config{
AllowCredentials: true,
ExposeHeaders: "Authorization",
}))
// enable rate limiter
// if it is enabled in docker-compose
// via environment variables
if os.Getenv("RATE_LIMITER") == "enabled" {
app.Use(limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool {
return c.IP() == "127.0.0.1"
},
Max: 20,
Expiration: 10 * time.Second,
KeyGenerator: func(ctx *fiber.Ctx) string {
return ctx.Get("x-forwarded-for")
},
LimitReached: func(ctx *fiber.Ctx) error {
return ctx.SendStatus(fiber.StatusTooManyRequests)
},
}))
}
// refresh token auth controller
app.Post("/api/auth/login", auth.LoginController)
app.Get("/api/auth/accessToken", auth.AccessTokenController)
app.Post("/api/auth/revokeSession", auth.RevokeSessionController)
app.Get("/api/auth/me", auth.StatusController)
app.Get("/api", controller.DefaultController)
app.Post("/api/register", controller.RegisterController)
app.Get("/api/checkBalance", controller.CheckBalanceController)
app.Get("/api/getAllCurrencys", controller.GetAllCurrencysController)
app.Get("/api/getCurrency", controller.GetCurrencyController)
app.Post("/api/buyCrypto", controller.BuyCryptoController)
app.Post("/api/sellCrypto", controller.SellCryptoController)
app.Get("/api/getAllTrades", controller.GetAllTradesController)
app.Get("/api/getWalletsForUser", controller.GetCryptoWalletsForUser)
// enables all deprecated endpoints
// must be specified in the config
if os.Getenv("DEPRECATED_ENDPOINTS") == "enabled" {
app.Get("/api/getCurrencyData", controller.GetCurrencyDataController)
app.Post("/api/login", controller.LoginController)
app.Get("/api/checkTokenStatus", controller.GetTokenStatusController)
}
// Web endpoints
app.Static("/", "./web/dist/web")
app.Static("/register", "./web/dist/web/index.html")
app.Static("/login", "./web/dist/web/index.html")
app.Static("/dashboard", "./web/dist/web/index.html")
app.Static("/currency-view/*", "./web/dist/web/index.html")
// starts the currency updating service as go-routine
go services.CurrencyUpdater()
// starts the refresh token removing service as go routine
go services.RefreshTokenRemovingService()
// starts the http-server
err = app.Listen(":" + os.Getenv("APPLICATION_PORT"))
if err != nil {
panic(err.Error())
}
}