gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taldir] 01/02: move from json to ini


From: gnunet
Subject: [taler-taldir] 01/02: move from json to ini
Date: Tue, 19 Apr 2022 17:53:23 +0200

This is an automated email from the git hooks/post-receive script.

martin-schanzenbach pushed a commit to branch master
in repository taldir.

commit be90eab5f965bf334a749e12267ef5f1d8df362f
Author: Martin Schanzenbach <mschanzenbach@posteo.de>
AuthorDate: Tue Apr 19 17:52:32 2022 +0200

    move from json to ini
---
 config.json | 13 -------------
 go.mod      |  1 +
 taldir.go   | 42 +++++++++++++++++++++++-------------------
 3 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/config.json b/config.json
deleted file mode 100644
index a03be20..0000000
--- a/config.json
+++ /dev/null
@@ -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
index 0a2a8bc..24be3e6 100644
--- 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
index eeb622e..2c3e914 100644
--- 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)

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]