99 lines
2 KiB
Go
99 lines
2 KiB
Go
package presences
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"codeberg.org/vlbeaudoin/voki/v3"
|
|
"git.agecem.com/bottin/bottin/v11"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func init() {
|
|
// rootCmd
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.bottin.presences.yaml)")
|
|
|
|
if err := BindFlags(rootCmd.PersistentFlags()); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
var rootCmd = &cobra.Command{
|
|
Use: "presences",
|
|
//Args: cobra.(0),
|
|
Short: "Gestion de présences en Assemblée Générale",
|
|
Run: func(c *cobra.Command, args []string) {
|
|
log.Println("Starting bottin/presences microservice")
|
|
|
|
ctx := context.TODO()
|
|
|
|
cfg, err := ParseConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
if args[0] == "debugConfig" {
|
|
log.Println("debug: Printing current config")
|
|
cfgAsBytes, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
log.Fatal("Cannot marshal config for printing:", err)
|
|
}
|
|
fmt.Println(string(cfgAsBytes))
|
|
}
|
|
}
|
|
|
|
if err := run(ctx, cfg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
func run(ctx context.Context, cfg Config) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
bottinClient := bottin.APIClient{Caller: voki.New(
|
|
http.DefaultClient,
|
|
cfg.Bottin.Host,
|
|
cfg.Bottin.Key,
|
|
cfg.Bottin.Port,
|
|
func() string {
|
|
if cfg.Bottin.TLS.Enabled {
|
|
return "https"
|
|
} else {
|
|
return "http"
|
|
}
|
|
}(),
|
|
)}
|
|
|
|
// connect to db
|
|
dbPool, err := pgxpool.New(ctx,
|
|
fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
|
|
cfg.DB.Username,
|
|
cfg.DB.Password,
|
|
cfg.DB.Host,
|
|
cfg.DB.Port,
|
|
cfg.DB.Database,
|
|
cfg.DB.SSLMode,
|
|
))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dbPool.Close()
|
|
|
|
dbClient := DBClient{Pool: dbPool}
|
|
|
|
if err := RunUIServer(ctx, cfg, &bottinClient, &dbClient); err != nil && err != http.ErrServerClosed {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|