taler-mailbox

Service for asynchronous wallet-to-wallet payment messages
Log | Files | Refs | Submodules | README | LICENSE

main.go (3421B)


      1 // This file is part of taler-mailbox, the Taler Mailbox implementation.
      2 // Copyright (C) 2022 Martin Schanzenbach
      3 //
      4 // Taler-mailbox 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 // Taler-mailbox 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 import (
     22 	"flag"
     23 	"fmt"
     24 	"log"
     25 	"net/http"
     26 	"os"
     27 	"path"
     28 	"path/filepath"
     29 
     30 	_ "github.com/lib/pq"
     31 	"github.com/schanzen/taler-go/pkg/merchant"
     32 	"github.com/schanzen/taler-go/pkg/util"
     33 	"rsc.io/getopt"
     34 	mailbox "taler.net/taler-mailbox/pkg/rest"
     35 )
     36 
     37 var (
     38 	m               mailbox.Mailbox
     39 	ltversion       string
     40 	version         string
     41 	mailboxdatahome string
     42 	mailboxconfdir  string
     43 )
     44 
     45 func handleRequests(m *mailbox.Mailbox) {
     46 	log.Fatal(http.ListenAndServe(m.Cfg.Ini.GetString("mailbox", "bind_to", "localhost:11000"), m.Router))
     47 }
     48 
     49 func printHelp() {
     50 	fmt.Print("taler-mailbox\n\n")
     51 	getopt.PrintDefaults()
     52 	fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n",
     53 		"Home page: https://taler.net\n",
     54 		"General help using GNU software: http://www.gnu.org/gethelp/\n")
     55 }
     56 
     57 func main() {
     58 	var cfgFlag = flag.String("c", "mailbox.conf", "Configuration file to use")
     59 	getopt.Alias("c", "config")
     60 	// FIXME use flags
     61 	loglevelStringOpt := flag.String("L", "INFO", "Log level to use. DEBUG, INFO, WARNING or ERROR")
     62 	getopt.Alias("L", "loglevel")
     63 	var versionFlag = flag.Bool("v", false, "Print version")
     64 	getopt.Alias("v", "version")
     65 	var helpFlag = flag.Bool("h", false, "Print help")
     66 	getopt.Alias("h", "help")
     67 
     68 	getopt.Parse()
     69 	if *helpFlag {
     70 		printHelp()
     71 		return
     72 	}
     73 	if *versionFlag {
     74 		fullName, err := os.Executable()
     75 		if err != nil {
     76 			log.Panic(err)
     77 		}
     78 		fmt.Printf("%s %s", filepath.Base(fullName), version)
     79 		return
     80 	}
     81 	cfgfile := path.Join(mailboxconfdir, "taler-mailbox.conf")
     82 	if len(*cfgFlag) != 0 {
     83 		cfgfile = *cfgFlag
     84 	}
     85 	loglevel := mailbox.LogInfo
     86 	for loglevelNum, loglevelString := range mailbox.LoglevelStringMap {
     87 		if loglevelString == *loglevelStringOpt {
     88 			loglevel = loglevelNum
     89 		}
     90 	}
     91 	m := mailbox.Mailbox{}
     92 	iniCfg, err := util.LoadConfiguration(cfgfile)
     93 	if err != nil {
     94 		log.Printf("Failed to read config: %v", err)
     95 		os.Exit(1)
     96 	}
     97 	psqlconn := iniCfg.GetString("mailbox-pq", "connection_string", "postgres:///taler-mailbox")
     98 
     99 	db, err := mailbox.OpenDatabase(psqlconn)
    100 	if err != nil {
    101 		log.Panic(err)
    102 	}
    103 	defer db.Close()
    104 	merchURL := iniCfg.GetString("mailbox", "merchant_baseurl_private", "http://merchant.mailbox/instances/myInstance")
    105 	merchToken := iniCfg.GetString("mailbox", "merchant_token", "secretAccessToken")
    106 	merch := merchant.NewMerchant(merchURL, merchToken)
    107 	m.Initialize(mailbox.MailboxConfig{
    108 		LibtoolVersion: ltversion,
    109 		Version:        version,
    110 		Datahome:       mailboxdatahome,
    111 		DB:             db,
    112 		Ini:            iniCfg,
    113 		Merchant:       merch,
    114 		Loglevel:       loglevel,
    115 	})
    116 	handleRequests(&m)
    117 }