package main

import (
	"log"
	"net/http"
	"strconv"

	"./core/database"
	req "./core/request"
	res "./core/response"
	"./src/lib/auth"
)

func main() {

	initDatabase()
	initAuthService()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		var Errors errorHandler.Errors

		res.SetResponseWriter(w)
		req.SetRequest(r)

		if !database.IsConnected {
			Errors.InternalError("Conexão com a base de dados falhou!")
			res.JSON(Errors)
			return
		}

		Routes.Match(w, r)
	})

	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(SERVER_PORT), nil))
}

func initDatabase() {

	database.Init(
		database.Config{
			DATABASE_HOST,
			DATABASE_PORT,
			DATABASE_USER,
			DATABASE_PASS,
			DATABASE_SCHEMA,
		})
}

func initAuthService() {

	auth.Config(
		auth.ServiceConfig{
			AUTHENTICATION_SERVICE,
		})
}
