mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-19 12:36:23 -07:00
Start setting up gochan-installer for providing a web interface for setting up configuration
This commit is contained in:
parent
fbee82edee
commit
772bd265f9
10 changed files with 302 additions and 68 deletions
|
@ -41,6 +41,8 @@ var (
|
|||
ErrNoMatchingEmbedHandler = errors.New("no matching handler for the embed URL")
|
||||
)
|
||||
|
||||
type InitialSetupStatus int
|
||||
|
||||
type GochanConfig struct {
|
||||
SystemCriticalConfig
|
||||
SiteConfig
|
||||
|
@ -863,6 +865,14 @@ func (em *EmbedMatcher) HasThumbnail() bool {
|
|||
return em.ThumbnailURLTemplate != ""
|
||||
}
|
||||
|
||||
func GetInitialSetupStatus() InitialSetupStatus {
|
||||
return initialSetupStatus
|
||||
}
|
||||
|
||||
func GetDefaultConfig() *GochanConfig {
|
||||
return defaultGochanConfig
|
||||
}
|
||||
|
||||
func WriteConfig() error {
|
||||
return cfg.Write()
|
||||
}
|
||||
|
|
|
@ -44,21 +44,6 @@ func SetRandomSeed(seed string) {
|
|||
cfg.RandomSeed = seed
|
||||
}
|
||||
|
||||
// SetSystemCriticalConfig sets system critical configuration values in testing. It will panic if it is not run in a
|
||||
// test environment
|
||||
func SetSystemCriticalConfig(systemCritical *SystemCriticalConfig) {
|
||||
testutil.PanicIfNotTest()
|
||||
setDefaultCfgIfNotSet()
|
||||
cfg.SystemCriticalConfig = *systemCritical
|
||||
}
|
||||
|
||||
// SetSiteConfig sets the site configuration values in testing. It will panic if it is not run in a test environment
|
||||
func SetSiteConfig(siteConfig *SiteConfig) {
|
||||
testutil.PanicIfNotTest()
|
||||
setDefaultCfgIfNotSet()
|
||||
cfg.SiteConfig = *siteConfig
|
||||
}
|
||||
|
||||
// SetBoardConfig applies the configuration to the given board. It will panic if it is not run in a test environment
|
||||
func SetBoardConfig(board string, boardCfg *BoardConfig) error {
|
||||
testutil.PanicIfNotTest()
|
||||
|
|
|
@ -17,13 +17,20 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
InitialSetupStatusUnknown InitialSetupStatus = iota
|
||||
InitialSetupNotStarted
|
||||
InitialSetupComplete
|
||||
|
||||
DirFileMode fs.FileMode = 0775
|
||||
NormalFileMode fs.FileMode = 0664
|
||||
)
|
||||
|
||||
var (
|
||||
uid int
|
||||
gid int
|
||||
uid int
|
||||
gid int
|
||||
standardConfigSearchPaths = []string{"gochan.json", "/usr/local/etc/gochan/gochan.json", "/etc/gochan/gochan.json"}
|
||||
|
||||
initialSetupStatus InitialSetupStatus = InitialSetupStatusUnknown
|
||||
)
|
||||
|
||||
// MissingField represents a field missing from the configuration file
|
||||
|
@ -48,6 +55,15 @@ func (iv *InvalidValueError) Error() string {
|
|||
return str
|
||||
}
|
||||
|
||||
// GetGochanJSONPath returns the location of gochan.json, searching in the working directory,
|
||||
// /usr/local/etc/gochan, and /etc/gochan in that order. If it is not found, it returns an empty string.
|
||||
func GetGochanJSONPath() string {
|
||||
if cfgPath != "" {
|
||||
return cfgPath
|
||||
}
|
||||
return gcutil.FindResource(standardConfigSearchPaths...)
|
||||
}
|
||||
|
||||
// GetUser returns the IDs of the user and group gochan should be acting as
|
||||
// when creating files. If they are 0, it is using the current user
|
||||
func GetUser() (int, int) {
|
||||
|
@ -72,7 +88,19 @@ func TakeOwnershipOfFile(f *os.File) error {
|
|||
return f.Chown(uid, gid)
|
||||
}
|
||||
|
||||
func loadConfig(searchPaths ...string) (err error) {
|
||||
// SetSystemCriticalConfig sets system critical configuration values
|
||||
func SetSystemCriticalConfig(systemCritical *SystemCriticalConfig) {
|
||||
setDefaultCfgIfNotSet()
|
||||
cfg.SystemCriticalConfig = *systemCritical
|
||||
}
|
||||
|
||||
// SetSiteConfig sets the site configuration values
|
||||
func SetSiteConfig(siteConfig *SiteConfig) {
|
||||
setDefaultCfgIfNotSet()
|
||||
cfg.SiteConfig = *siteConfig
|
||||
}
|
||||
|
||||
func loadConfig() (err error) {
|
||||
cfg = defaultGochanConfig
|
||||
if testing.Testing() {
|
||||
// create a dummy config for testing if we're using go test
|
||||
|
@ -97,7 +125,7 @@ func loadConfig(searchPaths ...string) (err error) {
|
|||
}
|
||||
return
|
||||
}
|
||||
cfgPath = gcutil.FindResource(searchPaths...)
|
||||
cfgPath = gcutil.FindResource(standardConfigSearchPaths...)
|
||||
if cfgPath == "" {
|
||||
return errors.New("gochan.json not found")
|
||||
}
|
||||
|
@ -121,11 +149,8 @@ func loadConfig(searchPaths ...string) (err error) {
|
|||
|
||||
// InitConfig loads and parses gochan.json on startup and verifies its contents
|
||||
func InitConfig() (err error) {
|
||||
var searchPaths []string
|
||||
if !testing.Testing() {
|
||||
searchPaths = []string{"gochan.json", "/usr/local/etc/gochan/gochan.json", "/etc/gochan/gochan.json"}
|
||||
}
|
||||
if err = loadConfig(searchPaths...); err != nil {
|
||||
initialSetupStatus = InitialSetupNotStarted
|
||||
if err = loadConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -188,7 +213,7 @@ func InitConfig() (err error) {
|
|||
|
||||
_, zoneOffset := time.Now().Zone()
|
||||
cfg.TimeZone = zoneOffset / 60 / 60
|
||||
|
||||
initialSetupStatus = InitialSetupComplete
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -109,7 +109,11 @@ func sectionBoardsTmplFunc(sectionID int) []gcsql.Board {
|
|||
|
||||
func init() {
|
||||
events.RegisterEvent([]string{"reset-boards-sections"}, func(_ string, _ ...any) error {
|
||||
return gcsql.ResetBoardSectionArrays()
|
||||
if config.GetSQLConfig().DBhost != "" {
|
||||
// Only reset if SQL is configured
|
||||
return gcsql.ResetBoardSectionArrays()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
gctemplates.AddTemplateFuncs(template.FuncMap{
|
||||
"banMask": banMaskTmplFunc,
|
||||
|
|
|
@ -20,10 +20,13 @@ type templateRef interface {
|
|||
|
||||
// InitMinifier sets up the HTML/JS/JSON minifier if enabled in gochan.json
|
||||
func InitMinifier() {
|
||||
siteConfig := config.GetSiteConfig()
|
||||
if !siteConfig.MinifyHTML && !siteConfig.MinifyJS {
|
||||
return
|
||||
var siteConfig *config.SiteConfig
|
||||
if config.GetInitialSetupStatus() == config.InitialSetupComplete {
|
||||
siteConfig = config.GetSiteConfig()
|
||||
} else {
|
||||
siteConfig = &config.GetDefaultConfig().SiteConfig
|
||||
}
|
||||
|
||||
minifier = minify.New()
|
||||
if siteConfig.MinifyHTML {
|
||||
minifier.AddFunc("text/html", minifyHTML.Minify)
|
||||
|
@ -40,6 +43,9 @@ func canMinify(mediaType string) (minify bool) {
|
|||
InitMinifier()
|
||||
}
|
||||
}()
|
||||
if config.GetInitialSetupStatus() != config.InitialSetupComplete {
|
||||
return true
|
||||
}
|
||||
siteConfig := config.GetSiteConfig()
|
||||
if mediaType == "text/html" && siteConfig.MinifyHTML {
|
||||
return true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue