Skip to content

Commit

Permalink
Remove "Verbosity" config field and rename "DebugMode" to "Verbose" f…
Browse files Browse the repository at this point in the history
…or enabling command line output on errors
  • Loading branch information
Eggbertx committed Dec 28, 2023
1 parent 138b41b commit 8e95439
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 26 deletions.
17 changes: 13 additions & 4 deletions cmd/gochan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ func main() {

fmt.Printf("Starting gochan v%s\n", versionStr)
config.InitConfig(versionStr)
config.SetVerbose(true)

uid, gid := config.GetUser()
systemCritical := config.GetSystemCriticalConfig()

err := gcplugin.LoadPlugins(systemCritical.Plugins)
err := gcutil.InitLogs(systemCritical.LogDir, true, uid, gid)
if err != nil {
fmt.Println("Error opening logs:", err.Error())
os.Exit(1)
}

if err = gcplugin.LoadPlugins(systemCritical.Plugins); err != nil {
gcutil.LogFatal().Err(err).Msg("failed loading plugins")
}

Expand All @@ -59,14 +65,13 @@ func main() {
Msg("Connected to database")

if err = gcsql.CheckAndInitializeDatabase(systemCritical.DBtype); err != nil {
fmt.Println("Failed to initialize the database:", err.Error())
gcutil.LogFatal().Err(err).Msg("Failed to initialize the database")
}
events.TriggerEvent("db-initialized")
parseCommandLine()
serverutil.InitMinifier()
// posting.InitGeoIP()
// posting.InitCaptcha()
posting.InitCaptcha()

if err = gctemplates.InitTemplates(); err != nil {
fmt.Println("Failed initializing templates:", err.Error())
Expand All @@ -85,6 +90,10 @@ func main() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
posting.InitPosting()
if err = gcutil.InitLogs(systemCritical.LogDir, systemCritical.Verbose, uid, gid); err != nil {
fmt.Println("Error opening logs:", err.Error())
os.Exit(1)
}
go initServer()
<-sc
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gochan/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func initServer() {

listener, err := net.Listen("tcp", systemCritical.ListenIP+":"+strconv.Itoa(systemCritical.Port))
if err != nil {
if !systemCritical.DebugMode {
if !systemCritical.Verbose {
fmt.Printf("Failed listening on %s:%d: %s", systemCritical.ListenIP, systemCritical.Port, err.Error())
}
gcutil.LogFatal().Err(err).Caller().
Expand Down Expand Up @@ -56,7 +56,7 @@ func initServer() {
}

if err != nil {
if !systemCritical.DebugMode {
if !systemCritical.Verbose {
fmt.Println("Error initializing server:", err.Error())
}
gcutil.Logger().Fatal().
Expand Down
11 changes: 7 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ type SystemCriticalConfig struct {
DBpassword string
DBprefix string

DebugMode bool
Verbose bool `json:"DebugMode"`
RandomSeed string
Version *GochanVersion `json:"-"`
TimeZone int `json:"-"`
Expand All @@ -185,7 +185,6 @@ type SiteConfig struct {

MaxRecentPosts int
RecentPostsWithNoFile bool
Verbosity int
EnableAppeals bool
MaxLogDays int

Expand Down Expand Up @@ -374,8 +373,12 @@ func DeleteBoardConfig(dir string) {
delete(boardConfigs, dir)
}

func GetDebugMode() bool {
return cfg.testing || cfg.SystemCriticalConfig.DebugMode
func VerboseMode() bool {
return cfg.testing || cfg.SystemCriticalConfig.Verbose
}

func SetVerbose(verbose bool) {
cfg.Verbose = verbose
}

func GetVersion() *GochanVersion {
Expand Down
1 change: 0 additions & 1 deletion pkg/config/jsonvars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const (
"GeoIPDBlocation": "/usr/share/GeoIP/GeoIP.dat",
"MaxRecentPosts": 12,
"RecentPostsWithNoFile": false,
"Verbosity": 0,
"EnableAppeals": true,
"MaxLogDays": 14,
"_comment": "Set RandomSeed to a (preferrably large) string of letters and numbers",
Expand Down
13 changes: 7 additions & 6 deletions pkg/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func (iv *InvalidValueError) Error() string {
return str
}

// 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) {
return uid, gid
}

func TakeOwnership(fp string) (err error) {
if runtime.GOOS == "windows" || fp == "" || cfg.Username == "" {
// Chown returns an error in Windows so skip it, also skip if Username isn't set
Expand Down Expand Up @@ -78,7 +84,6 @@ func InitConfig(versionStr string) {
cfg.ListenIP = "127.0.0.1"
cfg.Port = 8080
cfg.UseFastCGI = true
cfg.DebugMode = true
cfg.testing = true
cfg.TemplateDir = "templates"
cfg.DBtype = "sqlite3"
Expand All @@ -89,7 +94,7 @@ func InitConfig(versionStr string) {
cfg.RandomSeed = "test"
cfg.Version = ParseVersion(versionStr)
cfg.SiteSlogan = "Gochan testing"
cfg.Verbosity = 1
cfg.Verbose = true
cfg.Captcha.OnlyNeededForThreads = true
cfg.Cooldowns = BoardCooldowns{0, 0, 0}
cfg.BanColors = []string{
Expand Down Expand Up @@ -161,10 +166,6 @@ func InitConfig(versionStr string) {
}

cfg.LogDir = gcutil.FindResource(cfg.LogDir, "log", "/var/log/gochan/")
if err = gcutil.InitLogs(cfg.LogDir, cfg.DebugMode, uid, gid); err != nil {
fmt.Println("Error opening logs:", err.Error())
os.Exit(1)
}

if cfg.Port == 0 {
cfg.Port = 80
Expand Down
2 changes: 1 addition & 1 deletion pkg/gcsql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func sqlVersionError(err error, dbDriver string, query *string) error {
return err
}
}
if config.GetSystemCriticalConfig().DebugMode {
if config.GetSystemCriticalConfig().Verbose {
return fmt.Errorf(UnsupportedSQLVersionMsg+"\nQuery: "+*query, errText)
}
return fmt.Errorf(UnsupportedSQLVersionMsg, errText)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gcsql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SetupSQLString(query string, dbConn *GCDB) (string, error) {
}
prepared = strings.Join(arr, "")
case "sqlmock":
if config.GetDebugMode() {
if config.VerboseMode() {
prepared = query
break
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/gcutil/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func LogDiscard(events ...*zerolog.Event) {

func initLog(logPath string, debug bool) (err error) {
if logFile != nil {
// log file already initialized, skip
return nil
// log already initialized
if err = logFile.Close(); err != nil {
return err
}
}
logFile, err = os.OpenFile(logPath, logFlags, logFileMode) // skipcq: GSC-G302
if err != nil {
Expand All @@ -77,8 +79,10 @@ func initLog(logPath string, debug bool) (err error) {

func initAccessLog(logPath string) (err error) {
if accessFile != nil {
// access log already initialized, skip
return nil
// access log already initialized, close it first before reopening
if err = accessFile.Close(); err != nil {
return err
}
}
accessFile, err = os.OpenFile(logPath, logFlags, logFileMode) // skipcq: GSC-G302
if err != nil {
Expand All @@ -88,8 +92,8 @@ func initAccessLog(logPath string) (err error) {
return nil
}

func InitLogs(logDir string, debug bool, uid int, gid int) (err error) {
if err = initLog(path.Join(logDir, "gochan.log"), debug); err != nil {
func InitLogs(logDir string, verbose bool, uid int, gid int) (err error) {
if err = initLog(path.Join(logDir, "gochan.log"), verbose); err != nil {
return err
}
if err = logFile.Chown(uid, gid); err != nil {
Expand Down Expand Up @@ -144,6 +148,7 @@ func LogDebug() *zerolog.Event {
}

func CloseLog() error {

if logFile == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/serverutil/antispam.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// ValidReferer checks to make sure that the incoming request is from the same domain (or if debug mode is enabled)
func ValidReferer(request *http.Request) bool {
if config.GetDebugMode() {
if config.VerboseMode() {
return true
}
referer := request.Referer()
Expand Down

0 comments on commit 8e95439

Please sign in to comment.