taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

commit be90eab5f965bf334a749e12267ef5f1d8df362f
parent 7c7ea95a9fb12362d704e98499cb894b7919a87d
Author: Martin Schanzenbach <mschanzenbach@posteo.de>
Date:   Tue, 19 Apr 2022 17:52:32 +0200

move from json to ini

Diffstat:
Dconfig.json | 13-------------
Mgo.mod | 1+
Mtaldir.go | 42+++++++++++++++++++++++-------------------
3 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/config.json b/config.json @@ -1,13 +0,0 @@ -{ - "production": false, - "validators": ["email","phone"], - "email_sender": "taldir@taler.net", - "host": "https://taldir.net/", - "bind_to": "localhost:11000", - "salt": "ChangeMe", - "pq_host": "localhost", - "pq_port": 5432, - "pq_user": "taldir", - "pq_password": "secret", - "pq_dbname": "taldir" -} diff --git a/go.mod b/go.mod @@ -8,6 +8,7 @@ require ( github.com/jcmturner/gokrb5/v8 v8.2.0 // indirect github.com/jinzhu/now v1.1.5 // indirect golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect + gopkg.in/ini.v1 v1.66.4 // indirect gopkg.in/jcmturner/aescts.v1 v1.0.1 // indirect gopkg.in/jcmturner/dnsutils.v1 v1.0.1 // indirect gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect diff --git a/taldir.go b/taldir.go @@ -13,6 +13,8 @@ import ( "net/smtp" "crypto/sha256" "gorm.io/driver/postgres" + "gopkg.in/ini.v1" + "strings" ) type Configuration struct { @@ -55,7 +57,7 @@ type Validation struct { var db *gorm.DB // Our configuration from the config.json -var config Configuration +var cfg *ini.File // Map of supported validators as defined in the configuration var validators map[string]bool @@ -63,7 +65,7 @@ var validators map[string]bool // Send an email for email identities func sendEmail(recipient string, ref Validation) { - from := config.EmailSender + from := cfg.Section("taldir-email").Key("sender").MustString("taldir@example.com") to := []string{ recipient, } @@ -71,8 +73,10 @@ func sendEmail(recipient string, ref Validation) { smtpHost := "localhost" smtpPort := "587" - message := fmt.Sprintf("Please click here to validate your Taldir identity: %s%s", config.Host, ref.ValidationReference) - + message := fmt.Sprintf("Please click here to validate your Taldir identity: %s%s", + cfg.Section("taldir").Key("host").MustString("http://localhost"), + ref.ValidationReference) + err := smtp.SendMail(smtpHost+":"+smtpPort, nil, from, to, []byte(message)) if err != nil { fmt.Println(err) @@ -100,8 +104,8 @@ func returnSingleEntry(w http.ResponseWriter, r *http.Request){ // Hashes an identity key (e.g. sha256(<email address>)) with a salt for // Lookup and storage. func hashIdentityKey(idkey string) string { - fmt.Println("Using salt " + config.Salt) - salt := make([]byte, len(config.Salt)) + fmt.Println("Using salt " + cfg.Section("taldir").Key("salt").MustString("ChangeMe")) + salt := make([]byte, len(cfg.Section("taldir").Key("salt").MustString("ChangeMe"))) h := sha256.New() h.Write([]byte(idkey)) h.Write(salt) @@ -204,29 +208,29 @@ func handleRequests() { myRouter.HandleFunc("/directory/{identity_key}", returnSingleEntry).Methods("GET") myRouter.HandleFunc("/validation/{reference}", validateSingleEntry).Methods("GET") myRouter.HandleFunc("/register/{identity}", addPendingValidation).Methods("POST") - fmt.Println("Listening on " + config.BindTo) - log.Fatal(http.ListenAndServe(config.BindTo, myRouter)) + log.Fatal(http.ListenAndServe(cfg.Section("taldir").Key("bind_to").MustString("localhost:11000"), myRouter)) } func main() { - file, _ := os.Open("config.json") - defer file.Close() - decoder := json.NewDecoder(file) - config = Configuration{} - err := decoder.Decode(&config) + _cfg, err := ini.Load("taldir.conf") if err != nil { - fmt.Println("error:", err) + fmt.Printf("Failed to read config: %v", err) + os.Exit(1) } - if config.Production { + cfg = _cfg + if cfg.Section("taldir").Key("production").MustBool(false) { fmt.Println("Production mode enabled") } validators = make(map[string]bool) - fmt.Println(config.BindTo) - fmt.Println("Enabled validators:", config.Validators) - for _, a := range config.Validators { + for _, a := range strings.Split(cfg.Section("taldir").Key("validators").String(), " ") { validators[a] = true } - psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config.PqHost, config.PqPort, config.PqUser, config.PqPassword, config.PqDbname) + psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + cfg.Section("taldir-pq").Key("host").MustString("localhost"), + cfg.Section("taldir-pq").Key("port").MustInt64(5432), + cfg.Section("taldir-pq").Key("user").MustString("taldir"), + cfg.Section("taldir-pq").Key("password").MustString("secret"), + cfg.Section("taldir-pq").Key("db_name").MustString("taldir")) _db, err := gorm.Open(postgres.Open(psqlconn), &gorm.Config{}) if err != nil { panic(err)