package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/zeevdiukman/zprox/internal/config"
	"github.com/zeevdiukman/zprox/internal/session"
)

func BuildEntryPoint(data *config.EntryPoint, handler http.Handler) {
	h := session.Manager.LoadAndSave(handler)
	if data.TLS.Enabled {
		go func() {
			fmt.Println("Listening at " + data.Address + " with TLS ")
			err := http.ListenAndServeTLS(data.Address, "./z/crt.pem", "./z/key.pem", h)
			if err != nil {
				log.Println(err.Error())
			}
		}()
	} else {
		go func() {
			fmt.Println("Listening at " + data.Address + " non TLS ")

			err := http.ListenAndServe(data.Address, h)
			if err != nil {
				log.Println(err.Error())
			}
		}()
	}
}
