-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (59 loc) · 1.52 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
package main
import (
"context"
"fmt"
"github.com/Logiase/gomirai/bot"
"github.com/jinzhu/configor"
"github.com/labstack/echo/v4"
validator "gopkg.in/go-playground/validator.v9"
"io"
"log"
"os"
"os/signal"
"time"
)
var Log *log.Logger
var Bot *bot.Bot
var Conf Config
type EchoRequestValidator struct {
validator *validator.Validate
}
func (cv *EchoRequestValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
func main() {
if err := configor.Load(&Conf, "config.yml"); err != nil {
panic(err)
}
{
logFile, err := os.OpenFile(Conf.Log.Name, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
panic(err)
}
Log = log.New(io.MultiWriter(logFile, os.Stderr), "[main] ", log.LstdFlags|log.Lshortfile)
}
dialBot()
e := echo.New()
e.Validator = &EchoRequestValidator{validator: validator.New()}
e.POST("/webhook", webhookHandler)
// Start server
go func() {
if err := e.Start(Conf.Server.Address); err != nil {
e.Logger.Info("quit routine initiated...")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
fmt.Println("cleaning up...")
fmt.Println("releasing mirai-http session as exiting http server")
Bot.Client.Release(Conf.MiraiHTTP.QQNumber)
fmt.Println("shutting down http server...")
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}