package main

import (
	"git.markbailey.dev/cerbervs/ptpp/app"
	"git.markbailey.dev/cerbervs/ptpp/app/routing"
	"git.markbailey.dev/cerbervs/ptpp/app/server"
	"net/http"
	"os"
	"strconv"
	"time"

	_ "git.markbailey.dev/cerbervs/ptpp/app/session"
)

func main() {
	const (
		addr     = "0.0.0.0"
		prodPort = 8080
		devPort  = 8080
	)

	r := routing.NewRouter()
    mux := r.RegisterRoutes()

	var port int
	if os.Getenv("HTMX_APP_ENV") == "production" {
		port = prodPort
	} else {
		port = devPort
	}

	s := server.Server{
		Addr: addr,
		Server: http.Server{
			Addr:                         addr + ":" + strconv.Itoa(port),
			Handler:                      mux,
			DisableGeneralOptionsHandler: false,
			TLSConfig:                    nil,
			ReadTimeout:                  5 * time.Second,
			ReadHeaderTimeout:            0,
			WriteTimeout:                 5 * time.Second,
			IdleTimeout:                  0,
			MaxHeaderBytes:               0,
			TLSNextProto:                 nil,
			ConnState:                    nil,
			ErrorLog:                     nil,
			BaseContext:                  nil,
			ConnContext:                  nil,
		},
		Port: port,
	}

	a := app.App{
		Router: r,
		Server: &s,
	}

	a.Serve()
}
