-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharuno.go
139 lines (115 loc) · 3.37 KB
/
haruno.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"time"
"github.com/BurntSushi/toml"
"github.com/gorilla/mux"
"github.com/haruno-bot/haruno/coolq"
"github.com/haruno-bot/haruno/logger"
"github.com/haruno-bot/haruno/plugins"
_ "github.com/haruno-bot/haruno/sys"
)
type config struct {
Version string `toml:"version"`
LogsPath string `toml:"logsPath"`
ServerPort int `toml:"serverPort"`
CQWSURL string `toml:"cqWSURL"`
CQHTTPURL string `toml:"cqHTTPURL"`
CQToken string `toml:"cqToken"`
WebRoot string `toml:"webroot"`
}
// haruno 晴乃机器人
// 机器人运行的全局属性
type haruno struct {
s int64
c *config
}
const waitTime = time.Second * 15
var bot = new(haruno)
func (bot *haruno) loadConfig() {
cfg := new(config)
_, err := toml.DecodeFile("config.toml", cfg)
if err != nil {
logger.Logger.Fatalln("Haruno Initialize fialed:", err)
}
bot.s = time.Now().UnixNano() / 1e6
bot.c = cfg
}
// Initialize 从配置文件读取配置初始化
func (bot *haruno) Initialize() {
bot.loadConfig()
// 设置环境变量
os.Setenv("CQHTTPURL", bot.c.CQHTTPURL)
os.Setenv("CQWSURL", bot.c.CQWSURL)
os.Setenv("CQTOKEN", bot.c.CQToken)
logger.Service.SetLogsPath(bot.c.LogsPath)
logger.Service.Initialize()
plugins.SetupPlugins()
coolq.Client.Initialize(bot.c.CQToken)
go coolq.Client.Connect(bot.c.CQWSURL, bot.c.CQHTTPURL)
go coolq.Client.RegisterAllPlugins()
}
// Status 运行状态json格式
type Status struct {
Go int `json:"go"`
Version string `json:"version"`
Success int `json:"success"`
Fails int `json:"fails"`
Start int64 `json:"start"`
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
status := new(Status)
status.Fails = logger.Service.FailCnt()
status.Success = logger.Service.SuccessCnt()
status.Start = bot.s
status.Version = bot.c.Version
w.Header().Set("Content-Type", "application/json; charset=utf-8")
status.Go = runtime.NumGoroutine()
json.NewEncoder(w).Encode(status)
}
// Run 启动机器人
func (bot *haruno) Run() {
r := mux.NewRouter()
if bot.c.WebRoot != "" {
if _, err := os.Stat(bot.c.WebRoot); err == nil {
logger.Logger.Printf("web page root is found in \"%s\"", bot.c.WebRoot)
fileHandler := http.FileServer(http.Dir(bot.c.WebRoot))
r.Methods(http.MethodGet).Path("/").Handler(fileHandler)
r.Methods(http.MethodGet).PathPrefix("/static").Handler(fileHandler)
}
}
r.Methods(http.MethodGet).Path("/status").HandlerFunc(statusHandler)
r.Methods(http.MethodGet).Path("/logs/-/type=websocket").HandlerFunc(logger.WSLogHandler)
r.Methods(http.MethodGet).Path("/logs/-/type=plain").HandlerFunc(logger.RawLogHandler)
srv := &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", bot.c.ServerPort),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
go func() {
logger.Logger.Printf("haruno http server is listening on http://localhost:%d\n", bot.c.ServerPort)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
logger.Logger.Fatalln(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
srv.Shutdown(ctx)
logger.Logger.Println("haruno is shutting down")
os.Exit(0)
}
func main() {
bot.Initialize()
bot.Run()
}