taldir

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

main.go (3604B)


      1 // This file is part of taldir, the Taler Directory implementation.
      2 // Copyright (C) 2022 Martin Schanzenbach
      3 //
      4 // Taldir is free software: you can redistribute it and/or modify it
      5 // under the terms of the GNU Affero General Public License as published
      6 // by the Free Software Foundation, either version 3 of the License,
      7 // or (at your option) any later version.
      8 //
      9 // Taldir is distributed in the hope that it will be useful, but
     10 // WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 // Affero General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU Affero General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 //
     17 // SPDX-License-Identifier: AGPL3.0-or-later
     18 
     19 package main
     20 
     21 /* TODO
     22 - ToS API (terms, privacy) with localizations
     23 - Prettify QR code landing page
     24 - Base32: Use gnunet-go module? (currently copied)
     25 - OrderId processing
     26 - Maintenance of database: When to delete expired validations?
     27 */
     28 
     29 import (
     30 	"flag"
     31 	"fmt"
     32 	"log"
     33 	"net/http"
     34 	"os"
     35 	"path"
     36 
     37 	"github.com/schanzen/taler-go/pkg/merchant"
     38 	"gopkg.in/ini.v1"
     39 	"gorm.io/driver/postgres"
     40 	"rsc.io/getopt"
     41 	taldir "taler.net/taldir/pkg/taldir"
     42 )
     43 
     44 var (
     45 	t              taldir.Taldir
     46 	version        string
     47 	taldirdatahome string
     48 	taldirconfdir  string
     49 	verbose        bool // FIXME do something with this?
     50 )
     51 
     52 func handleRequests(t *taldir.Taldir) {
     53 	log.Fatal(http.ListenAndServe(t.Cfg.Ini.Section("taldir").Key("bind_to").MustString("localhost:11000"), t.Router))
     54 }
     55 
     56 func printHelp() {
     57 	fmt.Print("taler-directory\n\n")
     58 	getopt.PrintDefaults()
     59 	fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n",
     60 		"Home page: https://taler.net\n",
     61 		"General help using GNU software: http://www.gnu.org/gethelp/\n")
     62 }
     63 
     64 func main() {
     65 	var cfgFlag = flag.String("c", "", "Configuration file to use")
     66 	getopt.Alias("c", "config")
     67 	// FIXME use flags
     68 	loglevelStringOpt := flag.String("L", "INFO", "Log level to use. DEBUG, INFO, WARNING or ERROR")
     69 	getopt.Alias("L", "loglevel")
     70 	var verboseFlag = flag.Bool("v", false, "Verbose")
     71 	getopt.Alias("v", "verbose")
     72 	var helpFlag = flag.Bool("h", false, "Print help")
     73 	getopt.Alias("h", "help")
     74 
     75 	getopt.Parse()
     76 	cfgfile := path.Join(taldirconfdir, "taldir.conf")
     77 	if len(*cfgFlag) != 0 {
     78 		cfgfile = *cfgFlag
     79 	}
     80 	if *helpFlag {
     81 		printHelp()
     82 		return
     83 	}
     84 	verbose = *verboseFlag
     85 	loglevel := taldir.LogInfo
     86 	for loglevelNum, loglevelString := range taldir.LoglevelStringMap {
     87 		if loglevelString == *loglevelStringOpt {
     88 			loglevel = loglevelNum
     89 		}
     90 	}
     91 	t := taldir.Taldir{}
     92 	cfg, err := ini.LooseLoad(cfgfile)
     93 	if err != nil {
     94 		log.Fatalf("Failed to read config: %v", err)
     95 		os.Exit(1)
     96 	}
     97 	psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
     98 		cfg.Section("taldir-pq").Key("host").MustString("localhost"),
     99 		cfg.Section("taldir-pq").Key("port").MustInt64(5432),
    100 		cfg.Section("taldir-pq").Key("user").MustString("taldir"),
    101 		cfg.Section("taldir-pq").Key("password").MustString("secret"),
    102 		cfg.Section("taldir-pq").Key("db_name").MustString("taldir"))
    103 	db := postgres.Open(psqlconn)
    104 	merchURL := cfg.Section("taldir").Key("base_url_merchant").MustString("https://backend.demo.taler.net")
    105 	merchToken := cfg.Section("taldir").Key("merchant_token").MustString("sandbox")
    106 	cfg.WriteTo(os.Stdout)
    107 	t.Initialize(taldir.TaldirConfig{
    108 		Ini:      cfg,
    109 		Version:  version,
    110 		Datahome: taldirdatahome,
    111 		Db:       db,
    112 		Loglevel: loglevel,
    113 		Merchant: merchant.NewMerchant(merchURL, merchToken),
    114 	})
    115 	handleRequests(&t)
    116 }