main.go (3745B)
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 // @title Taler Mailbox API 20 // @description The Taler Mailbox service provides encrypted message delivery to wallets identified by their public key. 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 import ( 28 "flag" 29 "fmt" 30 "log" 31 "net/http" 32 "os" 33 "path" 34 "path/filepath" 35 36 _ "github.com/lib/pq" 37 "github.com/schanzen/taler-go/pkg/merchant" 38 "github.com/schanzen/taler-go/pkg/util" 39 "rsc.io/getopt" 40 mailbox "taler.net/taler-mailbox/pkg/rest" 41 ) 42 43 var ( 44 m mailbox.Mailbox 45 ltversion string 46 version string 47 mailboxdatahome string 48 mailboxconfdir string 49 ) 50 51 func handleRequests(m *mailbox.Mailbox) { 52 log.Fatal(http.ListenAndServe(m.Cfg.Ini.GetString("mailbox", "bind_to", "localhost:11000"), m.Router)) 53 } 54 55 func printHelp() { 56 fmt.Print("taler-mailbox\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", "mailbox.conf", "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 versionFlag = flag.Bool("v", false, "Print version") 70 getopt.Alias("v", "version") 71 var helpFlag = flag.Bool("h", false, "Print help") 72 getopt.Alias("h", "help") 73 74 getopt.Parse() 75 if *helpFlag { 76 printHelp() 77 return 78 } 79 if *versionFlag { 80 fullName, err := os.Executable() 81 if err != nil { 82 log.Panic(err) 83 } 84 fmt.Printf("%s %s", filepath.Base(fullName), version) 85 return 86 } 87 cfgfile := path.Join(mailboxconfdir, "taler-mailbox.conf") 88 if len(*cfgFlag) != 0 { 89 cfgfile = *cfgFlag 90 } 91 loglevel := mailbox.LogInfo 92 for loglevelNum, loglevelString := range mailbox.LoglevelStringMap { 93 if loglevelString == *loglevelStringOpt { 94 loglevel = loglevelNum 95 } 96 } 97 m := mailbox.Mailbox{} 98 iniCfg, err := util.LoadConfiguration(cfgfile) 99 if err != nil { 100 log.Printf("Failed to read config: %v", err) 101 os.Exit(1) 102 } 103 psqlconn := iniCfg.GetString("mailbox-pq", "connection_string", "postgres:///taler-mailbox") 104 105 db, err := mailbox.OpenDatabase(psqlconn) 106 if err != nil { 107 log.Panic(err) 108 } 109 defer db.Close() 110 merchURL := iniCfg.GetString("mailbox", "merchant_baseurl_private", "http://merchant.mailbox/instances/myInstance") 111 merchToken := iniCfg.GetString("mailbox", "merchant_token", "secretAccessToken") 112 merch := merchant.NewMerchant(merchURL, merchToken) 113 m.Initialize(mailbox.MailboxConfig{ 114 LibtoolVersion: ltversion, 115 Version: version, 116 Datahome: mailboxdatahome, 117 DB: db, 118 Ini: iniCfg, 119 Merchant: merch, 120 Loglevel: loglevel, 121 }) 122 handleRequests(&m) 123 }