package main

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

	"./config"
	"./core/database"
	req "./core/request"
	res "./core/response"
	"./routes"
	errorHandler "./src/lib/error"
)

func main() {
	dbConfig := database.Config{
		config.DATABASE_HOST,
		config.DATABASE_PORT,
		config.DATABASE_USER,
		config.DATABASE_PASS,
		config.DATABASE_SCHEMA,
	}

	database.Init(dbConfig)

	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.Routes.Match(w, r)
	})

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