This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
109 lines (91 loc) · 2.86 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
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
package main
import (
"os"
"time"
"github.com/aarzilli/golua/lua"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/pprof"
"github.com/rs/zerolog"
"github.com/rs/zerolog/diode"
"github.com/rs/zerolog/log"
"github.com/sosodev/heart/build"
"github.com/sosodev/heart/config"
"github.com/sosodev/heart/kv"
"github.com/sosodev/heart/modules"
"github.com/sosodev/heart/pool"
)
func main() {
// initial logging setup
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
if os.Getenv("PROD") != "prod" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
config := config.NewConfig()
zerolog.SetGlobalLevel(config.LogLevel)
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
// enable pprof profiling if requested
if config.Profile {
log.Info().Msg("Enabling pprof profiler on route /debug/pprof/")
app.Use(pprof.New())
}
// logging middleware
app.Use(func(c *fiber.Ctx) error {
// start timing request
start := time.Now()
// handle the request
chainErr := c.Next()
if chainErr != nil {
err := app.Config().ErrorHandler(c, chainErr)
if err != nil {
c.SendStatus(fiber.StatusInternalServerError)
}
}
responseTime := time.Since(start)
log.Info().Int("status", c.Response().StatusCode()).Str("method", c.Method()).Str("path", c.Path()).Str("response_time", responseTime.String()).Msg("Request")
return nil
})
statePool, err := pool.New(config, func(nuState *lua.State) error {
// TODO: considering reducing lib availibility in Lua
nuState.OpenLibs()
// Load modules to be used in the Lua code
// Unfortunately order does matter here
// Heart depends on context which depends on JSON
err := modules.LoadJSON(nuState)
if err != nil {
return err
}
err = modules.LoadContext(nuState)
if err != nil {
return err
}
err = modules.LoadKV(nuState)
if err != nil {
return err
}
err = modules.LoadHeart(app, nuState)
if err != nil {
return err
}
return nuState.DoFile(config.Path)
})
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize lua state")
}
defer statePool.Cleanup()
defer kv.CloseStores()
// This function grabs one of the initialStates from the pool to build up the fiber routes
// It's worth noting that this means that app routes can't be built up dynamically
// But that's probably not a good idea anyway and implementing it would probably kill performance or me :(
build.Routes(app, statePool)
// swap out the log's writer for a non-blocking one
// this greatly increases logging throughput
if config.Production {
nonBlockingWriter := diode.NewWriter(os.Stdout, 10000, 1*time.Millisecond, func(missed int) {})
defer nonBlockingWriter.Close()
log.Logger = log.Output(nonBlockingWriter)
}
log.Info().Str("port", config.Port).Msg("Heart is online 💜")
log.Fatal().Err(app.Listen(":" + config.Port)).Msg("App failed to run")
}