Add more settings, update readme and Dockerfile

This commit is contained in:
Ivan R. 2023-07-22 21:32:11 +05:00
parent e24a3a754c
commit 37e8663ac2
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
6 changed files with 45 additions and 6 deletions

2
.env
View file

@ -1,3 +1,5 @@
P_SECRETKEY="super-secret-key"
P_DBPATH="db.sqlite3"
P_LOGLEVEL="debug"
P_ENABLEGINLOGGER="true"
P_PRODUCTION="false"

View file

@ -22,7 +22,8 @@ COPY assets ./assets
COPY templates ./templates
RUN mkdir /var/lib/phoenix
ENV PHOENIX_DB_PATH=/var/lib/phoenix/db.sqlite3
ENV P_DBPATH="/var/lib/phoenix/db.sqlite3"
ENV P_PRODUCTION="true"
EXPOSE 8080

View file

@ -7,9 +7,13 @@ import (
)
type Config struct {
SecretKey string `required:"true"`
DBPath string `required:"true"`
LogLevel string `default:"warning"`
SecretKey string `required:"true"`
DBPath string `required:"true"`
LogLevel string `default:"warning"`
EnableGinLogger bool `default:"false"`
Production bool `default:"true"`
DefaultUsername string
DefaultPassword string
}
func GetConfig() (*Config, error) {

10
main.go
View file

@ -30,6 +30,16 @@ func main() {
logrus.Fatalf("%v", err)
}
// Create the first user
if cfg.DefaultUsername != "" && cfg.DefaultPassword != "" {
if database.CountAdmins(db) < 1 {
_, err := database.CreateAdmin(db, cfg.DefaultUsername, cfg.DefaultPassword)
if err != nil {
logrus.Errorf("%v", err)
}
}
}
engine := views.GetGinEngine(cfg, db)
engine.Run(":8080")
}

View file

@ -11,7 +11,16 @@ Self-hosted start page without the extra stuff.
## Configuration
Service settings can be set through environment variables.
- `PHOENIX_DB_PATH` - path to the sqlite database.
| Variable | Description | Default |
| --- | --- | --- |
| P_DBPATH | Path to the sqlite database. | Docker: `/var/lib/phoenix/db.sqlite3` |
| P_SECRETKEY | A long and random secret string used for authorization. | |
| P_LOGLEVEL | Log level settings: `debug`, `info`, `warning`, `error`, `fatal` | `warning` |
| P_ENABLEGINLOGGER | Enable gin's logging middleware. Can create a lot of logs. | `false` |
| P_PRODUCTION | Is this instance running in production mode? | `true` |
| P_DEFAULTUSERNAME | Data for the first user. | |
| P_DEFAULTPASSWORD | Data for the first user. | |
## Docker-compose example
```yml
@ -22,6 +31,10 @@ services:
- phoenix:/var/lib/phoenix
ports:
- 80:8080
environment:
P_SECRETKEY: "your-random-string"
P_DEFAULTUSERNAME: "admin"
P_DEFAULTPASSWORD: "super-password"
restart: unless-stopped
volumes:

View file

@ -7,7 +7,16 @@ import (
)
func GetGinEngine(cfg *config.Config, db *gorm.DB) *gin.Engine {
engine := gin.Default()
if cfg.Production {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.New()
engine.Use(gin.Recovery())
if cfg.EnableGinLogger {
engine.Use(gin.Logger())
}
engine.LoadHTMLGlob("templates/*")
engine.Static("/assets", "./assets")