diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | config/debug.json | 14 | ||||
-rw-r--r-- | config/default.json | 4 | ||||
-rw-r--r-- | utils/database.go | 24 | ||||
-rw-r--r-- | utils/runtime_config.go | 25 |
5 files changed, 26 insertions, 43 deletions
@@ -33,4 +33,4 @@ install: all install -Dm 644 sql/0002_migration.sql $(DESTDIR)$(PREFIX)/share/ceres/migrations/0002_migration.sql debug: - go run main.go -d -c config/default.json + go run main.go -c config/debug.json diff --git a/config/debug.json b/config/debug.json new file mode 100644 index 0000000..7d1295b --- /dev/null +++ b/config/debug.json @@ -0,0 +1,14 @@ +{ + "http":{ + "bind_host":"127.0.0.1", + "bind_port":"8080", + "static":"./data/static", + "templates":"./data/templates", + "storage":"./data/storage" + }, + "database":{ + "socket":"/run/mysqld/mysqld.sock", + "database":"ceres", + "migrations":"./sql" + } +} diff --git a/config/default.json b/config/default.json index 076d672..7710774 100644 --- a/config/default.json +++ b/config/default.json @@ -8,7 +8,7 @@ }, "database":{ "socket":"/run/mysqld/mysqld.sock", - "user":"ceres", - "database":"ceres" + "database":"ceres", + "migrations":"/usr/share/ceres/migrations" } } diff --git a/utils/database.go b/utils/database.go index b8a6941..f48af35 100644 --- a/utils/database.go +++ b/utils/database.go @@ -31,7 +31,7 @@ func InitDatabase(config DatabaseConfig) Database { db := NewDatabase(config) db.Connect() db.Ping() - db.Migrate(config.Debug) + db.Migrate(config.Migrations) // allow graceful shutdown var listener = make(chan os.Signal) @@ -53,15 +53,11 @@ func NewDatabase(config DatabaseConfig) Database { db.config = config var username string - if config.Debug { - user_ptr,err := user.Current() - if err != nil { - log.Fatal(err) - } - username = user_ptr.Username - } else { - username = config.User + user_ptr,err := user.Current() + if err != nil { + log.Fatal(err) } + username = user_ptr.Username db.target = fmt.Sprintf("%s@unix(%s)/%s", username, config.Socket, config.Database) return db @@ -85,15 +81,7 @@ func (db *Database) Ping() { } } -func (db *Database) Migrate(debug bool) { - - // get directory with SQL migration scripts - var dir string - if debug { - dir = "./sql" - } else { - dir = "/usr/share/ceres/migrations/" - } +func (db *Database) Migrate(dir string) { const t = databaseSchemaVersion // targeted database schema version diff --git a/utils/runtime_config.go b/utils/runtime_config.go index 42170f6..fb5bd72 100644 --- a/utils/runtime_config.go +++ b/utils/runtime_config.go @@ -12,7 +12,6 @@ import ( type RuntimeConfig struct { Path string - Debug bool Http HttpConfig `json:"http"` Database DatabaseConfig `json:"database"` } @@ -29,7 +28,7 @@ type DatabaseConfig struct { Socket string `json:"socket"` User string `json:"user"` Database string `json:"database"` - Debug bool + Migrations string `json:"migrations"` } func GetRuntimeConfig() RuntimeConfig { @@ -39,7 +38,6 @@ func GetRuntimeConfig() RuntimeConfig { // read command line flags flag.StringVar(&config.Path, "c", "/etc/ceres/config.json", "Path to ceres configuration file") - flag.BoolVar(&config.Debug, "d", false, "Use this flag if you are in a development environment") flag.Parse() // open config file @@ -55,30 +53,13 @@ func GetRuntimeConfig() RuntimeConfig { log.Fatalf("Could not read configuration file %s", config.Path) } + fmt.Print("Used config: " + string(configData) + "\n") + // parse content to config structs err = json.Unmarshal(configData, &config) if err != nil { log.Fatalf("Could not parse configuration file %s", config.Path) } - // override defaults if in debugging mode - if config.Debug { - config.Http.Static = "./data/static" - config.Http.Templates = "./data/templates" - config.Http.Storage = "./data/storage" - } - - // copy debug value - config.Database.Debug = config.Debug - - // print config if in debug mode - if config.Debug { - configuration,err := json.MarshalIndent(config, "", " ") - if err != nil { - log.Fatal(err) - } - fmt.Print("Used config: " + string(configuration) + "\n") - } - return config } |