package main

import (
	"net/http"
	thttp "github.com/tangs-drm/go-tool/http"
	"github.com/tangs-drm/go-tool/config"
	"github.com/tangs-drm/go-tool/log"
	"github.com/tangs-drm/go-tool/dbm"
	_ "github.com/go-sql-driver/mysql"
	"fmt"
	tdbm "ddpf/model/dbm"
	"os"
	"ddpf/model/session"
)

func main() {
	var err error

	execPath, err := os.Getwd()
	fmt.Println(execPath, err)
	log.Debug("[main] os.Getwd() get %v, %v", execPath, err)

	// 初始化配置文件
	var conf = "src/config.conf"
	var config = config.NewConfig()
	err = config.Config(conf)
	fmt.Println(os.Getwd())
	if err != nil {
		fmt.Println(err)
		log.Error("[main] read config with conf(%v) error ->(%v)", conf, err)
		return
	}
	config.Print()

	// 初始化数据库
	var db_url = config.String("db_url")
	_, err = dbm.Default(db_url)
	if err != nil {
		fmt.Println(err)
		log.Error("[main] init db by url(%v) error ->(%v)", db_url, err)
		return
	}
	// 初始化数据库的表,索引等
	var mysqlPath = "src/order.sql"
	err = tdbm.Init(mysqlPath)
	if err != nil {
		fmt.Println(err)
		log.Error("[main] init sql command with path(%v) error ->(%v)", mysqlPath, err)
		return
	}
	log.Debug("main init db success")

	//filter := login.NewFilter()
	mux := thttp.NewServerMux()

	// 过滤器
	mux.FilterFunc("/usr/", session.UserFilterFunc)

	// 初始化http请求路由

	var port = config.String("listen_port")
	log.Debug("main start listen on port %v", port)

	http.ListenAndServe(port, mux)
}