package main

import (
	"fmt"
	"github.com/labstack/echo"
	"log"
	"log/syslog"
	"net/http"
	"os"
)

var _appversion string = "0.1"
var _appname string = "ZiTel-Sysbo-WS"

func audit(txt string) {

	syslogger, err := syslog.New(syslog.LOG_INFO, _appname)
	if err != nil {
		log.Fatalln(err)
	}
	log.SetOutput(syslogger)
	log.Println(txt)

}

var RealIP string

func extractIP(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		RealIP = c.RealIP()
		audit("Recieved request from: " + RealIP)
		return next(c)
	}
}
func main() {
	if len(os.Args) != 3 {
		fmt.Println("Wrong Usage:\n\t ./CMD IP Port")
		audit("Application in the wrong way")
		os.Exit(1)
	}
	echoHandler := echo.New()
	echoHandler.Use(extractIP)
	audit("Application " + _appname + " (" + _appversion + ") Started by " + os.Getenv("USER"))
	echoHandler.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})

	h := &handler{}
	echoHandler.POST("/login", h.login)

	echoHandler.GET("/private", h.private, isLoggedIn)

	echoHandler.GET("/admin", h.private, isLoggedIn, isAdmin)

	//echoHandler.POST("/token", h.token)

	echoHandler.Logger.Fatal(echoHandler.Start(os.Args[1] + ":" + os.Args[2]))
}
