taldir

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

main.go (3416B)


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