Wire everything together, config and transaction implementation

This commit is contained in:
2026-06-04 23:43:07 +02:00
parent c7ac17559d
commit e19e5bf3c3
5 changed files with 374 additions and 0 deletions

65
main.go Normal file
View File

@@ -0,0 +1,65 @@
package main
import (
"context"
"log/slog"
"os"
"connectrpc.com/connect"
"connectrpc.com/validate"
"git.kocoder.xyz/vt/financial-tracking-service/internal/config"
"git.kocoder.xyz/vt/financial-tracking-service/internal/database"
"git.kocoder.xyz/vt/financial-tracking-service/internal/proto/transaction/v1/transactionv1connect"
"git.kocoder.xyz/vt/financial-tracking-service/internal/service"
sdb "git.kocoder.xyz/vt/service-base/database"
"git.kocoder.xyz/vt/service-base/middleware"
"git.kocoder.xyz/vt/service-base/rpc"
"git.kocoder.xyz/vt/service-base/server"
"git.kocoder.xyz/vt/service-base/telemetry"
_ "github.com/lib/pq"
)
func main() {
telemetry.SetupFallbackLogger()
conf := config.Read()
ctx := context.Background()
shutdown, err := telemetry.Init(ctx, conf.OTLP_ENDPOINT, "financial-tracking-server")
if err != nil {
slog.Error("Failed to initialize telemetry", slog.String("err", err.Error()))
}
defer func() {
if err := shutdown(ctx); err != nil {
slog.Error("Error shutting down telemetry", slog.String("err", err.Error()))
}
}()
// Connect to DB and wrap with otelsql for database tracing
db, err := sdb.OpenPostgres(conf.DB_URL)
if err != nil {
slog.Error("failed to connect to database", slog.String("err", err.Error()))
os.Exit(1)
}
defer db.Close()
dbQueries := database.New(db)
mux := rpc.NewServeMux()
path, handler := transactionv1connect.NewTransactionServiceHandler(
service.NewTransactionService(conf, dbQueries),
connect.WithInterceptors(validate.NewInterceptor()),
)
mux.RegisterConnect(transactionv1connect.TransactionServiceName, path, handler)
mux.MountReflection()
c := middleware.NewCORS(conf.ALLOWED_ORIGINS)
corsMux := c.Handler(mux)
// Start server with OpenTelemetry tracing, HTTP/1.1 & HTTP/2 (h2c) support
if err := server.ListenAndServeH2C(conf.LISTEN_ON, corsMux, "financial-tracking-server"); err != nil {
slog.Error("Server failed", slog.String("err", err.Error()))
}
}