bloat/main.go

60 lines
1.2 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package main
import (
2020-01-28 20:50:14 +00:00
"errors"
"flag"
2020-01-28 20:50:14 +00:00
"fmt"
2019-12-13 18:08:26 +00:00
"log"
"net/http"
"os"
"path/filepath"
2019-12-13 18:08:26 +00:00
2020-01-01 15:58:27 +00:00
"bloat/config"
"bloat/renderer"
"bloat/service"
)
var (
configFiles = []string{"bloat.conf", "/etc/bloat.conf"}
2019-12-13 18:08:26 +00:00
)
2020-01-28 20:50:14 +00:00
func errExit(err error) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
2019-12-13 18:08:26 +00:00
func main() {
configFile := flag.String("f", "", "config file")
verbose := flag.Bool("v", false, "verbose mode")
flag.Parse()
if len(*configFile) > 0 {
configFiles = []string{*configFile}
}
config, err := config.ParseFiles(configFiles)
2019-12-13 18:08:26 +00:00
if err != nil {
2020-01-28 20:50:14 +00:00
errExit(err)
2019-12-13 18:08:26 +00:00
}
if !config.IsValid() {
2020-01-28 20:50:14 +00:00
errExit(errors.New("invalid config"))
2019-12-13 18:08:26 +00:00
}
2020-01-28 20:49:58 +00:00
templatesGlobPattern := filepath.Join(config.TemplatesPath, "*")
renderer, err := renderer.NewRenderer(templatesGlobPattern)
2019-12-13 18:08:26 +00:00
if err != nil {
2020-01-28 20:50:14 +00:00
errExit(err)
2019-12-13 18:08:26 +00:00
}
2020-01-28 20:49:58 +00:00
s := service.NewService(config.ClientName, config.ClientScope,
config.ClientWebsite, config.CustomCSS, config.SingleInstance,
config.PostFormats, renderer)
handler := service.NewHandler(s, *verbose, config.StaticDirectory)
2019-12-13 18:08:26 +00:00
log.Println("listening on", config.ListenAddress)
2019-12-13 18:08:26 +00:00
err = http.ListenAndServe(config.ListenAddress, handler)
if err != nil {
2020-01-28 20:50:14 +00:00
errExit(err)
2019-12-13 18:08:26 +00:00
}
}