Files
fnx_web/init/init.go

73 lines
1.9 KiB
Go

package init
import (
"math/rand"
"os"
"time"
"fornaxian.tech/pixeldrain_web/webcontroller"
"github.com/Fornaxian/config"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
// PixelWebConfig contains the Pixeldrain Web UI configuration
type PixelWebConfig struct {
APIURLExternal string `toml:"api_url_external"`
APIURLInternal string `toml:"api_url_internal"`
WebsiteAddress string `toml:"website_address"`
SessionCookieDomain string `toml:"session_cookie_domain"`
ResourceDir string `toml:"resource_dir"`
DebugMode bool `toml:"debug_mode"`
MaintenanceMode bool `toml:"maintenance_mode"`
}
// DefaultConfig is the default configuration for Pixeldrain Web
const DefaultConfig = `# Pixeldrain Web UI server configuration
api_url_external = "/api" # Used in the web browser
api_url_internal = "http://127.0.0.1:8080" # Used for internal API requests to the pixeldrain server, not visible to users
website_address = "https://pixeldrain.com"
session_cookie_domain = ".pixeldrain.com"
resource_dir = "res"
debug_mode = false
maintenance_mode = false
`
// Init initializes the Pixeldrain Web UI controllers
func Init(r *httprouter.Router, prefix string, setLogLevel bool) {
log.Info("Starting web UI server (PID %v)", os.Getpid())
// Seed the RNG
rand.Seed(time.Now().UnixNano())
var webconf = &PixelWebConfig{}
var _, err = config.New(
DefaultConfig,
"",
"pdwebconf.toml",
webconf,
true,
)
if err != nil {
log.Error("Failed to load config file: %s", err)
os.Exit(1)
}
if !webconf.DebugMode && setLogLevel {
log.SetLogLevel(log.LevelInfo)
}
webcontroller.New(
r,
prefix,
webconf.ResourceDir,
webconf.APIURLInternal,
webconf.APIURLExternal,
webconf.WebsiteAddress,
webconf.SessionCookieDomain,
webconf.MaintenanceMode,
webconf.DebugMode,
)
}