mirror of
https://github.com/renorris/openfsd
synced 2026-03-22 06:25:35 +08:00
Introduce SetIfNotExists, atomic Client, dynamic web config, ServerConfig, data APIs, and fixes.
This commit is contained in:
@@ -44,6 +44,15 @@ func (p *PostgresConfigRepository) Set(key string, value string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *PostgresConfigRepository) SetIfNotExists(key string, value string) (err error) {
|
||||
querystr := `
|
||||
INSERT INTO config (key, value) VALUES ($1, $2)
|
||||
ON CONFLICT (key) DO NOTHING;
|
||||
`
|
||||
_, err = p.db.Exec(querystr, key, value)
|
||||
return
|
||||
}
|
||||
|
||||
// Get retrieves the value for the given key from the configuration.
|
||||
// If the key does not exist, it returns ErrConfigKeyNotFound.
|
||||
func (p *PostgresConfigRepository) Get(key string) (value string, err error) {
|
||||
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
)
|
||||
|
||||
type ConfigRepository interface {
|
||||
// InitDefault initializes the default state of the Config if one does not already exist.
|
||||
InitDefault() (err error)
|
||||
|
||||
// Set sets a value for a given key
|
||||
Set(key string, value string) (err error)
|
||||
|
||||
// SetIfNotExists sets a value for a given key if it does not already exist
|
||||
SetIfNotExists(key string, value string) (err error)
|
||||
|
||||
// Get gets a value for a given key.
|
||||
//
|
||||
// Returns ErrConfigKeyNotFound if no key/value pair is found.
|
||||
@@ -21,7 +21,14 @@ type ConfigRepository interface {
|
||||
}
|
||||
|
||||
const (
|
||||
ConfigJwtSecretKey = "JWT_SECRET_KEY"
|
||||
ConfigJwtSecretKey = "JWT_SECRET_KEY"
|
||||
|
||||
ConfigFsdServerHostname = "FSD_SERVER_HOSTNAME"
|
||||
ConfigFsdServerIdent = "FSD_SERVER_IDENT"
|
||||
ConfigFsdServerLocation = "FSD_SERVER_LOCATION"
|
||||
|
||||
ConfigApiServerBaseURL = "API_SERVER_BASE_URL"
|
||||
|
||||
ConfigWelcomeMessage = "WELCOME_MESSAGE"
|
||||
)
|
||||
|
||||
@@ -46,3 +53,27 @@ func GetWelcomeMessage(r *ConfigRepository) (msg string) {
|
||||
msg, _ = (*r).Get(ConfigWelcomeMessage)
|
||||
return
|
||||
}
|
||||
|
||||
func InitDefaultConfig(r *ConfigRepository) (err error) {
|
||||
secretKey, err := GenerateJwtSecretKey()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defaultConfig := map[string]string{
|
||||
ConfigJwtSecretKey: string(secretKey[:]),
|
||||
ConfigWelcomeMessage: "Connected to openfsd",
|
||||
ConfigFsdServerHostname: "localhost",
|
||||
ConfigFsdServerIdent: "OPENFSD",
|
||||
ConfigFsdServerLocation: "Earth",
|
||||
ConfigApiServerBaseURL: "http://localhost",
|
||||
}
|
||||
|
||||
for k, v := range defaultConfig {
|
||||
if err = (*r).SetIfNotExists(k, v); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,24 +9,12 @@ type SQLiteConfigRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func (s *SQLiteConfigRepository) InitDefault() (err error) {
|
||||
if err = s.ensureSecretKeyExists(); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *SQLiteConfigRepository) ensureSecretKeyExists() (err error) {
|
||||
secretKey, err := GenerateJwtSecretKey()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *SQLiteConfigRepository) SetIfNotExists(key string, value string) (err error) {
|
||||
querystr := `
|
||||
INSERT INTO config (key, value) VALUES (?, ?)
|
||||
ON CONFLICT(key) DO NOTHING;
|
||||
`
|
||||
if _, err = s.db.Exec(querystr, ConfigJwtSecretKey, secretKey[:]); err != nil {
|
||||
if _, err = s.db.Exec(querystr, key, value); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user