init
This commit is contained in:
81
template/services/api/config/config.go
Normal file
81
template/services/api/config/config.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/pelletier/go-toml"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var confPath string
|
||||
var Conf *Config
|
||||
var DefaultConf *Config
|
||||
|
||||
func init() {
|
||||
confPath = filepath.Join(xdg.ConfigHome, "<@var(context.project.name)>", "config.toml")
|
||||
Conf = &Config{}
|
||||
DefaultConf = &Config{
|
||||
Server: Server{
|
||||
Host: "127.0.0.1",
|
||||
Port: 8080,
|
||||
FrontendUrls: []string{"http://localhost:5173"},
|
||||
},
|
||||
Database: Database{
|
||||
User: "pp",
|
||||
Password: "<@var(context.project.name)>",
|
||||
Host: "127.0.0.1",
|
||||
Port: 5432,
|
||||
Name: "<@var(context.project.name)>",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Load() error {
|
||||
confContent, err := os.ReadFile(confPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return toml.Unmarshal(confContent, Conf)
|
||||
|
||||
}
|
||||
|
||||
func Write(config Config) error {
|
||||
confContent, err := toml.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dir := filepath.Dir(confPath)
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(confPath, confContent, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Host string
|
||||
Port int
|
||||
FrontendUrls []string
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
User string
|
||||
Password string
|
||||
Host string
|
||||
Port int
|
||||
Name string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Server Server
|
||||
Database Database
|
||||
}
|
||||
Reference in New Issue
Block a user