2019-12-13 18:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-28 20:50:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-12-13 18:08:26 +00:00
|
|
|
"log"
|
2020-10-30 17:07:06 +00:00
|
|
|
"net"
|
2019-12-13 18:08:26 +00:00
|
|
|
"net/http"
|
2021-03-09 21:24:13 +00:00
|
|
|
"net/url"
|
2019-12-13 18:08:26 +00:00
|
|
|
"os"
|
2019-12-17 20:17:25 +00:00
|
|
|
"path/filepath"
|
2019-12-25 16:38:47 +00:00
|
|
|
"strings"
|
2020-10-30 17:07:06 +00:00
|
|
|
"time"
|
2019-12-13 18:08:26 +00:00
|
|
|
|
2020-01-01 15:58:27 +00:00
|
|
|
"bloat/config"
|
|
|
|
"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 (
|
2020-01-31 18:18:31 +00:00
|
|
|
configFile = "/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)
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:52:03 +00:00
|
|
|
func setupHttp(proxyUrl *string) {
|
2020-10-30 17:07:06 +00:00
|
|
|
tr := http.DefaultTransport.(*http.Transport)
|
|
|
|
tr.MaxIdleConnsPerHost = 30
|
|
|
|
tr.MaxIdleConns = 300
|
|
|
|
tr.ForceAttemptHTTP2 = false
|
|
|
|
tr.DialContext = (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 3 * time.Minute,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext
|
2021-04-03 00:52:03 +00:00
|
|
|
if proxyUrl != nil {
|
|
|
|
proxyUrl, err := url.Parse(*proxyUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
tr.Proxy = http.ProxyURL(proxyUrl)
|
2021-03-09 21:24:13 +00:00
|
|
|
}
|
2020-10-30 17:07:06 +00:00
|
|
|
client := http.DefaultClient
|
|
|
|
client.Transport = tr
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:08:26 +00:00
|
|
|
func main() {
|
2020-01-01 15:18:04 +00:00
|
|
|
opts, _, err := util.Getopts(os.Args, "f:")
|
|
|
|
if err != nil {
|
2020-01-28 20:50:14 +00:00
|
|
|
errExit(err)
|
2020-01-01 15:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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-17 20:17:25 +00:00
|
|
|
err = os.Mkdir(config.DatabasePath, 0755)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
2020-01-28 20:50:14 +00:00
|
|
|
errExit(err)
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 20:49:58 +00:00
|
|
|
sessionDBPath := filepath.Join(config.DatabasePath, "session")
|
2020-11-22 17:29:58 +00:00
|
|
|
sessionDB, err := util.NewDatabse(sessionDBPath)
|
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
|
|
|
}
|
|
|
|
|
2020-01-28 20:49:58 +00:00
|
|
|
appDBPath := filepath.Join(config.DatabasePath, "app")
|
2020-11-22 17:29:58 +00:00
|
|
|
appDB, err := util.NewDatabse(appDBPath)
|
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
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:08:26 +00:00
|
|
|
var logger *log.Logger
|
2020-01-28 20:49:58 +00:00
|
|
|
if len(config.LogFile) < 1 {
|
2019-12-13 18:08:26 +00:00
|
|
|
logger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
} else {
|
2020-02-26 10:40:46 +00:00
|
|
|
lf, err := os.OpenFile(config.LogFile,
|
|
|
|
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
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
|
|
|
}
|
|
|
|
defer lf.Close()
|
|
|
|
logger = log.New(lf, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:52:03 +00:00
|
|
|
setupHttp(config.ProxyUrl)
|
2020-10-30 17:07:06 +00:00
|
|
|
|
2020-01-28 20:49:58 +00:00
|
|
|
s := service.NewService(config.ClientName, config.ClientScope,
|
|
|
|
config.ClientWebsite, customCSS, config.PostFormats, renderer,
|
2020-04-19 08:18:36 +00:00
|
|
|
sessionRepo, appRepo, config.SingleInstance)
|
2020-11-22 17:29:58 +00:00
|
|
|
handler := service.NewHandler(s, logger, config.StaticDirectory)
|
2019-12-13 18:08:26 +00:00
|
|
|
|
2020-01-28 20:49:58 +00:00
|
|
|
logger.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
|
|
|
}
|
|
|
|
}
|