taldir

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

main.go (3812B)


      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 // @title           Taler Directory API
     20 // @description     The Taler Directory (TalDir) maps alias hashes to wallet URIs. Clients register an alias by completing an out-of-band challenge, then look it up later using the hashed alias.
     21 // @contact.url     https://taler.net
     22 // @license.name    AGPL-3.0-or-later
     23 // @license.url     https://www.gnu.org/licenses/agpl-3.0.html
     24 // @BasePath        /
     25 package main
     26 
     27 /* TODO
     28 - ToS API (terms, privacy) with localizations
     29 - Prettify QR code landing page
     30 - Base32: Use gnunet-go module? (currently copied)
     31 - OrderId processing
     32 - Maintenance of database: When to delete expired validations?
     33 */
     34 
     35 import (
     36 	"flag"
     37 	"fmt"
     38 	"log"
     39 	"net/http"
     40 	"os"
     41 	"path"
     42 	"path/filepath"
     43 
     44 	_ "github.com/lib/pq"
     45 	"github.com/schanzen/taler-go/pkg/merchant"
     46 	"github.com/schanzen/taler-go/pkg/util"
     47 	"rsc.io/getopt"
     48 	taldir "taler.net/taldir/pkg/taldir"
     49 )
     50 
     51 var (
     52 	version        string
     53 	taldirdatahome string
     54 	taldirconfdir  string
     55 )
     56 
     57 func handleRequests(t *taldir.Taldir) {
     58 	log.Fatal(http.ListenAndServe(t.Cfg.Ini.GetString("directory", "bind_to", "localhost:11000"), t.Router))
     59 }
     60 
     61 func printHelp() {
     62 	fmt.Print("taler-directory\n\n")
     63 	getopt.PrintDefaults()
     64 	fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n",
     65 		"Home page: https://taler.net\n",
     66 		"General help using GNU software: http://www.gnu.org/gethelp/\n")
     67 }
     68 
     69 func main() {
     70 	var cfgFlag = flag.String("c", "", "Configuration file to use")
     71 	getopt.Alias("c", "config")
     72 	// FIXME use flags
     73 	loglevelStringOpt := flag.String("L", "INFO", "Log level to use. DEBUG, INFO, WARNING or ERROR")
     74 	getopt.Alias("L", "loglevel")
     75 	var helpFlag = flag.Bool("h", false, "Print help")
     76 	getopt.Alias("h", "help")
     77 	var versionFlag = flag.Bool("v", false, "Print version")
     78 	getopt.Alias("v", "version")
     79 
     80 	getopt.Parse()
     81 	cfgfile := path.Join(taldirconfdir, "taldir.conf")
     82 	if len(*cfgFlag) != 0 {
     83 		cfgfile = *cfgFlag
     84 	}
     85 	if *helpFlag {
     86 		printHelp()
     87 		return
     88 	}
     89 	if *versionFlag {
     90 		fullName, err := os.Executable()
     91 		if err != nil {
     92 			log.Panic(err)
     93 		}
     94 		fmt.Printf("%s %s", filepath.Base(fullName), version)
     95 		return
     96 	}
     97 	loglevel := taldir.LogInfo
     98 	for loglevelNum, loglevelString := range taldir.LoglevelStringMap {
     99 		if loglevelString == *loglevelStringOpt {
    100 			loglevel = loglevelNum
    101 		}
    102 	}
    103 	t := taldir.Taldir{}
    104 	cfg, err := util.LoadConfiguration(cfgfile)
    105 	if err != nil {
    106 		log.Fatalf("Failed to read config: %v", err)
    107 		os.Exit(1)
    108 	}
    109 	psqlconn := cfg.GetString("directory-pq", "connection_string", "postgres:///taler-directory")
    110 
    111 	db, err := taldir.OpenDatabase(psqlconn)
    112 	if err != nil {
    113 		log.Panic(err)
    114 	}
    115 	defer db.Close()
    116 	merchURL := cfg.GetString("directory", "merchant_base_url", "https://backend.demo.taler.net")
    117 	merchToken := cfg.GetString("directory", "merchant_token", "sandbox")
    118 	t.Initialize(taldir.TaldirConfig{
    119 		Ini:      cfg,
    120 		Version:  version,
    121 		Datahome: taldirdatahome,
    122 		Db:       db,
    123 		Loglevel: loglevel,
    124 		Merchant: merchant.NewMerchant(merchURL, merchToken),
    125 	})
    126 	handleRequests(&t)
    127 }