mirror of
https://gitea.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-23 07:23:00 +00:00
103 lines
2.0 KiB
Go
103 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"bloat/config"
|
|
"bloat/kv"
|
|
"bloat/renderer"
|
|
"bloat/repository"
|
|
"bloat/service"
|
|
"bloat/util"
|
|
)
|
|
|
|
var (
|
|
configFile = "bloat.conf"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().Unix())
|
|
}
|
|
|
|
func main() {
|
|
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)
|
|
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)
|
|
}
|
|
|
|
err = os.Mkdir(config.DatabasePath, 0755)
|
|
if err != nil && !os.IsExist(err) {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
sessionDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "session"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
appDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "app"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
sessionRepo := repository.NewSessionRepository(sessionDB)
|
|
appRepo := repository.NewAppRepository(appDB)
|
|
|
|
customCSS := config.CustomCSS
|
|
if !strings.HasPrefix(customCSS, "http://") &&
|
|
!strings.HasPrefix(customCSS, "https://") {
|
|
customCSS = "/static/" + customCSS
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite,
|
|
customCSS, config.PostFormats, renderer, sessionRepo, appRepo)
|
|
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)
|
|
}
|
|
}
|