diff --git a/Dockerfile.backend b/Dockerfile.backend new file mode 100644 index 0000000..e069cbd --- /dev/null +++ b/Dockerfile.backend @@ -0,0 +1,29 @@ +# ========================= +# Build stage +# ========================= +FROM golang:1.25.5-alpine AS builder + +WORKDIR /app + +COPY backend/go.mod backend/go.sum ./ +RUN go mod download + +COPY backend/ ./ + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ + go build -o server ./cmd/server + + +# ========================= +# Runtime stage +# ========================= +FROM gcr.io/distroless/base-debian12 + +WORKDIR /app + +COPY --from=builder /app/server ./server + +EXPOSE 8081 + +USER nonroot:nonroot +ENTRYPOINT ["./server"] diff --git a/Dockerfile.frontend b/Dockerfile.frontend new file mode 100644 index 0000000..a45c13a --- /dev/null +++ b/Dockerfile.frontend @@ -0,0 +1,30 @@ +# ========================= +# Build stage +# ========================= +FROM node:20-alpine AS builder + +WORKDIR /app + +COPY frontend/studia/package*.json ./ +RUN npm ci + +COPY frontend/studia/ ./ +RUN npm run build + + +# ========================= +# Runtime stage +# ========================= +FROM nginx:1.27-alpine + +# Remove default nginx config +RUN rm /etc/nginx/conf.d/default.conf + +# Custom nginx config +COPY nginx.conf /etc/nginx/conf.d/default.conf + +# Vite build output +COPY --from=builder /app/dist /usr/share/nginx/html + +EXPOSE 3000 +CMD ["nginx", "-g", "daemon off;"] diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index b6a94ae..4d6d9ab 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -1,12 +1,13 @@ package main import ( - "log" "studia/internal/config" + "studia/internal/logger" "studia/internal/server" ) func main() { + logger.Init() cfg := config.New() @@ -16,8 +17,8 @@ func main() { cfg.DatabasePassword = "12345678" cfg.DatabaseName = "studia" - log.Println("Configuration loaded:", cfg) + logger.Log.Info().Msgf("Configuration loaded: %+v", cfg) - log.Println("Starting server...") + logger.Log.Info().Msg("Starting server...") server.StartServer(cfg) } diff --git a/backend/go.mod b/backend/go.mod index de61c9b..e611dec 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/gin-gonic/contrib v0.0.0-20250521004450-2b1292699c15 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect github.com/spf13/pflag v1.0.9 // indirect ) @@ -35,6 +36,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/quic-go/qpack v0.5.1 // indirect github.com/quic-go/quic-go v0.54.0 // indirect + github.com/rs/zerolog v1.34.0 github.com/spf13/cobra v1.10.2 github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.0 // indirect diff --git a/backend/go.sum b/backend/go.sum index 066ef99..120502b 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -4,6 +4,7 @@ github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZw github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -34,6 +35,7 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -49,6 +51,10 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= @@ -59,12 +65,16 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg= github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= +github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= @@ -96,7 +106,9 @@ golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs= golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4= diff --git a/backend/internal/database/database.go b/backend/internal/database/database.go index 18998d9..1a8f0ef 100644 --- a/backend/internal/database/database.go +++ b/backend/internal/database/database.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "studia/internal/config" + "studia/internal/logger" _ "github.com/lib/pq" // Import the PostgreSQL driver ) @@ -22,7 +23,7 @@ func New(cfg *config.Config) *sql.DB { WHERE table_schema = 'public' `) if err != nil { - log.Println("failed to query existing tables:", err) + logger.Log.Error().Err(err).Msg("Failed to query existing tables") } missing := checkTables(expectedTables, existing) @@ -32,7 +33,7 @@ func New(cfg *config.Config) *sql.DB { // Here you would normally run migrations to create the missing tables // For simplicity, we just log the missing tables } else { - log.Println("All expected tables are present.") + logger.Log.Info().Msg("All expected tables are present.") } return db @@ -41,7 +42,7 @@ func New(cfg *config.Config) *sql.DB { func setupDatabase(cfg *config.Config) *sql.DB { // Database connection setup logic here - log.Println(cfg) + logger.Log.Println(cfg) switch cfg.DatabaseDriver { case "postgres": // Setup Postgres connection diff --git a/backend/internal/logger/logger.go b/backend/internal/logger/logger.go new file mode 100644 index 0000000..f3ef37c --- /dev/null +++ b/backend/internal/logger/logger.go @@ -0,0 +1,46 @@ +// internal/logger/logger.go +package logger + +import ( + "io" + "os" + "time" + + "github.com/rs/zerolog" +) + +var Log zerolog.Logger + +// Init initializes the global logger. +// It configures console output, timestamps, and log level from environment. +func Init() { + // Determine output writer + consoleWriter := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: time.RFC3339, + } + + // You can switch to JSON output by replacing with os.Stdout directly: + // writer := os.Stdout + + // Create the global logger + Log = zerolog.New(consoleWriter). + Level(zerolog.TraceLevel).With().Caller().Logger(). + With(). + Timestamp(). + Logger() + + // Set log level from environment variable, default to InfoLevel + level := zerolog.InfoLevel + if lvlStr, ok := os.LookupEnv("LOG_LEVEL"); ok { + if parsedLevel, err := zerolog.ParseLevel(lvlStr); err == nil { + level = parsedLevel + } + } + zerolog.SetGlobalLevel(level) +} + +// SetOutput allows changing output (e.g., to a file) +func SetOutput(w io.Writer) { + Log = Log.Output(w) +} diff --git a/backend/internal/server/server.go b/backend/internal/server/server.go index bfc3a9f..4a9e9df 100644 --- a/backend/internal/server/server.go +++ b/backend/internal/server/server.go @@ -1,12 +1,10 @@ package server import ( - "fmt" - "log" - "os" "studia/internal/auth" "studia/internal/config" "studia/internal/database" + "studia/internal/logger" "time" "github.com/gin-contrib/cors" @@ -21,18 +19,18 @@ func StartServer(cfg *config.Config) { // 2. CORS-Konfiguration // Lese die Frontend-URL aus den Umgebungsvariablen - frontendURL := os.Getenv("FRONTEND_URL") + // frontendURL := os.Getenv("FRONTEND_URL") // Lokaler Fallback (wichtig für die Entwicklung) allowedOrigins := []string{ "http://localhost:5173", // Gängiger Vite-Dev-Port } - if frontendURL != "" { - allowedOrigins = append(allowedOrigins, frontendURL) - fmt.Printf("CORS: Erlaubte Produktiv-URL hinzugefügt: %s\n", frontendURL) + if cfg.FrontendURL != "" { + allowedOrigins = append(allowedOrigins, cfg.FrontendURL) + logger.Log.Printf("CORS: Erlaubte Produktiv-URL hinzugefügt: %s\n", cfg.FrontendURL) } else { - log.Println("ACHTUNG: FRONTEND_URL fehlt in den Umgebungsvariablen. Nur lokale URLs erlaubt.") + logger.Log.Error().Msg("ACHTUNG: FRONTEND_URL fehlt in den Umgebungsvariablen. Nur lokale URLs erlaubt.") } // CORS diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..eb855fc --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,24 @@ +version: "3.9" + +services: + backend: + build: + context: . + dockerfile: Dockerfile.backend + container_name: studia-backend + expose: + - "9090" + environment: + - PORT=9090 + restart: unless-stopped + + frontend: + build: + context: . + dockerfile: Dockerfile.frontend + container_name: studia-frontend + ports: + - "3000:3000" + depends_on: + - backend + restart: unless-stopped diff --git a/frontend/studia/src/api/user.tsx b/frontend/studia/src/api/user.tsx index 0beb401..99d36fe 100644 --- a/frontend/studia/src/api/user.tsx +++ b/frontend/studia/src/api/user.tsx @@ -11,11 +11,11 @@ export async function loginUser(email:string, password: string) { return res.json(); // { token: string } } -export async function registerUser(email:string, password: string){ +export async function registerUser(email:string, username:string, password: string){ const res = await fetch(`${API_URL}/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ email, password }), + body: JSON.stringify({ email,username, password }), }); if (!res.ok) throw new Error('Registrierung fehlgeschlagen'); return res.json(); // { token: string } diff --git a/frontend/studia/src/components/LoginModal.tsx b/frontend/studia/src/components/LoginModal.tsx index 7e5af9e..477de01 100644 --- a/frontend/studia/src/components/LoginModal.tsx +++ b/frontend/studia/src/components/LoginModal.tsx @@ -1,4 +1,4 @@ -import {loginUser} from "../api/user"; +import {loginUser, registerUser} from "../api/user"; import { useState } from "react"; export default function LoginModal({ isOpen, onClose, onSuccess }: any) { @@ -36,8 +36,12 @@ export default function LoginModal({ isOpen, onClose, onSuccess }: any) { } // TODO: Call registerUser API - // const res = await registerUser(email as string, username as string, password as string); - + const res = await registerUser(email as string, username as string, password as string); + if (!res.ok) { + alert("Registration failed!"); + return; + } + // TODO: Implement actual registration logic here console.log("Registering with:", email, password); // For now, let's just switch back to login after a "successful" registration diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..3a26763 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,17 @@ +server { + listen 3000; + + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri /index.html; + } + + location /api/ { + proxy_pass http://backend:8081; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } +}