package hub

import (
	"flag"
	"log"
	"net/http"
	"os"

	"git.crumpington.com/lib/go/webutil"
)

func Main() {
	log.SetFlags(0)

	conf := Config{}
	flag.StringVar(&conf.RootDir, "root-dir", "", "[REQUIRED] Root directory.")
	flag.StringVar(&conf.ListenAddr, "listen", "", "[REQUIRED] Listen address.")
	flag.BoolVar(&conf.Insecure, "insecure", false, "Don't use secure cookies.")

	flag.Parse()

	if conf.RootDir == "" || conf.ListenAddr == "" {
		flag.Usage()
		os.Exit(1)
	}

	app, err := NewApp(conf)
	if err != nil {
		log.Fatal(err)
	}

	srv := &http.Server{
		Addr:    conf.ListenAddr,
		Handler: app.mux,
	}

	log.Fatal(webutil.ListenAndServe(srv))
}
