package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"git.simanc.com/software/golib/v4/log"
)

type addrFlag []string

func (i *addrFlag) String() string {
	return strings.Join(*i, ",")
}

func (i *addrFlag) Set(value string) error {
	*i = append(*i, value)
	return nil
}

var (
	address addrFlag
	path    string
)

func main() {
	flag.Var(&address, "addr", "")
	flag.StringVar(&path, "path", "./", "Log filepath")
	flag.Parse()

	for _, addr := range address {
		go func(addr string) {
			fmt.Println("Listen on:", addr)
			server, err := log.NewServer(addr, path)
			if err != nil {
				log.Panic("NewServer: %s", err)
			}
			if err = server.ListenAndServe(); err != nil {
				log.Panic("ListenAndServe: %s", err)
			}
		}(addr)
	}

	{
		osSignals := make(chan os.Signal, 1)
		signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
		<-osSignals
	}
}
