taler-mailbox

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

main.go (2412B)


      1 // This file is part of taler-mailbox, the Taler Directory implementation.
      2 // Copyright (C) 2025 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 	"os"
     27 	"path"
     28 	"strings"
     29 
     30 	_ "github.com/lib/pq"
     31 	talerutil "github.com/schanzen/taler-go/pkg/util"
     32 	"rsc.io/getopt"
     33 
     34 	"gopkg.in/ini.v1"
     35 )
     36 
     37 var (
     38 	mailboxdatahome string
     39 	mailboxconfdir  string
     40 )
     41 
     42 func printHelp() {
     43 	fmt.Print("taler-mailbox-dbinit\n\n")
     44 	getopt.PrintDefaults()
     45 	fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n",
     46 		"Home page: https://taler.net\n",
     47 		"General help using GNU software: http://www.gnu.org/gethelp/\n")
     48 }
     49 
     50 func main() {
     51 	var cfg *ini.File
     52 	var err error
     53 	var cfgFlag = flag.String("c", "", "Configuration file to use")
     54 	getopt.Alias("c", "config")
     55 	var helpFlag = flag.Bool("h", false, "Print help")
     56 	getopt.Alias("h", "help")
     57 
     58 	getopt.Parse()
     59 	if *helpFlag {
     60 		printHelp()
     61 		return
     62 	}
     63 	cfgfile := path.Join(mailboxconfdir, "mailbox.conf")
     64 	if len(*cfgFlag) != 0 {
     65 		cfg, err = ini.Load(*cfgFlag)
     66 		if err != nil {
     67 			fmt.Printf("Failed to read config: %v\n", err)
     68 			os.Exit(1)
     69 		}
     70 	} else {
     71 		// FIXME also try in datahome
     72 		cfg, err = ini.LooseLoad(cfgfile)
     73 		if err != nil {
     74 			fmt.Printf("Failed to read config: %v\n", err)
     75 			os.Exit(1)
     76 		}
     77 	}
     78 	psqlconn := cfg.Section("mailbox-pq").Key("connection_string").MustString("postgres:///taler-mailbox")
     79 	segments := strings.Split(strings.Split(psqlconn, "?")[0], "/")
     80 	dbName := segments[len(segments)-1]
     81 
     82 	db, err := sql.Open("postgres", psqlconn)
     83 	if err != nil {
     84 		log.Panic(err)
     85 	}
     86 	defer db.Close()
     87 	err = talerutil.DBInit(db, mailboxdatahome, dbName, "taler-mailbox")
     88 	if err != nil {
     89 		log.Fatalf("%v\n", err)
     90 	}
     91 }