taldir

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

main.go (3390B)


      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 	"database/sql"
     38 
     39 	_ "github.com/lib/pq"
     40 	"github.com/schanzen/taler-go/pkg/merchant"
     41 	"gopkg.in/ini.v1"
     42 	"rsc.io/getopt"
     43 	taldir "taler.net/taldir/pkg/taldir"
     44 )
     45 
     46 var (
     47 	t              taldir.Taldir
     48 	version        string
     49 	taldirdatahome string
     50 	taldirconfdir  string
     51 	verbose        bool // FIXME do something with this?
     52 )
     53 
     54 func handleRequests(t *taldir.Taldir) {
     55 	log.Fatal(http.ListenAndServe(t.Cfg.Ini.Section("taldir").Key("bind_to").MustString("localhost:11000"), t.Router))
     56 }
     57 
     58 func printHelp() {
     59 	fmt.Print("taler-directory\n\n")
     60 	getopt.PrintDefaults()
     61 	fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n",
     62 		"Home page: https://taler.net\n",
     63 		"General help using GNU software: http://www.gnu.org/gethelp/\n")
     64 }
     65 
     66 func main() {
     67 	var cfgFlag = flag.String("c", "", "Configuration file to use")
     68 	getopt.Alias("c", "config")
     69 	// FIXME use flags
     70 	loglevelStringOpt := flag.String("L", "INFO", "Log level to use. DEBUG, INFO, WARNING or ERROR")
     71 	getopt.Alias("L", "loglevel")
     72 	var verboseFlag = flag.Bool("v", false, "Verbose")
     73 	getopt.Alias("v", "verbose")
     74 	var helpFlag = flag.Bool("h", false, "Print help")
     75 	getopt.Alias("h", "help")
     76 
     77 	getopt.Parse()
     78 	cfgfile := path.Join(taldirconfdir, "taldir.conf")
     79 	if len(*cfgFlag) != 0 {
     80 		cfgfile = *cfgFlag
     81 	}
     82 	if *helpFlag {
     83 		printHelp()
     84 		return
     85 	}
     86 	verbose = *verboseFlag
     87 	loglevel := taldir.LogInfo
     88 	for loglevelNum, loglevelString := range taldir.LoglevelStringMap {
     89 		if loglevelString == *loglevelStringOpt {
     90 			loglevel = loglevelNum
     91 		}
     92 	}
     93 	t := taldir.Taldir{}
     94 	cfg, err := ini.LooseLoad(cfgfile)
     95 	if err != nil {
     96 		log.Fatalf("Failed to read config: %v", err)
     97 		os.Exit(1)
     98 	}
     99 	psqlconn := cfg.Section("taldir-pq").Key("connection_string").MustString("postgres:///taler-directory")
    100 
    101 	db, err := sql.Open("postgres", psqlconn)
    102 	if err != nil {
    103 		log.Panic(err)
    104 	}
    105 	defer db.Close()
    106 	merchURL := cfg.Section("taldir").Key("base_url_merchant").MustString("https://backend.demo.taler.net")
    107 	merchToken := cfg.Section("taldir").Key("merchant_token").MustString("sandbox")
    108 	cfg.WriteTo(os.Stdout)
    109 	t.Initialize(taldir.TaldirConfig{
    110 		Ini:      cfg,
    111 		Version:  version,
    112 		Datahome: taldirdatahome,
    113 		Db:       db,
    114 		Loglevel: loglevel,
    115 		Merchant: merchant.NewMerchant(merchURL, merchToken),
    116 	})
    117 	handleRequests(&t)
    118 }