2017-11-10 12:39:55 +01:00
|
|
|
package init
|
|
|
|
|
|
|
|
import (
|
2020-05-24 14:23:03 +02:00
|
|
|
"math/rand"
|
2017-11-10 12:39:55 +01:00
|
|
|
"os"
|
2020-05-24 14:23:03 +02:00
|
|
|
"time"
|
2017-11-10 12:39:55 +01:00
|
|
|
|
2020-07-29 17:27:36 +02:00
|
|
|
"fornaxian.tech/pixeldrain_web/webcontroller"
|
2018-06-17 16:15:58 +02:00
|
|
|
"github.com/Fornaxian/config"
|
|
|
|
"github.com/Fornaxian/log"
|
2017-11-10 12:39:55 +01:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2019-12-30 13:00:00 +01:00
|
|
|
// PixelWebConfig contains the Pixeldrain Web UI configuration
|
|
|
|
type PixelWebConfig struct {
|
|
|
|
APIURLExternal string `toml:"api_url_external"`
|
|
|
|
APIURLInternal string `toml:"api_url_internal"`
|
2021-01-12 23:20:32 +01:00
|
|
|
WebsiteAddress string `toml:"website_address"`
|
2019-12-30 13:00:00 +01:00
|
|
|
SessionCookieDomain string `toml:"session_cookie_domain"`
|
|
|
|
ResourceDir string `toml:"resource_dir"`
|
|
|
|
DebugMode bool `toml:"debug_mode"`
|
2021-03-04 17:10:59 +01:00
|
|
|
ProxyAPIRequests bool `toml:"proxy_api_requests"`
|
2019-12-30 13:00:00 +01:00
|
|
|
MaintenanceMode bool `toml:"maintenance_mode"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig is the default configuration for Pixeldrain Web
|
2021-03-04 17:10:59 +01:00
|
|
|
const DefaultConfig = `## Pixeldrain Web UI server configuration
|
|
|
|
|
|
|
|
# Address used in the browser for making requests directly to the API. Can be
|
|
|
|
# relative to the current domain name
|
|
|
|
api_url_external = "/api"
|
|
|
|
|
|
|
|
# Address used to make internal API requests to the backend
|
|
|
|
api_url_internal = "https://pixeldrain.com/api"
|
2019-12-30 13:00:00 +01:00
|
|
|
|
2021-01-12 23:20:32 +01:00
|
|
|
website_address = "https://pixeldrain.com"
|
2021-03-04 17:10:59 +01:00
|
|
|
session_cookie_domain = ""
|
2019-12-30 13:00:00 +01:00
|
|
|
resource_dir = "res"
|
2021-03-04 17:10:59 +01:00
|
|
|
|
|
|
|
# Parse all the templates every time a request comes in
|
|
|
|
debug_mode = true
|
|
|
|
|
|
|
|
# Create proxy listeners to forward all requests made to /api to
|
|
|
|
# api_url_internal
|
|
|
|
proxy_api_requests = true
|
|
|
|
|
|
|
|
# When this is true every request will return a maintainance HTML page
|
2019-12-30 13:00:00 +01:00
|
|
|
maintenance_mode = false
|
|
|
|
`
|
|
|
|
|
2017-11-10 12:39:55 +01:00
|
|
|
// Init initializes the Pixeldrain Web UI controllers
|
2018-08-15 10:11:31 +02:00
|
|
|
func Init(r *httprouter.Router, prefix string, setLogLevel bool) {
|
2021-03-11 18:52:55 +01:00
|
|
|
log.Colours = true
|
2017-11-10 12:39:55 +01:00
|
|
|
log.Info("Starting web UI server (PID %v)", os.Getpid())
|
|
|
|
|
2020-05-24 14:23:03 +02:00
|
|
|
// Seed the RNG
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2019-12-30 13:00:00 +01:00
|
|
|
var webconf = &PixelWebConfig{}
|
2018-06-17 16:15:58 +02:00
|
|
|
var _, err = config.New(
|
2019-12-30 13:00:00 +01:00
|
|
|
DefaultConfig,
|
2018-06-17 16:15:58 +02:00
|
|
|
"",
|
|
|
|
"pdwebconf.toml",
|
|
|
|
webconf,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to load config file: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-11-10 12:39:55 +01:00
|
|
|
|
2018-08-15 10:11:31 +02:00
|
|
|
if !webconf.DebugMode && setLogLevel {
|
2018-08-15 10:04:43 +02:00
|
|
|
log.SetLogLevel(log.LevelInfo)
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:00:00 +01:00
|
|
|
webcontroller.New(
|
|
|
|
r,
|
|
|
|
prefix,
|
|
|
|
webconf.ResourceDir,
|
|
|
|
webconf.APIURLInternal,
|
|
|
|
webconf.APIURLExternal,
|
2021-01-12 23:20:32 +01:00
|
|
|
webconf.WebsiteAddress,
|
2019-12-30 13:00:00 +01:00
|
|
|
webconf.SessionCookieDomain,
|
|
|
|
webconf.MaintenanceMode,
|
|
|
|
webconf.DebugMode,
|
2021-03-04 17:10:59 +01:00
|
|
|
webconf.ProxyAPIRequests,
|
2019-12-30 13:00:00 +01:00
|
|
|
)
|
2017-11-10 12:39:55 +01:00
|
|
|
}
|