From 8021684f779fb58d47f5d9a1768c7b152b1f6f58 Mon Sep 17 00:00:00 2001 From: Fanir Date: Wed, 26 Jan 2022 00:22:22 +0100 Subject: [PATCH] added configuration for webserver and AllowRoot --- .gitignore | 1 + README.md | 10 ++++++++++ cmd/feedizer/config/config.go | 15 +++++++++++++++ cmd/feedizer/config/env.go | 9 +++++++++ cmd/feedizer/main.go | 8 ++++---- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index aa047f0..2690f36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.yaml # ---> Go # Binaries for programs and plugins diff --git a/README.md b/README.md index 67de6cc..f6d7c6d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # Feedizer A rewrite of the [Feedizer](https://git.zom.bi/fanir/feedizer) project originally written in PHP5. + +## Configuration + +To write the default configuration to a file, run `feedizer [-C path/to/configfile.yaml] newconfig` + +## Running as root + +Feedizer will abort startup if run as root. If you want to disable this behavior, +i.e. because feedizer is running inside a docker container, you can set +`FEEDIZER_ALLOWROOT=1` in the environment or `AllowRoot: true` in the config file. diff --git a/cmd/feedizer/config/config.go b/cmd/feedizer/config/config.go index 47ad97e..cb14b78 100644 --- a/cmd/feedizer/config/config.go +++ b/cmd/feedizer/config/config.go @@ -17,8 +17,18 @@ type DatabaseConfig struct { Options string } +type ServerConfig struct { + Address string + TemplateDir string + StaticDir string +} + type Config struct { Database DatabaseConfig + + Server ServerConfig + + AllowRoot bool } var config = Config{ @@ -28,6 +38,11 @@ var config = Config{ Database: "feedizer", Options: "sslmode=require", }, + Server: ServerConfig{ + Address: ":3000", + TemplateDir: "../../templates", + StaticDir: "../../static", + }, } const appName = "feedizer" diff --git a/cmd/feedizer/config/env.go b/cmd/feedizer/config/env.go index 54da9eb..063e82e 100644 --- a/cmd/feedizer/config/env.go +++ b/cmd/feedizer/config/env.go @@ -57,6 +57,15 @@ func structFromEnv(v reflect.Value, t reflect.Type, prefix string) (namesAvailab panic(err) } fv.SetInt(x) + case reflect.Bool: + switch strings.ToLower(val) { + case "1", "true", "t", "on": + fv.SetBool(true) + case "0", "false", "f", "off": + fv.SetBool(false) + default: + panic(fmt.Sprintf("cannot set field for environment variable %s: cannot parse %s as boolean. Please use one of: 1, true, t, on / 0, false, f, off", envName, fk)) + } default: panic(fmt.Sprintf("cannot set field for environment variable %s: type %s is not supported", envName, fk)) } diff --git a/cmd/feedizer/main.go b/cmd/feedizer/main.go index ff47eaf..a2d471a 100644 --- a/cmd/feedizer/main.go +++ b/cmd/feedizer/main.go @@ -14,10 +14,6 @@ import ( ) func main() { - if os.Geteuid() == 0 { - log.Fatalln("do not run feedizer as root") - } - configFile := flag.String("C", "", "custom config file path") autoMigrate := flag.Bool("m", true, "Automatically migrate the database to the highest version.") flag.Parse() @@ -49,6 +45,10 @@ func startApp(automigrate bool) { log.Fatalln(err) } + if !cfg.AllowRoot && os.Geteuid() == 0 { + log.Fatalln("do not run feedizer as root") + } + db, err := database.Open(cfg.Database) if err != nil { log.Fatalln(err)