-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (79 loc) · 2.18 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/zechenturm/yahas/item"
"github.com/zechenturm/yahas/logging"
"github.com/gorilla/mux"
)
type coreConfig struct {
Loglevels logConfig `json:"logging"`
Permissions map[string]map[string]bool `json:"plugin-permissions"`
IgnorePlugins []string `json:"ignore-plugins"`
}
type logConfig struct {
Core string `json:"core"`
Default string `json:"default"`
Bindings map[string]string `json:"bindings"`
Plugins map[string]string `json:"plugins"`
}
var itemFileDir = "config/items"
var coreLogger *logging.Logger
var Items = make(item.NamespaceMap)
var coreconf coreConfig
var mainRouter *mux.Router
var loader *item.Loader
func main() {
coreconf, err := loadConfig()
if err != nil {
err := logging.InitLogging(logging.DEFAULT)
if err != nil {
fmt.Println("Error initializin logging:", err)
return
}
coreLogger = logging.New("core", logging.DEFAULT)
coreLogger.ErrorLn("Error loading config:", err)
return
}
err = logging.InitLogging(logging.StrToLvl(coreconf.Loglevels.Default))
if err != nil {
fmt.Println("Error initializin logging:", err)
return
}
coreLogger = logging.New("core", logging.StrToLvl(coreconf.Loglevels.Core))
loader = item.NewLoader(coreLogger, &coreconf.Loglevels.Bindings)
Items, err = loader.LoadItems(itemFileDir)
if err != nil {
coreLogger.ErrorLn("Error loading items:", err)
return
}
mainRouter = mux.NewRouter()
mainRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./web/"))))
err = loadPlugins(coreconf.IgnorePlugins)
if err != nil {
coreLogger.ErrorLn("Error loading plugins:", err)
}
err = http.ListenAndServe(":8000", mainRouter)
if err != nil {
coreLogger.ErrorLn("HTTP server error:", err)
}
for {
time.Sleep(time.Second)
}
}
func loadConfig() (coreConfig, error) {
bytes, err := ioutil.ReadFile("config/core.json")
if err != nil {
return coreConfig{}, err
}
config := coreConfig{}
err = json.Unmarshal(bytes, &config)
if err != nil {
return coreConfig{}, err
}
fmt.Println("config:", config)
return config, nil
}