agendas/main.go
2024-12-30 15:00:42 -05:00

76 lines
1.3 KiB
Go

package main
import (
"context"
"fmt"
"log"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
"git.agecem.com/bottin/bottin/v10/pkg/bottin"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/term"
)
// Entry
func init() {}
func main() {
cfg := Config{}
cfg.Bottin.Host = "api.bottin.agecem.com"
cfg.Bottin.Port = 443
cfg.Bottin.TLS.Enabled = true
ctx := context.TODO()
fmt.Println("bottin password (no echo): ")
password, err := term.ReadPassword(0)
if err != nil {
log.Fatal(err)
}
cfg.Bottin.Key = string(password)
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:
var protocol string
if cfg.Bottin.TLS.Enabled {
protocol = "https"
} else {
protocol = "http"
}
bottinClient := bottin.APIClient{Caller: voki.New(
http.DefaultClient,
cfg.Bottin.Host,
cfg.Bottin.Key,
cfg.Bottin.Port,
protocol,
)}
// connect to db
dbPool, err := pgxpool.New(ctx, "postgres://agendas:agendas@localhost:5432/agendas")
if err != nil {
return err
}
defer dbPool.Close()
dbClient := DBClient{Pool: dbPool}
if err := RunServer(ctx, cfg, &bottinClient, &dbClient); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
}