main.go (3555B)
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 "gopkg.in/ini.v1" 25 "gorm.io/driver/postgres" 26 "log" 27 "net/http" 28 "os" 29 "path" 30 31 "github.com/schanzen/taler-go/pkg/merchant" 32 "rsc.io/getopt" 33 mailbox "taler.net/taler-mailbox/pkg/rest" 34 ) 35 36 var ( 37 m mailbox.Mailbox 38 ltversion string 39 version string 40 mailboxdatahome string 41 mailboxconfdir string 42 verbose bool 43 ) 44 45 func handleRequests(m *mailbox.Mailbox) { 46 log.Fatal(http.ListenAndServe(m.Cfg.Ini.Section("mailbox").Key("bind_to").MustString("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 verboseFlag = flag.Bool("v", false, "Verbose") 64 getopt.Alias("v", "verbose") 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 cfgfile := path.Join(mailboxconfdir, "mailbox.conf") 74 if len(*cfgFlag) != 0 { 75 cfgfile = *cfgFlag 76 } 77 verbose = *verboseFlag 78 loglevel := mailbox.LogInfo 79 for loglevelNum, loglevelString := range mailbox.LoglevelStringMap { 80 if loglevelString == *loglevelStringOpt { 81 loglevel = loglevelNum 82 } 83 } 84 m := mailbox.Mailbox{} 85 iniCfg, err := ini.Load(cfgfile) 86 if err != nil { 87 log.Printf("Failed to read config: %v", err) 88 os.Exit(1) 89 } 90 psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", 91 iniCfg.Section("mailbox-pq").Key("host").MustString("localhost"), 92 iniCfg.Section("mailbox-pq").Key("port").MustInt64(5432), 93 iniCfg.Section("mailbox-pq").Key("user").MustString("taler-mailbox"), 94 iniCfg.Section("mailbox-pq").Key("password").MustString("secret"), 95 iniCfg.Section("mailbox-pq").Key("db_name").MustString("taler-mailbox")) 96 db := postgres.Open(psqlconn) 97 merchURL := iniCfg.Section("mailbox").Key("merchant_baseurl_private").MustString("http://merchant.mailbox/instances/myInstance") 98 merchToken := iniCfg.Section("mailbox").Key("merchant_token").MustString("secretAccessToken") 99 merch := merchant.NewMerchant(merchURL, merchToken) 100 m.Initialize(mailbox.MailboxConfig{ 101 LibtoolVersion: ltversion, 102 Version: version, 103 Datahome: mailboxdatahome, 104 DB: db, 105 Ini: iniCfg, 106 Merchant: merch, 107 Loglevel: loglevel, 108 }) 109 handleRequests(&m) 110 }