2019-12-13 20:08:26 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-28 22:50:14 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-12-13 20:08:26 +02:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"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
|
|
|
"time"
|
|
|
|
|
2020-01-01 17:58:27 +02:00
|
|
|
"bloat/config"
|
|
|
|
"bloat/kv"
|
|
|
|
"bloat/renderer"
|
2020-01-28 19:51:00 +02:00
|
|
|
"bloat/repo"
|
2020-01-01 17:58:27 +02:00
|
|
|
"bloat/service"
|
|
|
|
"bloat/util"
|
2020-01-01 17:18:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFile = "bloat.conf"
|
2019-12-13 20:08:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-01-01 17:18:04 +02:00
|
|
|
opts, _, err := util.Getopts(os.Args, "f:")
|
|
|
|
if err != nil {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2020-01-01 17:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'f':
|
|
|
|
configFile = opt.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := config.ParseFile(configFile)
|
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-17 22:17:25 +02:00
|
|
|
err = os.Mkdir(config.DatabasePath, 0755)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
2020-01-28 22:50:14 +02:00
|
|
|
errExit(err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 22:49:58 +02:00
|
|
|
sessionDBPath := filepath.Join(config.DatabasePath, "session")
|
|
|
|
sessionDB, err := kv.NewDatabse(sessionDBPath)
|
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
|
|
|
}
|
|
|
|
|
2020-01-28 22:49:58 +02:00
|
|
|
appDBPath := filepath.Join(config.DatabasePath, "app")
|
|
|
|
appDB, err := kv.NewDatabse(appDBPath)
|
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
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
sessionRepo := repo.NewSessionRepo(sessionDB)
|
|
|
|
appRepo := repo.NewAppRepo(appDB)
|
2019-12-17 22:17:25 +02:00
|
|
|
|
2019-12-25 18:38:47 +02:00
|
|
|
customCSS := config.CustomCSS
|
|
|
|
if !strings.HasPrefix(customCSS, "http://") &&
|
|
|
|
!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-01-28 22:49:58 +02:00
|
|
|
lf, err := os.Open(config.LogFile)
|
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,
|
|
|
|
config.ClientWebsite, customCSS, config.PostFormats, renderer,
|
|
|
|
sessionRepo, appRepo)
|
2019-12-13 20:08:26 +02:00
|
|
|
s = service.NewAuthService(sessionRepo, appRepo, s)
|
|
|
|
s = service.NewLoggingService(logger, s)
|
|
|
|
handler := service.NewHandler(s, config.StaticDirectory)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|