package main

import (
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"imuslab.com/arozos/ReverseProxy/mod/aroz"
	"imuslab.com/arozos/ReverseProxy/mod/database"
	"imuslab.com/arozos/ReverseProxy/mod/tlscert"
)

var (
	handler        *aroz.ArozHandler
	sysdb          *database.Database
	tlsCertManager *tlscert.Manager
)

//Kill signal handler. Do something before the system the core terminate.
func SetupCloseHandler() {
	c := make(chan os.Signal, 2)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-c
		log.Println("\r- Shutting down ReverseProxy")

		sysdb.Close()

		os.Exit(0)
	}()
}

func main() {
	//Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
	handler = aroz.HandleFlagParse(aroz.ServiceInfo{
		Name:        "ReverseProxy",
		Desc:        "Basic reverse proxy listener",
		Group:       "Network",
		IconPath:    "reverseproxy/img/small_icon.png",
		Version:     "0.3",
		StartDir:    "reverseproxy/index.html",
		SupportFW:   true,
		LaunchFWDir: "reverseproxy/index.html",
		SupportEmb:  false,
		InitFWSize:  []int{1080, 580},
	})

	//Register the standard web services urls
	fs := http.FileServer(http.Dir("./web"))

	http.Handle("/", fs)
	initAPIs()
	SetupCloseHandler()

	//Create database
	db, err := database.NewDatabase("sys.db", false)
	if err != nil {
		log.Fatal(err)
	}
	sysdb = db

	//Create tables for the database
	sysdb.NewTable("settings")

	//Create a TLS certificate manager
	tlsCertManager, _ = tlscert.NewManager("./certs")

	//Start the reverse proxy server in go routine
	go func() {
		ReverseProxtInit()
	}()

	time.Sleep(300 * time.Millisecond)
	//Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
	log.Println("ReverseProxy started. Visit control panel at http://localhost" + handler.Port)
	err = http.ListenAndServe(handler.Port, nil)

	if err != nil {
		log.Fatal(err)
	}

}
