package main

import (
	"context"
	"log"
	"os"
	"os/signal"
	"sync"
)

var localAddr = "0.0.0.0:8084"
var externalAddr = "https://localhost:8084"

func main() {
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt)
	wg := &sync.WaitGroup{}

	if os.Getenv("LOCAL_ADDR") != "" {
		localAddr = os.Getenv("LOCAL_ADDR")
	}

	if os.Getenv("EXT_ADDR") != "" {
		externalAddr = os.Getenv("EXT_ADDR")
	}

	srv := NewServer(localAddr)
	srv.LoadData("./data/data.db")

	go func() {
		wg.Add(1)
		srv.ListenAndServe()
	}()

	<-stop

	go func() {
		log.Println("Shutdown server...")
		srv.Shutdown(context.Background())
		wg.Done()
		log.Println("Server.shutdown...")
	}()
	wg.Wait()
}
