2019-12-13 18:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-28 20:50:14 +00:00
|
|
|
"errors"
|
2022-01-02 11:15:57 +00:00
|
|
|
"flag"
|
2020-01-28 20:50:14 +00:00
|
|
|
"fmt"
|
2019-12-13 18:08:26 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-12-17 20:17:25 +00:00
|
|
|
"path/filepath"
|
2019-12-25 16:38:47 +00:00
|
|
|
"strings"
|
2019-12-13 18:08:26 +00:00
|
|
|
|
2020-01-01 15:58:27 +00:00
|
|
|
"bloat/config"
|
|
|
|
"bloat/renderer"
|
|
|
|
"bloat/service"
|
2020-01-01 15:18:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-02 10:52:15 +00:00
|
|
|
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() {
|
2022-01-02 11:15:57 +00:00
|
|
|
configFile := flag.String("f", "", "config file")
|
2023-09-09 06:30:38 +00:00
|
|
|
verbose := flag.Bool("v", false, "verbose mode")
|
2022-01-02 11:15:57 +00:00
|
|
|
flag.Parse()
|
2020-01-01 15:18:04 +00:00
|
|
|
|
2022-01-02 11:15:57 +00:00
|
|
|
if len(*configFile) > 0 {
|
|
|
|
configFiles = []string{*configFile}
|
2020-01-01 15:18:04 +00:00
|
|
|
}
|
2022-01-02 10:52:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-25 16:38:47 +00:00
|
|
|
customCSS := config.CustomCSS
|
2020-08-22 06:38:59 +00:00
|
|
|
if len(customCSS) > 0 && !strings.HasPrefix(customCSS, "http://") &&
|
2019-12-25 16:38:47 +00:00
|
|
|
!strings.HasPrefix(customCSS, "https://") {
|
|
|
|
customCSS = "/static/" + customCSS
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:49:58 +00:00
|
|
|
s := service.NewService(config.ClientName, config.ClientScope,
|
2021-03-20 05:12:48 +00:00
|
|
|
config.ClientWebsite, customCSS, config.SingleInstance,
|
2022-10-25 13:40:49 +00:00
|
|
|
config.PostFormats, renderer)
|
2023-09-09 06:30:38 +00:00
|
|
|
handler := service.NewHandler(s, *verbose, config.StaticDirectory)
|
2019-12-13 18:08:26 +00:00
|
|
|
|
2023-09-09 06:30:38 +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
|
|
|
}
|
|
|
|
}
|