Add a config to create new users on startup

This commit is contained in:
2026-03-01 00:15:09 +01:00
parent 18b5f0f0e1
commit 660c44992e
5 changed files with 134 additions and 46 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"strings"
"github.com/alecthomas/kong"
kongtoml "github.com/alecthomas/kong-toml"
@@ -11,15 +12,27 @@ import (
)
type Config struct {
Port string `toml:"port" default:"8080" comment:"The port to listen on for the server."`
DatabaseFilePath string `toml:"database-file-path" default:"plm.db" comment:"Path to sqlite database file."`
DemoDataPath string `toml:"demo-data-path" comment:"The path to the sql file to load for demo data."`
JWTKey string `toml:"jwt-key" comment:"The key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."`
ImageFolderPath string `toml:"image-folder-path" default:"img" comment:"Folder where uploaded files will be stored."`
Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."`
InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."`
DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."`
DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as user 'demo'"`
Port string `toml:"port" default:"8080" comment:"The port to listen on for the server."`
DatabaseFilePath string `toml:"database-file-path" default:"plm.db" comment:"Path to sqlite database file."`
DemoDataPath string `toml:"demo-data-path" comment:"The path to the sql file to load for demo data."`
JWTKey string `toml:"jwt-key" comment:"The key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."`
ImageFolderPath string `toml:"image-folder-path" default:"img" comment:"Folder where uploaded files will be stored."`
Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."`
InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."`
DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."`
DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as user 'demo'"`
AddUser UserListAsStrings `toml:"add-user" short:"a" comment:"Add an user on startup following htpasswd bcrypt format, example: [\"demo:$2y$10$UHR2646SZo2W.Rhna7bn5eWNLXWJZ/Sa3oLd9RlxlXs57Bwp6isOS\",\"user:$2y$10$3WYUp.VDpzJRywtrxO1s/uWfUIKpTE4yh5B1d2RCef3hvczYbEWTC\"]"`
}
type UserListAsStrings []string
func (u UserListAsStrings) Validate() error {
for _, s := range u {
if strings.Count(s, ":") != 1 {
return errors.New("For adding users, please follow the format [username]:[bcrypt hashed password]")
}
}
return nil
}
func defaultConfig() Config {
@@ -33,6 +46,7 @@ func defaultConfig() Config {
InventaireUrl: "https://inventaire.io",
DisableRegistration: false,
DemoMode: false,
AddUser: []string{},
}
}