package main

import (
	"filesdk"
	"fmt"
	"sparrow/pkg/generator"
	"sparrow/pkg/server"
	"sparrow/services/knowoapi/model"

	"github.com/kataras/iris/v12"
)

const (
	host = "http://192.168.0.62:18199"
)

func main() {

	err := server.Init("knowoapi")
	if err != nil {
		server.Log.Fatal(err)
		return
	}
	app := newApp()
	// register a http handler
	err = server.RegisterHTTPHandler(app)
	if err != nil {
		server.Log.Errorf("RegisterHTTPHandler Error: %s", err)
		return
	}
	// go
	err = server.Run()
	if err != nil {
		server.Log.Fatal(err)
	}
}

func newApp() *iris.Application {
	app := iris.New()
	iris.WithoutBodyConsumptionOnUnmarshal(app)
	app.AllowMethods(iris.MethodOptions)
	registerErrors(app)
	db, err := getDB()
	if err != nil {
		fmt.Print(err)
	}
	models := new(model.All).Init(db)
	gen, err := generator.NewKeyGenerator(*confAESKey)
	if err != nil {
		fmt.Print(err)
	}
	initFileSDK()
	//注册路由
	registerRouters(app, models, gen)
	app.Build()
	return app
}

func initFileSDK() {
	fileConfig := filesdk.Config{
		Host: host,
	}
	fileConfig.Init()
}
