package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"net/http"
)

var db *sql.DB

func main() {
	var cfg AppConfig = loadConfig("./config.yaml")

	var err error
	db, err = sql.Open("sqlite3", cfg.DataBasePath)
	if err != nil {
		log.Fatalf("error while opening db(%v) - %v", cfg.DataBasePath, err)
	}
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/admin_login", adminLoginHandler)

	var port = cfg.ServerPort
	fmt.Printf("> Starting %v server at port %v\n", cfg.Brand, port)
	hostexpose_info := ""
	var host string
	if cfg.HostExpose {
		host = "0.0.0.0"
		hostexpose_info = "ENABLED (public)"
	} else { 
		host = "127.0.0.1"
		hostexpose_info = "DISABLED (local)"
	}
	fmt.Printf("> Host Expose: %v\n", hostexpose_info)

	err = http.ListenAndServe(fmt.Sprintf("%v:%d", host, port) /*host:port*/, nil)
	if err != nil {
		fmt.Printf("failed running %v server - %v", cfg.Brand, err)

	}
}
