commit 63c9db3a2b8664502039de9d3a71053523817628
parent 7a911862c22a4944ed202037bbdc25bb25ec6e6c
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date: Sat, 21 Mar 2026 14:27:45 +0100
use new talerutil helpers for configuration
Diffstat:
7 files changed, 30 insertions(+), 54 deletions(-)
diff --git a/cmd/mailbox-server/main.go b/cmd/mailbox-server/main.go
@@ -28,10 +28,9 @@ import (
"path"
"path/filepath"
- "gopkg.in/ini.v1"
-
_ "github.com/lib/pq"
"github.com/schanzen/taler-go/pkg/merchant"
+ "github.com/schanzen/taler-go/pkg/util"
"rsc.io/getopt"
mailbox "taler.net/taler-mailbox/pkg/rest"
)
@@ -45,7 +44,7 @@ var (
)
func handleRequests(m *mailbox.Mailbox) {
- log.Fatal(http.ListenAndServe(m.Cfg.Ini.Section("mailbox").Key("bind_to").MustString("localhost:11000"), m.Router))
+ log.Fatal(http.ListenAndServe(m.Cfg.Ini.GetString("mailbox", "bind_to", "localhost:11000"), m.Router))
}
func printHelp() {
@@ -91,20 +90,20 @@ func main() {
}
}
m := mailbox.Mailbox{}
- iniCfg, err := ini.Load(cfgfile)
+ iniCfg, err := util.LoadConfiguration(cfgfile)
if err != nil {
log.Printf("Failed to read config: %v", err)
os.Exit(1)
}
- psqlconn := iniCfg.Section("mailbox-pq").Key("connection_string").MustString("postgres:///taler-mailbox")
+ psqlconn := iniCfg.GetString("mailbox-pq", "connection_string", "postgres:///taler-mailbox")
db, err := sql.Open("postgres", psqlconn)
if err != nil {
log.Panic(err)
}
defer db.Close()
- merchURL := iniCfg.Section("mailbox").Key("merchant_baseurl_private").MustString("http://merchant.mailbox/instances/myInstance")
- merchToken := iniCfg.Section("mailbox").Key("merchant_token").MustString("secretAccessToken")
+ merchURL := iniCfg.GetString("mailbox", "merchant_baseurl_private", "http://merchant.mailbox/instances/myInstance")
+ merchToken := iniCfg.GetString("mailbox", "merchant_token", "secretAccessToken")
merch := merchant.NewMerchant(merchURL, merchToken)
m.Initialize(mailbox.MailboxConfig{
LibtoolVersion: ltversion,
diff --git a/cmd/mailbox-server/main_test.go b/cmd/mailbox-server/main_test.go
@@ -20,7 +20,6 @@ import (
"github.com/schanzen/taler-go/pkg/merchant"
talerutil "github.com/schanzen/taler-go/pkg/util"
- "gopkg.in/ini.v1"
"taler.net/taler-mailbox/internal/gana"
"taler.net/taler-mailbox/internal/util"
"taler.net/taler-mailbox/pkg/rest"
@@ -78,12 +77,12 @@ func shouldReturnPaid() bool {
}
func TestMain(m *testing.M) {
- cfg, err := ini.Load("test-mailbox.conf")
+ cfg, err := talerutil.LoadConfiguration("test-mailbox.conf")
if err != nil {
fmt.Printf("Failed to read config: %v", err)
os.Exit(1)
}
- psqlconn := cfg.Section("mailbox-pq").Key("connection_string").MustString("postgres:///taler-mailbox")
+ psqlconn := cfg.GetString("mailbox-pq", "connection_string", "postgres:///taler-mailbox")
segments := strings.Split(strings.Split(psqlconn, "?")[0], "/")
dbName := segments[len(segments)-1]
diff --git a/cmd/mailbox-server/test-mailbox.conf b/cmd/mailbox-server/test-mailbox.conf
@@ -2,6 +2,7 @@
bind_to = localhost:11000
production = false
message_body_bytes = 256
+message_fee = KUDOS:0
monthly_fee = KUDOS:0
registration_update_fee = KUDOS:0
message_response_limit = 25
diff --git a/go.mod b/go.mod
@@ -5,7 +5,7 @@ go 1.24.0
require (
github.com/gorilla/mux v1.8.1
github.com/lib/pq v1.11.2
- github.com/schanzen/taler-go v1.2.1
+ github.com/schanzen/taler-go v1.5.2
gopkg.in/ini.v1 v1.67.1
rsc.io/getopt v0.0.0-20170811000552-20be20937449
)
diff --git a/go.sum b/go.sum
@@ -7,8 +7,8 @@ github.com/lib/pq v1.11.2 h1:x6gxUeu39V0BHZiugWe8LXZYZ+Utk7hSJGThs8sdzfs=
github.com/lib/pq v1.11.2/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/schanzen/taler-go v1.2.1 h1:K6ANhXb8ThsLs26ToXi7hD3pliqH1jAWI5UImNlr7Io=
-github.com/schanzen/taler-go v1.2.1/go.mod h1:bMpS4SS4jI5DyCbJqcy2J2O81OM5N57mVH+B+yJP1Xg=
+github.com/schanzen/taler-go v1.5.2 h1:Db7jRl+CaJ3ylvxFS/umFOyPxg/qhHj2qKEOzXfNbIM=
+github.com/schanzen/taler-go v1.5.2/go.mod h1:Q6xJcRUjnB15MbceZXF7FPBdqBid2a232VpVFyNY/So=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
diff --git a/pkg/rest/mailbox.go b/pkg/rest/mailbox.go
@@ -25,7 +25,6 @@ import (
"database/sql"
"encoding/binary"
"encoding/json"
- "errors"
"fmt"
"io"
"log"
@@ -39,7 +38,6 @@ import (
"github.com/schanzen/taler-go/pkg/merchant"
tos "github.com/schanzen/taler-go/pkg/rest"
talerutil "github.com/schanzen/taler-go/pkg/util"
- "gopkg.in/ini.v1"
"taler.net/taler-mailbox/internal/gana"
"taler.net/taler-mailbox/internal/util"
)
@@ -73,7 +71,7 @@ type MailboxConfig struct {
Datahome string
// Configuration
- Ini *ini.File
+ Ini talerutil.TalerConfiguration
// The database connection to use
DB *sql.DB
@@ -207,8 +205,7 @@ type MailboxRateLimitedResponse struct {
}
func (m *Mailbox) configResponse(w http.ResponseWriter, r *http.Request) {
- dpStr := m.Cfg.Ini.Section("mailbox").Key("delivery_period").MustString("72h")
- dp, err := time.ParseDuration(dpStr)
+ dp, err := m.Cfg.Ini.GetDuration("mailbox", "delivery_period", 3 * 24 * time.Hour)
if err != nil {
log.Fatal(err)
}
@@ -688,36 +685,21 @@ func (m *Mailbox) deleteMessagesResponse(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusNoContent)
}
-func (m *Mailbox) getFileName(relativeFileName string) string {
- _, err := os.Stat(relativeFileName)
- if errors.Is(err, os.ErrNotExist) {
- _, err := os.Stat(m.Cfg.Datahome + "/" + relativeFileName)
- if errors.Is(err, os.ErrNotExist) {
- log.Printf("Tried fallback not found %s\n", m.Cfg.Datahome+"/"+relativeFileName)
- return ""
- }
- return m.Cfg.Datahome + "/" + relativeFileName
- }
- return relativeFileName
-}
-
func (m *Mailbox) termsResponse(w http.ResponseWriter, r *http.Request) {
- s := m.Cfg.Ini.Section("mailbox")
- termspath := m.getFileName(s.Key("default_terms_path").MustString("terms/"))
+ termspath := m.Cfg.Ini.GetFilename("mailbox", "default_terms_path", "terms/", m.Cfg.Datahome)
tos.ServiceTermsResponse(w, r, termspath, tos.TalerTosConfig{
- DefaultFileType: s.Key("default_doc_filetype").MustString("text/html"),
- DefaultLanguage: s.Key("default_doc_lang").MustString("en"),
- SupportedFileTypes: strings.Split(s.Key("supported_doc_filetypes").String(), " "),
+ DefaultFileType: m.Cfg.Ini.GetString("mailbox", "default_doc_filetype", "text/html"),
+ DefaultLanguage: m.Cfg.Ini.GetString("mailbox", "default_doc_lang", "en"),
+ SupportedFileTypes: strings.Split(m.Cfg.Ini.GetString("mailbox", "supported_doc_filetypes", ""), " "),
})
}
func (m *Mailbox) privacyResponse(w http.ResponseWriter, r *http.Request) {
- s := m.Cfg.Ini.Section("mailbox")
- pppath := m.getFileName(s.Key("default_pp_path").MustString("privacy/"))
+ pppath := m.Cfg.Ini.GetFilename("mailbox", "default_pp_path", "privacy/", m.Cfg.Datahome)
tos.PrivacyPolicyResponse(w, r, pppath, tos.TalerTosConfig{
- DefaultFileType: s.Key("default_doc_filetype").MustString("text/html"),
- DefaultLanguage: s.Key("default_doc_lang").MustString("en"),
- SupportedFileTypes: strings.Split(s.Key("supported_doc_filetypes").String(), " "),
+ DefaultFileType: m.Cfg.Ini.GetString("mailbox", "default_doc_filetype", "text/html"),
+ DefaultLanguage: m.Cfg.Ini.GetString("mailbox", "default_doc_lang", "en"),
+ SupportedFileTypes: strings.Split(m.Cfg.Ini.GetString("mailbox", "supported_doc_filetypes", ""), " "),
})
}
@@ -751,31 +733,28 @@ func (m *Mailbox) Logf(loglevel LogLevel, fmt string, args ...any) {
func (m *Mailbox) Initialize(cfg MailboxConfig) {
m.Cfg = cfg
m.Logger = log.New(os.Stdout, "taler-mailbox:", log.LstdFlags)
- if cfg.Ini.Section("mailbox").Key("production").MustBool(false) {
- fmt.Println("Production mode enabled")
- }
- m.BaseURL = cfg.Ini.Section("mailbox").Key("base_url").MustString("https://example.com")
- m.MessageBodyBytes = cfg.Ini.Section("mailbox").Key("message_body_bytes").MustInt64(256)
- m.MessageResponseLimit = cfg.Ini.Section("mailbox").Key("message_response_limit").MustUint64(50)
- monthlyFee, err := talerutil.ParseAmount(cfg.Ini.Section("mailbox").Key("monthly_fee").MustString("KUDOS:0"))
+ m.BaseURL = cfg.Ini.GetString("mailbox", "base_url", "https://example.com")
+ m.MessageBodyBytes = cfg.Ini.GetInt64("mailbox", "message_body_bytes", 256)
+ m.MessageResponseLimit = uint64(cfg.Ini.GetInt64("mailbox", "message_response_limit", 50))
+ monthlyFee, err := cfg.Ini.GetAmount("mailbox", "monthly_fee", &talerutil.Amount{})
if err != nil {
fmt.Printf("Failed to parse monthly fee: %v", err)
os.Exit(1)
}
m.MonthlyFee = monthlyFee
- updateFee, err := talerutil.ParseAmount(cfg.Ini.Section("mailbox").Key("registration_update_fee").MustString("KUDOS:0"))
+ updateFee, err := cfg.Ini.GetAmount("mailbox", "registration_update_fee", &talerutil.Amount{})
if err != nil {
fmt.Printf("Failed to parse update fee: %v", err)
os.Exit(1)
}
m.RegistrationUpdateFee = updateFee
- messageFee, err := talerutil.ParseAmount(cfg.Ini.Section("mailbox").Key("message_fee").MustString("KUDOS:0"))
+ messageFee, err := cfg.Ini.GetAmount("mailbox", "message_fee", &talerutil.Amount{})
if err != nil {
fmt.Printf("Failed to parse message fee: %v", err)
os.Exit(1)
}
m.MessageFee = messageFee
- m.FreeMessageQuota = cfg.Ini.Section("mailbox").Key("free_message_quota").MustUint64(0)
+ m.FreeMessageQuota = uint64(cfg.Ini.GetInt64("mailbox", "free_message_quota", 0))
m.DB = cfg.DB
go func() {
for {
@@ -788,8 +767,7 @@ func (m *Mailbox) Initialize(cfg MailboxConfig) {
}
}()
// Clean up pending
- pendingExpStr := cfg.Ini.Section("mailbox").Key("pending_registration_expiration").MustString("24h")
- pendingExp, err := time.ParseDuration(pendingExpStr)
+ pendingExp, err := cfg.Ini.GetDuration("mailbox", "pending_registration_expiration", 24 * time.Hour)
if err != nil {
fmt.Printf("Failed to parse pending registration expiration: %v", err)
os.Exit(1)
diff --git a/taler-mailbox.conf.example b/taler-mailbox.conf.example
@@ -1,7 +1,6 @@
[mailbox]
bind_to = localhost:11000
base_url = https://example.com
-production = false
message_body_bytes = 256
message_response_limit = 50
monthly_fee = KUDOS:0