-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (56 loc) · 1.58 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
package core
import (
"encoding/json"
"github.com/go-echarts/statsview"
routing "github.com/qiangxue/fasthttp-routing"
mantis "github.com/sphireinc/mantis/http"
"github.com/valyala/fasthttp"
"os"
)
type Config struct {
Application `json:"application"`
Components `json:"components"`
Server `json:"server"`
Middleware `json:"middleware"`
Router `json:"router"`
Log `json:"log"`
Environment `json:"environment"`
Persistence `json:"persistence"`
Communication `json:"communication"`
S mantis.Status
}
type Context = routing.Context
type Res = mantis.Response
// Run loads routes and starts the Server.
func (c *Config) Run() {
if c.Components.StatsView {
go func() {
_ = statsview.New().Start()
}()
}
// if c has no Address, default to localhost
if c.Server.Address == "" {
c.Server.Address = "127.0.0.1"
c.Server.Port = "8080"
c.Log.Writer.Info("no server address in config, defaulting to 127.0.0.1:8080")
}
// Load our routes
c.Router.load()
c.Log.Writer.Info("serving app - BON VOYAGE!")
if err := fasthttp.ListenAndServe(":"+c.Server.Port, c.Router.Router.HandleRequest); err != nil {
c.Log.Writer.HandleFatalError(err)
}
}
// Load takes our Config object and loads our environment defined JSON config
func (c *Config) Load() {
file, err := os.ReadFile(c.Environment.Location)
if err != nil {
panic("Cannot find config file " + c.Environment.Location)
}
err = json.Unmarshal([]byte(file), &c)
if err != nil {
panic("Error loading config file " + c.Environment.Location)
}
// Populate our statuses
c.S.Fill()
}