mtfosbot/main.go

145 lines
3.5 KiB
Go
Raw Normal View History

2018-08-14 09:25:34 +00:00
package main
import (
2018-09-17 16:35:48 +00:00
"encoding/gob"
2018-09-19 09:26:36 +00:00
"errors"
2018-09-19 12:23:39 +00:00
"flag"
2018-10-09 16:35:28 +00:00
"fmt"
2018-08-14 09:25:34 +00:00
"log"
2018-09-19 09:26:36 +00:00
"os"
"path"
2018-10-09 16:35:28 +00:00
"runtime"
2018-08-14 09:25:34 +00:00
"strconv"
"strings"
2018-10-09 16:35:28 +00:00
"time"
2018-08-14 09:25:34 +00:00
2018-09-20 17:14:08 +00:00
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
2018-09-19 14:06:27 +00:00
"git.trj.tw/golang/mtfosbot/module/cmd"
2018-10-09 17:12:07 +00:00
"git.trj.tw/golang/mtfosbot/module/es"
2018-09-19 12:23:39 +00:00
"git.trj.tw/golang/mtfosbot/module/options"
2018-09-19 09:26:36 +00:00
"git.trj.tw/golang/mtfosbot/module/utils"
2018-08-14 09:25:34 +00:00
"git.trj.tw/golang/mtfosbot/model"
2018-09-10 10:13:27 +00:00
"git.trj.tw/golang/mtfosbot/module/background"
2018-08-22 09:20:12 +00:00
"git.trj.tw/golang/mtfosbot/module/config"
2018-09-13 10:18:59 +00:00
twitchirc "git.trj.tw/golang/mtfosbot/module/twitch-irc"
2018-08-14 09:25:34 +00:00
"git.trj.tw/golang/mtfosbot/router/routes"
"github.com/gin-gonic/gin"
)
var server *gin.Engine
2018-09-19 12:23:39 +00:00
func init() {
options.RegFlag()
flag.Parse()
}
2018-08-14 09:25:34 +00:00
func main() {
2018-09-19 12:23:39 +00:00
runOptions := options.GetFlag()
if runOptions.Help {
flag.Usage()
return
}
err := config.LoadConfig(runOptions.Config)
2018-08-22 09:20:12 +00:00
if err != nil {
log.Fatal(err)
2018-08-14 09:25:34 +00:00
}
// connect to database
2018-08-28 03:55:38 +00:00
db, err := model.NewDB()
2018-08-14 09:25:34 +00:00
if err != nil {
log.Fatal(err)
}
2018-08-28 03:55:38 +00:00
defer db.Close()
2018-08-14 09:25:34 +00:00
2018-09-19 14:06:27 +00:00
if runOptions.DBTool {
cmd.DBTool()
}
registerTypes()
background.SetBackground()
// create http server
server = routes.NewServ()
routes.SetRoutes(server)
2018-09-15 15:48:40 +00:00
go twitchirc.InitIRC()
2018-09-13 10:18:59 +00:00
2018-09-19 09:26:36 +00:00
// create thumbnail directory
conf := config.GetConf()
if !utils.CheckExists(conf.ImageRoot, true) {
log.Fatal(errors.New("image root not exists"))
}
if !utils.CheckExists(path.Join(conf.ImageRoot, "thumbnail"), true) {
err = os.MkdirAll(path.Join(conf.ImageRoot, "thumbnail"), 0775)
if err != nil {
log.Fatal(err)
}
}
2018-10-01 16:11:32 +00:00
if !utils.CheckExists(conf.LogImageRoot, true) {
log.Fatal(errors.New("log image root not exists"))
}
2018-09-19 09:26:36 +00:00
2018-10-09 16:35:28 +00:00
go func() {
for {
PrintMemUsage()
2018-10-09 16:52:40 +00:00
time.Sleep(time.Second * 20)
2018-10-09 16:35:28 +00:00
}
}()
2018-08-22 09:20:12 +00:00
server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, ""))
2018-08-14 09:25:34 +00:00
}
2018-09-17 16:35:48 +00:00
2018-10-09 16:35:28 +00:00
// PrintMemUsage -
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
2018-10-09 16:52:40 +00:00
fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc))
fmt.Printf("\t HeapSys = %v MiB", bToMb(m.HeapSys))
fmt.Printf("\t NextGC = %v MiB\n", bToMb(m.NextGC))
2018-10-09 17:12:07 +00:00
obj := map[string]interface{}{
"Alloc": fmt.Sprintf("%v MiB", bToMb(m.Alloc)),
"Sys": fmt.Sprintf("%v MiB", bToMb(m.Sys)),
"HeapAlloc": fmt.Sprintf("%v MiB", bToMb(m.HeapAlloc)),
"HeapSys": fmt.Sprintf("%v MiB", bToMb(m.HeapSys)),
"HeapIdle": fmt.Sprintf("%v MiB", bToMb(m.HeapIdle)),
"HeapInuse": fmt.Sprintf("%v MiB", bToMb(m.HeapInuse)),
"HeapReleased": fmt.Sprintf("%v MiB", bToMb(m.HeapReleased)),
"StackInuse": fmt.Sprintf("%v MiB", bToMb(m.StackInuse)),
"StackSys": fmt.Sprintf("%v MiB", bToMb(m.StackSys)),
"GCSys": fmt.Sprintf("%v MiB", bToMb(m.GCSys)),
"NextGC": fmt.Sprintf("%v MiB", bToMb(m.NextGC)),
"NumGC": fmt.Sprintf("%v", m.NumGC),
}
es.PutLog("memory", obj)
2018-10-09 16:35:28 +00:00
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
2018-09-17 16:35:48 +00:00
func registerTypes() {
gob.Register(model.Account{})
gob.Register(model.Commands{})
gob.Register(model.DonateSetting{})
gob.Register(model.FacebookPage{})
gob.Register(model.KeyCommands{})
gob.Register(model.LineGroup{})
gob.Register(model.LineMessageLog{})
gob.Register(model.LineUser{})
gob.Register(model.OpayDonateList{})
gob.Register(model.TwitchChannel{})
gob.Register(model.YoutubeChannel{})
2018-09-20 17:14:08 +00:00
gob.Register(twitch.TwitchTokenData{})
gob.Register(twitch.UserInfo{})
2018-09-17 16:35:48 +00:00
gob.Register(map[string]interface{}{})
}