2019-12-13 18:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"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
|
|
|
"time"
|
|
|
|
|
2020-01-01 15:58:27 +00:00
|
|
|
"bloat/config"
|
|
|
|
"bloat/kv"
|
|
|
|
"bloat/renderer"
|
2020-01-28 17:51:00 +00:00
|
|
|
"bloat/repo"
|
2020-01-01 15:58:27 +00:00
|
|
|
"bloat/service"
|
|
|
|
"bloat/util"
|
2020-01-01 15:18:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFile = "bloat.conf"
|
2019-12-13 18:08:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-01-01 15:18:04 +00:00
|
|
|
opts, _, err := util.Getopts(os.Args, "f:")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'f':
|
|
|
|
configFile = opt.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := config.ParseFile(configFile)
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.IsValid() {
|
|
|
|
log.Fatal("invalid config")
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer, err := renderer.NewRenderer(config.TemplatesGlobPattern)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
err = os.Mkdir(config.DatabasePath, 0755)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
2019-12-13 18:08:26 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
sessionDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "session"))
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
appDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "app"))
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-28 17:51:00 +00:00
|
|
|
sessionRepo := repo.NewSessionRepo(sessionDB)
|
|
|
|
appRepo := repo.NewAppRepo(appDB)
|
2019-12-17 20:17:25 +00:00
|
|
|
|
2019-12-25 16:38:47 +00:00
|
|
|
customCSS := config.CustomCSS
|
|
|
|
if !strings.HasPrefix(customCSS, "http://") &&
|
|
|
|
!strings.HasPrefix(customCSS, "https://") {
|
|
|
|
customCSS = "/static/" + customCSS
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:08:26 +00:00
|
|
|
var logger *log.Logger
|
|
|
|
if len(config.Logfile) < 1 {
|
|
|
|
logger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
} else {
|
|
|
|
lf, err := os.Open(config.Logfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer lf.Close()
|
|
|
|
logger = log.New(lf, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:25:29 +00:00
|
|
|
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite,
|
|
|
|
customCSS, config.PostFormats, renderer, sessionRepo, appRepo)
|
2019-12-13 18:08:26 +00:00
|
|
|
s = service.NewAuthService(sessionRepo, appRepo, s)
|
|
|
|
s = service.NewLoggingService(logger, s)
|
|
|
|
handler := service.NewHandler(s, config.StaticDirectory)
|
|
|
|
|
|
|
|
log.Println("listening on", config.ListenAddress)
|
|
|
|
err = http.ListenAndServe(config.ListenAddress, handler)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|