package main

import (
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
	_ "git.kingpenguin.tk/chteufleur/datahouse.git/models/temperature"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/user"
	_ "git.kingpenguin.tk/chteufleur/datahouse.git/routers"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

var (
	log = logs.NewLogger(10000)
)

func init() {
	log.SetLogger("console", "")

	orm.RegisterDriver("mysql", orm.DR_MySQL)
	url := database.UserDB + ":" + database.PwdDB + "@/" + database.DataBase + "?charset=utf8"
	err := orm.RegisterDataBase(database.Alias, "mysql", url)
	if err != nil {
		log.Error("Failed to register database", err)
	}

	force := false
	verbose := true
	err = orm.RunSyncdb(database.Alias, force, verbose)
	if err != nil {
		log.Error("Failed to initialize database", err)
	}
}

func main() {

	if !user.IsUserExist("admin") {
		user.AddUser("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
	}

	beego.Run()
}
