2019-12-05 09:36:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-09 03:02:49 -05:00
|
|
|
"fmt"
|
2019-12-08 03:59:58 -05:00
|
|
|
"time"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
"github.com/patrickmn/go-cache"
|
2019-12-05 10:42:20 -05:00
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
"github.com/naiba/nezha/cmd/dashboard/controller"
|
|
|
|
"github.com/naiba/nezha/cmd/dashboard/rpc"
|
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-05 09:36:58 -05:00
|
|
|
)
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
func init() {
|
|
|
|
var err error
|
2019-12-09 05:14:31 -05:00
|
|
|
dao.ServerList = make(map[string]*model.Server)
|
2019-12-20 10:58:09 -05:00
|
|
|
dao.Conf = &model.Config{}
|
|
|
|
err = dao.Conf.Read("data/config.yaml")
|
2019-12-08 03:59:58 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dao.DB, err = gorm.Open("sqlite3", "data/sqlite.db")
|
2019-12-05 09:36:58 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-08 10:18:29 -05:00
|
|
|
if dao.Conf.Debug {
|
|
|
|
dao.DB = dao.DB.Debug()
|
|
|
|
}
|
2019-12-08 03:59:58 -05:00
|
|
|
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
|
2019-12-08 10:18:29 -05:00
|
|
|
initDB()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDB() {
|
2019-12-20 10:58:09 -05:00
|
|
|
dao.DB.AutoMigrate(model.Server{}, model.User{})
|
2019-12-09 03:02:49 -05:00
|
|
|
// load cache
|
|
|
|
var servers []model.Server
|
|
|
|
dao.DB.Find(&servers)
|
|
|
|
for _, s := range servers {
|
2019-12-09 05:14:31 -05:00
|
|
|
innerS := s
|
|
|
|
dao.ServerList[fmt.Sprintf("%d", innerS.ID)] = &innerS
|
2019-12-09 03:02:49 -05:00
|
|
|
}
|
2019-12-08 03:59:58 -05:00
|
|
|
}
|
2019-12-05 09:36:58 -05:00
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
func main() {
|
2020-10-24 09:44:03 -04:00
|
|
|
go controller.ServeWeb(dao.Conf.HTTPPort)
|
2019-12-10 05:05:02 -05:00
|
|
|
go rpc.ServeRPC(5555)
|
2019-12-08 03:59:58 -05:00
|
|
|
select {}
|
2019-12-05 09:36:58 -05:00
|
|
|
}
|