package config import ( "github.com/adrg/xdg" "github.com/pelletier/go-toml" "net" "net/url" "os" "path/filepath" "strconv" ) 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", "http://127.0.0.1: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 } // URL returns the Postgres connection URL. The local dev database has no SSL. func (d Database) URL() *url.URL { return &url.URL{ Scheme: "postgres", User: url.UserPassword(d.User, d.Password), Host: net.JoinHostPort(d.Host, strconv.Itoa(d.Port)), Path: "/" + d.Name, RawQuery: "sslmode=disable", } } type Config struct { Server Server Database Database }