taler-mailbox

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

main.go (3309B)


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