From cab0dc9eeac34da121846b55f57a14623480d57b Mon Sep 17 00:00:00 2001 From: Achuz123 Date: Tue, 26 Aug 2025 01:24:42 +0530 Subject: [PATCH] feat: Add Docker support and configurable port --- Dockerfile | 32 ++++++++++++++++++++++++++++++++ cmd/server/main.go | 23 +++++++++++++++++------ docker-compose.yml | 16 ++++++++++++++++ internal/api/handler.go | 6 +++--- readme.md | 13 +++++++++++++ 5 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1df38fc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +# Stage 1: Build the Go application +FROM golang:1.23-alpine AS builder + +WORKDIR /app + +# Copy Go module files and download dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the rest of the source code +COPY . . + +# Build the server binary specifically +# Change is on this line: ./cmd -> ./cmd/server +RUN CGO_ENABLED=0 go build -o /app/potok-server ./cmd/server + +# Stage 2: Create the final, lightweight production image +FROM alpine:latest + +WORKDIR /app + +# Copy the migrations folder, which is needed at runtime +COPY --from=builder /app/migrations /migrations + +# Copy the compiled application binary from the builder stage +COPY --from=builder /app/potok-server . + +# Expose the port the application runs on +EXPOSE 8080 + +# The command to run the application +CMD ["/app/potok-server"] diff --git a/cmd/server/main.go b/cmd/server/main.go index ebb1376..fd53b74 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,25 +3,36 @@ package main import ( "fmt" "log" + "os" // <-- Import the "os" package "github.com/michaeltukdev/Potok/internal/api" "github.com/michaeltukdev/Potok/internal/database" ) func main() { - db, err := database.InitDB("potok.db") + // Read DATABASE_PATH from environment, with a default if not set + dbPath := os.Getenv("DATABASE_PATH") + if dbPath == "" { + dbPath = "potok.db" // Default database path + } + db, err := database.InitDB(dbPath) if err != nil { - log.Fatal(err) + log.Fatal("Failed to init DB:", err) } - fmt.Println("Database running...") if err := database.RunMigrations(db); err != nil { log.Fatal("Failed to run migrations:", err) } - fmt.Println("Migrations completed...") - fmt.Println("Starting HTTP server on :8080") - api.StartServer() + // Read PORT from environment, with a default if not set + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + fmt.Println("Starting HTTP server on :" + port) + // Pass the port to the StartServer function + api.StartServer(port) } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9fb0b50 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +services: + app: + build: . + container_name: potok-server + ports: + - "8081:8080" + volumes: + - potok-database:/app/database + - potok-vault:/app/vault + environment: + - PORT=8080 + - DATABASE_PATH=/app/database/potok.db + +volumes: + potok-database: + potok-vault: diff --git a/internal/api/handler.go b/internal/api/handler.go index b61d00a..7d34a6e 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -16,7 +16,7 @@ import ( "github.com/michaeltukdev/Potok/internal/middleware" ) -func StartServer() { +func StartServer(port string) { // <-- 1. Accept the port as an argument r := mux.NewRouter() api := r.PathPrefix("/").Subrouter() @@ -35,8 +35,8 @@ func StartServer() { // Authenticated user info api.HandleFunc("/me", handleMe).Methods("GET") - log.Println("Starting server on :8080") - http.ListenAndServe(":8080", r) + log.Println("Starting server on :" + port) // <-- 2. Use the port variable in the log + http.ListenAndServe(":" + port, r) // <-- 3. Use the port variable to start the server } // handleVaults returns all vaults for the authenticated user. diff --git a/readme.md b/readme.md index db064db..9788b5a 100644 --- a/readme.md +++ b/readme.md @@ -90,3 +90,16 @@ Contributions are welcome! Please open issues or pull requests. - [spf13/cobra](https://github.com/spf13/cobra) - [zalando/go-keyring](https://github.com/zalando/go-keyring) - [fsnotify](https://github.com/fsnotify/fsnotify) + + + +## Running with Docker +You can run the Potok server and its database with a single command using Docker. + +Make sure you have Docker and Docker Compose installed. + +From the root of the project, run the following command: + +docker compose up --build + +The server will be available at http://localhost:8080.