diff --git a/README.md b/README.md index 7bafc64..6b75963 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,32 @@ TODO (or is it...) ### Build and run it yourself -TODO +#### Requirements + +- The project is developed with Go 1.20.5 + +#### Build + +There are 2 scripts to build the project: +- `super_build.sh` +- `super_build.bat` + +They will build the project for windows, linux and macOS. The binaries will be in the `bin` folder. +You can also really just use `go build` to build the project for your current OS. + +#### Run + +Once the project is built, you can simply run the binary for your OS. Setting the environment variables is +of course required, but not necessarily via the `.env` file. ### Environment variables -| Name | Type | Description | Comment | -|----------------------|--------|---------------------------------------|-----------------------------------------| -| ENVIRONMENT | string | The environment the API is running in | required, `development` or `production` | -| PORT | int | The port the API will use | required | -| CORS_ALLOWED_ORIGINS | string | The allowed origins for CORS | required, semicolon separated list | +| Name | Type | Description | Comment | +|----------------------|--------|---------------------------------------|-------------------------------------------------------------------------------------------------------| +| ENVIRONMENT | string | The environment the API is running in | required, `development` or `production` | +| PORT | int | The port the API will use | required | +| CORS_ALLOWED_ORIGINS | string | The allowed origins for CORS | required, semicolon separated list | +| DB_FILE | string | The path to the sqlite db file | required, can be at the root but name still required (e.g. `./gohcms.db`) ; have to end up with `.db` | ## API Usage diff --git a/api/dependecyInjection.go b/api/dependecyInjection.go index 19e6969..92ec0c2 100644 --- a/api/dependecyInjection.go +++ b/api/dependecyInjection.go @@ -6,6 +6,8 @@ import ( "github.com/glebarez/sqlite" "go.uber.org/dig" "gorm.io/gorm" + "os" + "path/filepath" ) type LocalContainer struct { @@ -48,10 +50,16 @@ func InitContainer() { digContainer := dig.New() - db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) - if err != nil { - panic(err) + dbName := os.Getenv("DB_FILE") + if err := os.MkdirAll(filepath.Dir(dbName), os.ModePerm); err != nil { + panic("Unable to create necessary subdirectories: " + err.Error()) } + + db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{}) + if err != nil { + panic("Unable to open the database: " + err.Error()) + } + _ = db.AutoMigrate(&models.Article{}, &models.User{}) _ = digContainer.Provide(func() *gorm.DB { return db }) diff --git a/main/server/server.go b/main/server/server.go index c4b8579..1892c4b 100644 --- a/main/server/server.go +++ b/main/server/server.go @@ -11,7 +11,7 @@ import ( ) var possibleEnvFileLocations = []string{".env", "../.env"} -var envVarsToLoad = []string{"PORT", "ENVIRONMENT", "CORS_ALLOWED_ORIGINS"} +var envVarsToLoad = []string{"PORT", "ENVIRONMENT", "CORS_ALLOWED_ORIGINS", "DB_FILE"} func initEnvVariables() { var err error