2019-12-13 20:08:26 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-28 22:50:14 +02:00
|
|
|
"errors"
|
2022-01-02 13:15:57 +02:00
|
|
|
"flag"
|
2020-01-28 22:50:14 +02:00
|
|
|
"fmt"
|
2019-12-13 20:08:26 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-12-17 22:17:25 +02:00
|
|
|
"path/filepath"
|
2019-12-25 18:38:47 +02:00
|
|
|
"strings"
|
2019-12-13 20:08:26 +02:00
|
|
|
|
2020-01-01 17:58:27 +02:00
|
|
|
"bloat/config"
|
|
|
|
"bloat/renderer"
|
|
|
|
"bloat/service"
|
2020-01-01 17:18:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-02 12:52:15 +02:00
|
|
|
configFiles = []string{"bloat.conf", "/etc/bloat.conf"}
|
2019-12-13 20:08:26 +02:00
|
|
|
)
|
|
|
|
|
2020-01-28 22:50:14 +02:00
|
|
|
func errExit(err error) {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:08:26 +02:00
|
|
|
func main() {
|
2022-01-02 13:15:57 +02:00
|
|
|
configFile := flag.String("f", "", "config file")
|
|
|
|
flag.Parse()
|
2020-01-01 17:18:04 +02:00
|
|
|
|
2022-01-02 13:15:57 +02:00
|
|
|
if len(*configFile) > 0 {
|
|
|
|
configFiles = []string{*configFile}
|
2020-01-01 17:18:04 +02:00
|
|
|
}
|
2022-01-02 12:52:15 +02:00
|
|
|
config, err := config.ParseFiles(configFiles)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !config.IsValid() {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(errors.New("invalid config"))
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 22:49:58 +02:00
|
|
|
templatesGlobPattern := filepath.Join(config.TemplatesPath, "*")
|
|
|
|
renderer, err := renderer.NewRenderer(templatesGlobPattern)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 18:38:47 +02:00
|
|
|
customCSS := config.CustomCSS
|
2020-08-22 09:38:59 +03:00
|
|
|
if len(customCSS) > 0 && !strings.HasPrefix(customCSS, "http://") &&
|
2019-12-25 18:38:47 +02:00
|
|
|
!strings.HasPrefix(customCSS, "https://") {
|
|
|
|
customCSS = "/static/" + customCSS
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:08:26 +02:00
|
|
|
var logger *log.Logger
|
2020-01-28 22:49:58 +02:00
|
|
|
if len(config.LogFile) < 1 {
|
2019-12-13 20:08:26 +02:00
|
|
|
logger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
} else {
|
2020-02-26 12:40:46 +02:00
|
|
|
lf, err := os.OpenFile(config.LogFile,
|
|
|
|
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
defer lf.Close()
|
|
|
|
logger = log.New(lf, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2020-01-28 22:49:58 +02:00
|
|
|
s := service.NewService(config.ClientName, config.ClientScope,
|
2021-03-20 07:12:48 +02:00
|
|
|
config.ClientWebsite, customCSS, config.SingleInstance,
|
2022-11-12 00:20:49 +02:00
|
|
|
config.PostFormats, renderer)
|
2020-11-22 19:29:58 +02:00
|
|
|
handler := service.NewHandler(s, logger, config.StaticDirectory)
|
2019-12-13 20:08:26 +02:00
|
|
|
|
2020-01-28 22:49:58 +02:00
|
|
|
logger.Println("listening on", config.ListenAddress)
|
2019-12-13 20:08:26 +02:00
|
|
|
err = http.ListenAndServe(config.ListenAddress, handler)
|
|
|
|
if err != nil {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
}
|