diff --git a/kontor-gin/Containerfile b/kontor-gin/Containerfile new file mode 100644 index 0000000..c90b9fe --- /dev/null +++ b/kontor-gin/Containerfile @@ -0,0 +1,25 @@ +# ---------- Stage 1: Build ---------- +FROM docker.io/library/golang:1.26-alpine AS builder +# Set the working directory +WORKDIR /app +# Ensure a portable, static-ish binary +ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 +# Copy and download dependencies +COPY go.mod go.sum ./ +RUN go mod download +# Copy the source code +COPY . . +# Build the Go application (strip debug info for smaller size) +RUN go build -trimpath -ldflags="-s -w" -o kontor cmd/kontor/main.go + +# ---------- Stage 2: Final ---------- +FROM alpine:latest +# Set the working directory +WORKDIR /app +# Install runtime dependencies you actually need +RUN apk add --no-cache ca-certificates tzdata curl +# Copy the binary and set ownership +COPY --from=builder /app/kontor /app/kontor +ENV DB_HOST=postgres DB_USER=kontor DB_PASSWORD=kontor DB_NAME=kontor DB_PORT=5432 GIN_MODE=release +# Set the entrypoint command +ENTRYPOINT ["/app/kontor"] diff --git a/kontor-gin/cmd/kontor/main.go b/kontor-gin/cmd/kontor/main.go new file mode 100644 index 0000000..b42b24f --- /dev/null +++ b/kontor-gin/cmd/kontor/main.go @@ -0,0 +1,74 @@ +package main + +import ( + // "kontor-api-gin/pkg/handler" + "kontor-api-gin/pkg/routes" + "kontor-api-gin/pkg/schema" + "log" + "os" + + "github.com/gin-gonic/gin" +) + +func main() { + log.Println("Kontor started") + + // Set Gin mode based on environment + // Production mode disables debug output + if os.Getenv("GIN_MODE") == "release" { + gin.SetMode(gin.ReleaseMode) + } + + // Connect to database + schema.Connect() + + // Run migrations + schema.RunMigrations() + + // Create Gin router with default middleware + // Default includes Logger and Recovery middleware + router := gin.Default() + + // Add CORS middleware for frontend access + router.Use(corsMiddleware()) + + // Setup application routes + api := routes.SetupApiRoutes(router) + routes.SetupMediaRoutes(api) + + // Health check endpoint + router.GET("/health", func(c *gin.Context) { + c.JSON(200, gin.H{ + "status": "healthy", + }) + }) + + // Start server + port := os.Getenv("PORT") + if port == "" { + port = "8550" + } + + log.Printf("Server starting on port %s", port) + if err := router.Run(":" + port); err != nil { + log.Fatal("Failed to start server:", err) + } + + log.Println("Kontor finished") +} + +// corsMiddleware adds CORS headers for cross-origin requests +func corsMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(204) + return + } + + c.Next() + } +} diff --git a/kontor-gin/go.mod b/kontor-gin/go.mod new file mode 100644 index 0000000..b628a62 --- /dev/null +++ b/kontor-gin/go.mod @@ -0,0 +1,46 @@ +module kontor-api-gin + +go 1.26.8 + +require ( + github.com/bytedance/gopkg v0.1.4 // indirect + github.com/bytedance/sonic v1.15.4 // indirect + github.com/bytedance/sonic/loader v0.5.2 // indirect + github.com/cloudwego/base64x v0.1.7 // indirect + github.com/gabriel-vasile/mimetype v1.4.15 // indirect + github.com/gin-contrib/sse v1.1.2 // indirect + github.com/gin-gonic/gin v1.12.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.4 // indirect + github.com/goccy/go-json v0.10.6 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.11.0 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.4.0 // indirect + github.com/leodido/go-urn v1.5.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.4.3 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.62.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.2 // indirect + go.mongodb.org/mongo-driver/v2 v2.9.1 // indirect + golang.org/x/arch v0.31.0 // indirect + golang.org/x/crypto v0.57.0 // indirect + golang.org/x/net v0.59.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect + gorm.io/driver/postgres v1.6.3 // indirect + gorm.io/gorm v1.31.2 // indirect +) diff --git a/kontor-gin/go.sum b/kontor-gin/go.sum new file mode 100644 index 0000000..757df08 --- /dev/null +++ b/kontor-gin/go.sum @@ -0,0 +1,98 @@ +github.com/bytedance/gopkg v0.1.4 h1:oZnQwnX82KAIWb7033bEwtxvTqXcYMxDBaQxo5JJHWM= +github.com/bytedance/gopkg v0.1.4/go.mod h1:v1zWfPm21Fb+OsyXN2VAHdL6TBb2L88anLQgdyje6R4= +github.com/bytedance/sonic v1.15.4 h1:FgtV/4aBHpla9AxuMpuuzVUpa/Cf3izufkxNmnEzdI8= +github.com/bytedance/sonic v1.15.4/go.mod h1:8e51yTPdY8M6t+vvGL1c2Y1xL9i+frEeIAQAEl75NUc= +github.com/bytedance/sonic/loader v0.5.2 h1:0QtP1gevc1OZ6/H8Lb9BRZiCXd1Ftjd3OKuj1T1lBIo= +github.com/bytedance/sonic/loader v0.5.2/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.7 h1:NppS+Fgzg5ovhn4NkUXaDT3x9jldgH5ToMCqzBSi2zI= +github.com/cloudwego/base64x v0.1.7/go.mod h1:Cu1PV9zfrSf7ET2tIbWbbEy7jO7HHJ13q4X2SQ8aWYg= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI= +github.com/gabriel-vasile/mimetype v1.4.15/go.mod h1:azpTcoLcDZRNgFou5j+APrqQx9HqVPWa6ijYQIIVswQ= +github.com/gin-contrib/sse v1.1.2 h1:MU2fgl1RrdYTMcgJLtz2kJF+vPg3xrqaaKfUUU18tCo= +github.com/gin-contrib/sse v1.1.2/go.mod h1:QXzuVkA0YO7o/gun03UI1Q+FTI8ZV/n5t03kIQAI89s= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.4 h1:9Rcod2ZPO6mOEG6b4GqyoHE/H6//Ze0RuhOo1hT1x0w= +github.com/go-playground/validator/v10 v10.30.4/go.mod h1:numpT+RPLE91R9oYWMY/R9zRgJBewr3IXHko4OISPpk= +github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU= +github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/leodido/go-urn v1.5.0 h1:pLqT2kq1zpHW/1D18QMjMpdtX7cekxqtJJjg5ANyWw0= +github.com/leodido/go-urn v1.5.0/go.mod h1:9BORnCDhdPBJNDEX+w1bJisa8yOKYi116VeO96s4ifE= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= +github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.62.0 h1:ZHDjCk5OacATwGvs8PWE97CTvX7AqZiVoW7++ZOXTf8= +github.com/quic-go/quic-go v0.62.0/go.mod h1:RAro2j2yN9a9EiPACLHT9IB2NXCvGQmmo/alT0yYI0w= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.2 h1:zkEASHHyEClGeURfgNT9PJZVfAbs9oEX9QXggwWNJbc= +github.com/ugorji/go/codec v1.3.2/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +go.mongodb.org/mongo-driver/v2 v2.9.1 h1:jewiFs2m1/VOQp8qhFshX6hWZ+EAXDhZHXExAUMcOgQ= +go.mongodb.org/mongo-driver/v2 v2.9.1/go.mod h1:SHKN0IWkKmEVGHLjXnni6s4wPKX4v86FTgOeJJFuXcA= +golang.org/x/arch v0.31.0 h1:22MlEb14/O/EPCYHFxsDdv5TuLD5dMjT5e2QeJw4ULk= +golang.org/x/arch v0.31.0/go.mod h1:KcJSod3cqT2dKcjBxqTyGfbumNikqU9p5tHJinPJnuY= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= +golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.6.3 h1:bAn6O2pUa8LtpWEvL5NFU4+52Tfx8Ut7IVaIacCLcI0= +gorm.io/driver/postgres v1.6.3/go.mod h1:0c4fQA44XhOklXDkgtuKqysHCycTa5i9e3EIpDGCwXk= +gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo= +gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= diff --git a/kontor-gin/pkg/handlers/media.go b/kontor-gin/pkg/handlers/media.go new file mode 100644 index 0000000..d41acf3 --- /dev/null +++ b/kontor-gin/pkg/handlers/media.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "kontor-api-gin/pkg/schema" + "log" + "net/http" + + "github.com/gin-gonic/gin" +) + +func ListMediaFiles(c *gin.Context) { + var files []schema.MediaFile + + // query := schema.DB.Model(&schema.MediaFile{}) + // result := query.Find(&files) + result := schema.DB.Find(&files) + log.Printf("found %d MediaFiles", result.RowsAffected) + c.JSON(http.StatusOK, gin.H{ + "message": "All MediaFiles", + "files": files, + }) +} diff --git a/kontor-gin/pkg/routes/api.go b/kontor-gin/pkg/routes/api.go new file mode 100644 index 0000000..41c6bfe --- /dev/null +++ b/kontor-gin/pkg/routes/api.go @@ -0,0 +1,10 @@ +package routes + +import ( + "github.com/gin-gonic/gin" +) + +func SetupApiRoutes(router *gin.Engine) *gin.RouterGroup { + v1 := router.Group("api/v1") + return v1 +} diff --git a/kontor-gin/pkg/routes/media.go b/kontor-gin/pkg/routes/media.go new file mode 100644 index 0000000..cc89bba --- /dev/null +++ b/kontor-gin/pkg/routes/media.go @@ -0,0 +1,16 @@ +package routes + +import ( + "kontor-api-gin/pkg/handlers" + + "github.com/gin-gonic/gin" +) + +func SetupMediaRoutes(router *gin.RouterGroup) { + media := router.Group("/media") + + files := media.Group("/files") + { + files.GET("", handlers.ListMediaFiles) + } +} diff --git a/kontor-gin/pkg/schema/base.go b/kontor-gin/pkg/schema/base.go new file mode 100644 index 0000000..f247963 --- /dev/null +++ b/kontor-gin/pkg/schema/base.go @@ -0,0 +1,22 @@ +package schema + +import ( + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type Base struct { + ID string `gorm:"type:string;primary_key;"` + CreatedAt time.Time `json:"created_date"` + UpdatedAt time.Time `json:"last_modified_date"` + DeletedAt *time.Time `json:"deleted_date"` + Version int `gorm:"default:0" json:"version"` +} + +// BeforeCreate will set a UUID rather than numeric ID. +func (b *Base) BeforeCreate(tx *gorm.DB) (err error) { + b.ID = uuid.New().String() + return +} diff --git a/kontor-gin/pkg/schema/database.go b/kontor-gin/pkg/schema/database.go new file mode 100644 index 0000000..ce647f9 --- /dev/null +++ b/kontor-gin/pkg/schema/database.go @@ -0,0 +1,69 @@ +package schema + +import ( + "fmt" + "log" + "os" + "time" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + +) + +var DB *gorm.DB + +func Connect() { + // Build the connection string from environment variables + // This keeps sensitive credentials out of the codebase + dsn := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%s sslmode=disable", + os.Getenv("DB_HOST"), + os.Getenv("DB_USER"), + os.Getenv("DB_PASSWORD"), + os.Getenv("DB_NAME"), + os.Getenv("DB_PORT"), + ) + + // Configure GORM with custom settings + // The logger helps with debugging SQL queries during development + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + }) + if err != nil { + log.Fatal("Failed to connect to database:", err) + } + + // Get the underlying SQL database connection for pool configuration + sqlDB, err := db.DB() + if err != nil { + log.Fatal("Failed to get database instance:", err) + } + + // Configure connection pool settings + // MaxIdleConns: number of connections kept open when idle + sqlDB.SetMaxIdleConns(10) + // MaxOpenConns: maximum total connections to the database + sqlDB.SetMaxOpenConns(100) + // ConnMaxLifetime: how long a connection can be reused + sqlDB.SetConnMaxLifetime(time.Hour) + + DB = db + log.Println("Database connected successfully") +} + +func RunMigrations() { + // AutoMigrate creates tables, missing columns, and missing indexes + // It will NOT delete unused columns to protect your data + err := DB.AutoMigrate( + &MediaActor{}, + &MediaFile{}, + &MediaActorFile{}, + ) + if err != nil { + log.Fatal("Migration failed:", err) + } + + log.Println("Database migration completed") +} diff --git a/kontor-gin/pkg/schema/media.go b/kontor-gin/pkg/schema/media.go new file mode 100644 index 0000000..7664a20 --- /dev/null +++ b/kontor-gin/pkg/schema/media.go @@ -0,0 +1,76 @@ +package schema + +import ( +) + +type MediaFile struct { + Base + + CloudLink string `json:"cloud_link"` + FileName string `json:"file_name"` + Path string `json:"path"` + Review bool `json:"review"` + Title string `gorm:"size:300" json:"title"` + WebLink string `gorm:"uniqueIndex" json:"url"` + ShouldDownload bool `json:"should_download"` + + MediaActorFiles []MediaActorFile `gorm:"foreignKey:MediaFileID" json:"media_actor_files"` +} + +func (media_file *MediaFile) TableName() string { + return "media_file" +} + +type MediaActor struct { + Base + + Name string `json:"name"` + WebLink string `gorm:"uniqueIndex" json:"url"` + + MediaActorFiles []MediaActorFile `gorm:"foreignKey:MediaActorID" json:"media_actor_files"` +} + +func (media_actor *MediaActor) TableName() string { + return "media_actor" +} + +type MediaActorFile struct { + Base + + MediaActorID *string `json:"media_actor_id"` + MediaActor *MediaActor `gorm:"foreignKey:MediaActorID" json:"media_actor"` + MediaFileID *string `json:"media_file_id"` + MediaFile *MediaFile `gorm:"foreignKey:MediaFileID" json:"media_file"` +} + +func (media_file *MediaActorFile) TableName() string { + return "media_actor_file" +} + +// type MediaArticle struct { +// bun.BaseModel `bun:"table:media_article"` + +// ID string `bun:"id,pk"` +// CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"` +// UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"` +// Version int `bun:"version,default:0"` +// Review bool `bun:"review"` +// Title string `bun:"title"` +// WebLink string `bun:"url,unique:url"` +// } + +// type MediaVideo struct { +// bun.BaseModel `bun:"table:media_article"` + +// ID string `bun:"id,pk"` +// CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"` +// UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"` +// Version int `bun:"version,default:0"` +// CloudLink string `bun:"cloud_link"` +// FileName string `bun:"file_name"` +// Path string `bun:"path"` +// Review bool `bun:"review"` +// Title string `bun:"title"` +// WebLink string `bun:"url,unique:url"` +// ShouldDownload bool `bun:"should_download"` +// }