bacheca/cmd/bacheca-server/main.go

71 lines
1.6 KiB
Go

package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-kit/kit/log"
svc "git.abbiamoundominio.org/hamcha/bacheca"
"git.abbiamoundominio.org/hamcha/bacheca/storage"
boltbackend "git.abbiamoundominio.org/hamcha/bacheca/storage/bolt"
filebackend "git.abbiamoundominio.org/hamcha/bacheca/storage/file"
)
func main() {
httpPort := flag.Int("http.port", 8080, "HTTP listen port")
dbtype := flag.String("storage.type", "bolt", "Storage backend type")
dbfile := flag.String("storage.file", "bacheca.db", "Storage file or path, for file-based backends (bolt)")
flag.Parse()
var backend storage.Backend
switch *dbtype {
case "bolt":
boltdb, err := boltbackend.MakeBoltBackend(*dbfile)
if err != nil {
panic(err)
}
defer boltdb.Close()
backend = boltdb
case "file":
filebackend, err := filebackend.MakeFileBackend(*dbfile)
if err != nil {
panic(err)
}
backend = filebackend
default:
panic("unknown storage backend type (-storage.type), check help for supported backends")
}
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
s := svc.MakeService(backend)
handler := svc.MakeHTTPHandler(s, logger)
//TODO Add auth middleware
errs := make(chan error)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
go func() {
bind := fmt.Sprintf(":%d", *httpPort)
logger.Log("transport", "HTTP", "addr", bind)
errs <- http.ListenAndServe(bind, handler)
}()
logger.Log("exit", <-errs)
}