-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
79 lines (62 loc) · 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"os"
"time"
"msattack/config"
"msattack/handlers/title"
"msattack/middleware"
"msattack/storage"
"msattack/utils"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = time.RFC3339Nano
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: zerolog.TimeFieldFormat})
config.LoadConfig("config/config.yml")
configuration := config.GlobalConfig
if configuration.Port <= 0 {
log.Fatal().Msg("Port in config file must be a positive integer.")
}
app := fiber.New(fiber.Config{
JSONEncoder: sonic.Marshal,
JSONDecoder: sonic.Unmarshal,
ErrorHandler: utils.DefaultErrorHandler,
})
app.Use(cors.New())
// Set default headers
app.Use(func(c *fiber.Ctx) error {
c.Set("Date", time.Now().Format(time.RFC1123))
c.Set("MSA-Signature", "https://github.com/jamestiotio/msattack")
return c.Next()
})
// Define global maintenance mode handler
app.Use(middleware.CheckForMaintenance)
// Define all other handlers
app.Get("/", utils.WelcomeMessage)
titleGroup := app.Group("/title")
titleGroup.Post("/get_pack_info", title.GetPackInfo)
titleGroup.Post("/get_file_list", title.GetFileList)
titleGroup.Get("/get_master_table", title.GetMasterTable)
storageGroup := app.Group(configuration.DataStorageEndpoint)
storageGroup.Get("/:version/:file_name", storage.GetDataFile)
storagePackGroup := storageGroup.Group(fmt.Sprintf("/pack/%d", configuration.PackVersion))
storagePackGroup.Get("/:pack_file_name", storage.GetPackFile)
log.Info().Msg("Spinning up server on port " + fmt.Sprintf("%d", configuration.Port) + "...")
port := fmt.Sprintf(":%d", configuration.Port)
if configuration.UseTLS {
err := app.ListenTLS(port, configuration.TLSCertPath, configuration.TLSKeyPath)
if err != nil {
log.Fatal().Err(err).Msg("Error in starting up server!")
}
} else {
err := app.Listen(port)
if err != nil {
log.Fatal().Err(err).Msg("Error in starting up server!")
}
}
}