package main

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

	"metis/pkg/service"
)

var listenAndServe = http.ListenAndServe

func serveCmd(args []string) {
	fs := flag.NewFlagSet("serve", flag.ExitOnError)
	bindAddr := fs.String("bind", "", "override bind address")
	fs.Parse(args)

	settings := service.FromEnv()
	if *bindAddr != "" {
		settings.BindAddr = *bindAddr
	}
	app, err := service.NewApp(settings)
	if err != nil {
		fatalf("init service: %v", err)
	}
	log.Printf("metis listening on %s", settings.BindAddr)
	if err := listenAndServe(settings.BindAddr, app.Handler()); err != nil {
		fatalf("serve: %v", err)
	}
}
