-
Notifications
You must be signed in to change notification settings - Fork 6
/
loader.go
89 lines (84 loc) · 2 KB
/
loader.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
package server
import (
"bufio"
"fmt"
"github.com/buaazp/fasthttprouter"
"github.com/mamoe/mirai-public-api/logger"
"github.com/valyala/fasthttp"
"io"
"log"
"os"
"strings"
)
func (this *MiraiApiServer) Init() {
//logger
this.Logger = logger.NewLogger("MiraiAPIServer")
this.Logger.Terminal.Color("g")
this.Logger.Log("Loading Mirai API Server...")
//config
this.Logger.Log("Loading config")
this.initConfig()
//router
this.Logger.Log("Loading fasthttp router...")
this.Router = fasthttprouter.New()
this.Router.GET("/getPluginList", PluginListPage)
this.Router.GET("/getPluginDetailedInfo", PluginDetailedInfoPage)
if this.Config["404Support"] == "true" {
this.initNotFoundPage()
this.Router.NotFound = func(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/html;charset=utf-8")
fmt.Fprintf(ctx, this.NotFoundPage)
}
}
//service
this.Service = &Service{}
this.Service.initScan()
//start
this.Logger.Log("Starting fasthttp Server")
log.Fatal(fasthttp.ListenAndServe(this.Config["listenHTTP"], this.Router.Handler))
}
func (this *MiraiApiServer) initNotFoundPage() {
page, err := GetWebPage(this.Config["404PageURL"])
if err != nil {
this.Logger.Log(err.Error())
}
this.NotFoundPage = page
}
func (this *MiraiApiServer) initConfig() {
if this.ConfigPath == "" || IsExist(this.ConfigPath) {
this.ConfigPath = "app.conf"
this.Logger.Log("Using default config file : app.conf")
}
config := make(map[string]string)
f, err := os.Open(this.ConfigPath)
defer f.Close()
if err != nil {
panic(err)
}
r := bufio.NewReader(f)
for {
b, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
s := strings.TrimSpace(string(b))
index := strings.Index(s, "=")
if index < 0 {
continue
}
key := strings.TrimSpace(s[:index])
if len(key) == 0 {
continue
}
value := strings.TrimSpace(s[index+1:])
if len(value) == 0 {
continue
}
config[key] = value
this.Logger.Log(" " + key + "=" + value)
}
this.Config = config
}