feat: ship backend logs to OmniLogger
Lint / Lint (push) Successful in 1m12s
Security / Secrets (gitleaks) (push) Successful in 59s
Security / Dependencies (Trivy) (push) Successful in 49s
Tests / Dashboard checks (push) Successful in 52s
Security / Code (Semgrep) (push) Successful in 1m33s
Tests / Go backend (push) Failing after 1m1s
Docker / Docker Build & Push (push) Failing after 5m16s

This commit is contained in:
Gabriel20xx committed 2026-11-20 21:42:42 +01:00
1 parent ed3544acfe
commit 01b18be1e4
8 files changed
+400 -109

No files matched your search

+27 -23
View File
@@ -6,8 +6,9 @@ import (
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net"
"net/http"
"os"
@@ -20,6 +21,7 @@ import (
sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
"repos.gfranz.ch/Gabriel20xx/shared/backend/coreconfig"
"repos.gfranz.ch/Gabriel20xx/shared/backend/logshipper"
sharedpostgres "repos.gfranz.ch/Gabriel20xx/shared/backend/postgres"
)
@@ -53,25 +55,26 @@ func main() {
if len(os.Args) > 1 && os.Args[1] == "remap-users" {
os.Exit(runRemapUsers(os.Args[2:], os.Stdout))
}
log.SetOutput(io.MultiWriter(os.Stderr, omniShipper.Writer()))
port := envOrDefault("PORT", "8080")
connectionString := strings.TrimSpace(os.Getenv("DATABASE_URL"))
if connectionString == "" {
fmt.Fprintln(os.Stderr, "DATABASE_URL environment variable is required")
os.Exit(1)
logErrorln(logshipper.LevelCritical, "DATABASE_URL environment variable is required")
exitAfterFlush(1)
}
internalServiceSecret := strings.TrimSpace(os.Getenv("SECRET"))
trustedProxies, err := parseTrustedProxies(os.Getenv("TRUSTED_PROXIES"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
logErrorln(logshipper.LevelCritical, err)
exitAfterFlush(1)
}
// PRODUCTION=true refuses plain HTTP. This process has no TLS of its own, so
// the encrypted hop has to be a reverse proxy listed in TRUSTED_PROXIES.
production := strings.EqualFold(strings.TrimSpace(os.Getenv("PRODUCTION")), "true")
if production && len(trustedProxies) == 0 {
fmt.Fprintln(os.Stderr, "PRODUCTION=true requires TRUSTED_PROXIES: HTTPS must be terminated by a reverse proxy listed there")
os.Exit(1)
logErrorln(logshipper.LevelCritical, "PRODUCTION=true requires TRUSTED_PROXIES: HTTPS must be terminated by a reverse proxy listed there")
exitAfterFlush(1)
}
ctx, cancel := context.WithCancel(context.Background())
@@ -79,8 +82,8 @@ func main() {
database, err := connectWithRetry(ctx, connectionString)
if err != nil {
fmt.Fprintf(os.Stderr, "database initialization failed: %v\n", err)
os.Exit(1)
logErrorf(logshipper.LevelCritical, "database initialization failed: %v\n", err)
exitAfterFlush(1)
}
defer database.close()
go database.pruneExpiredSessions(ctx)
@@ -98,11 +101,11 @@ func main() {
authDisabled := strings.EqualFold(os.Getenv("AUTH_DISABLED"), "true")
switch {
case authDisabled:
fmt.Println("auth: DISABLED (AUTH_DISABLED=true) -- every request is treated as authenticated")
logPrintln(logshipper.LevelWarn, "auth: DISABLED (AUTH_DISABLED=true) -- every request is treated as authenticated")
case authGuardURL != "":
fmt.Printf("auth: AuthGuard at %s\n", authGuardURL)
logPrintf(logshipper.LevelInfo, "auth: AuthGuard at %s\n", authGuardURL)
default:
fmt.Println("auth: PENDING (AUTHGUARD_URL not set) -- waiting for ProjectConfigurator to push an AuthGuard URL; auth calls fail closed until then")
logPrintln(logshipper.LevelWarn, "auth: PENDING (AUTHGUARD_URL not set) -- waiting for ProjectConfigurator to push an AuthGuard URL; auth calls fail closed until then")
}
auth := newAuthService(database, authGuardURL, strings.EqualFold(os.Getenv("COOKIE_SECURE"), "true"), authDisabled)
@@ -115,6 +118,7 @@ func main() {
// is a no-op when this service was constructed in auth-disabled mode (AUTH_DISABLED=true above).
carrierNodeURL := strings.TrimSpace(os.Getenv("CARRIERNODE_URL"))
coreConfig := coreconfig.NewStore(coreconfig.Info{AuthGuardURL: authGuardURL, CarrierNodeURL: carrierNodeURL})
omniShipper.Follow(coreConfig)
coreConfig.OnUpdate(func(info coreconfig.Info) {
if info.AuthGuardURL != "" {
auth.SetAuthGuardBaseURL(info.AuthGuardURL)
@@ -132,8 +136,8 @@ func main() {
webRoot, err := fs.Sub(webFS, "web")
if err != nil {
fmt.Fprintf(os.Stderr, "embedded web assets missing: %v\n", err)
os.Exit(1)
logErrorf(logshipper.LevelCritical, "embedded web assets missing: %v\n", err)
exitAfterFlush(1)
}
mux := http.NewServeMux()
@@ -142,9 +146,9 @@ func main() {
handler := withSecurityHeaders(withGzip(withCORS(allowedOriginsFromEnv(), mux)))
if production {
handler = withHTTPSRequired(trustedProxies, handler)
fmt.Println("PRODUCTION=true: plain HTTP requests are refused unless they arrive over TLS or through a trusted proxy that sets X-Forwarded-Proto: https")
logPrintln(logshipper.LevelInfo, "PRODUCTION=true: plain HTTP requests are refused unless they arrive over TLS or through a trusted proxy that sets X-Forwarded-Proto: https")
} else {
fmt.Fprintln(os.Stderr, "WARNING: TLS is off in this process, so passwords, session cookies and sync tokens cross the network unencrypted unless a TLS proxy is in front. Set PRODUCTION=true to refuse plain HTTP.")
logErrorln(logshipper.LevelWarn, "WARNING: TLS is off in this process, so passwords, session cookies and sync tokens cross the network unencrypted unless a TLS proxy is in front. Set PRODUCTION=true to refuse plain HTTP.")
}
httpServer := &http.Server{
@@ -156,11 +160,11 @@ func main() {
IdleTimeout: 120 * time.Second,
}
fmt.Printf("CollabTable Go server listening on :%s\n", port)
fmt.Printf("Database: PostgreSQL (pgx)\n")
logPrintf(logshipper.LevelInfo, "CollabTable Go server listening on :%s\n", port)
logPrintf(logshipper.LevelInfo, "Database: PostgreSQL (pgx)\n")
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "server error: %v\n", err)
os.Exit(1)
logErrorf(logshipper.LevelCritical, "server error: %v\n", err)
exitAfterFlush(1)
}
}
@@ -172,7 +176,7 @@ func connectWithRetry(ctx context.Context, connectionString string) (*DB, error)
if err == nil {
if schemaErr := database.initSchema(ctx); schemaErr == nil {
if attempt > 1 {
fmt.Printf("[DB] Connected after %d attempt(s)\n", attempt)
logPrintf(logshipper.LevelInfo, "[DB] Connected after %d attempt(s)\n", attempt)
}
return database, nil
} else {
@@ -181,11 +185,11 @@ func connectWithRetry(ctx context.Context, connectionString string) (*DB, error)
}
}
fmt.Fprintf(os.Stderr, "[DB] Initialization attempt %d failed: %v\n", attempt, err)
logErrorf(logshipper.LevelWarn, "[DB] Initialization attempt %d failed: %v\n", attempt, err)
if retryOptions.MaxAttempts > 0 && attempt >= retryOptions.MaxAttempts {
return nil, err
}
fmt.Printf("[DB] Retrying in %s...\n", retryOptions.Interval)
logPrintf(logshipper.LevelWarn, "[DB] Retrying in %s...\n", retryOptions.Interval)
select {
case <-ctx.Done():
return nil, ctx.Err()