diff --git a/.gitignore b/.gitignore index f2e28c8..ed3f651 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -data/boxes.db -images/* \ No newline at end of file +data/* +images/* diff --git a/Dockerfile b/Dockerfile index 1433338..3bed61f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,39 @@ -FROM golang:1.21-alpine AS builder +# Stage 1: Build the Go binary +FROM golang:1.17-alpine AS builder +# Install build tools and SQLite development libraries +RUN apk add --no-cache gcc musl-dev sqlite-dev + +# Set the working directory inside the container WORKDIR /app -# Copy the source code +# Copy the Go modules files and download dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the rest of the application source code COPY . . # Build the Go application -RUN go mod tidy -RUN go build -o main . +RUN go build -o boxes-api -# Mount the data directory -VOLUME /app/data +# Stage 2: Create a minimal image with the Go binary +FROM alpine:latest -# Copy config.yaml from the application directory -COPY config.yaml /app/ +# Install necessary runtime dependencies +RUN apk add --no-cache ca-certificates -# Expose the port your application listens on +# Set the working directory inside the container +WORKDIR /app + +# Copy the binary from the builder stage +COPY --from=builder /app/boxes-api . + +# Copy any other necessary files (e.g., configuration files) +# COPY --from=builder /app/config.yaml . + +# Expose the port your app listens on EXPOSE 8080 -# Command to run your application -CMD ["/app/main"] \ No newline at end of file +# Command to run the executable +CMD ["./boxes-api"] \ No newline at end of file diff --git a/boxes-api b/boxes-api new file mode 100755 index 0000000..a8035d9 Binary files /dev/null and b/boxes-api differ diff --git a/config.yaml b/config/config.yaml similarity index 100% rename from config.yaml rename to config/config.yaml diff --git a/go.mod b/go.mod index 1333077..2566455 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module boxes-api -go 1.21.1 +go 1.21 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/main.go b/main.go index dc39776..2fa0156 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "os" "github.com/gorilla/mux" "github.com/jinzhu/gorm" @@ -16,16 +17,21 @@ var ( ) func main() { + + configFile := os.Getenv("CONFIG") var err error - config, err = LoadConfig("config.yaml") + config, err = LoadConfig(configFile) + + // check for errors + if err != nil || config == nil { + log.Fatalf("Failed to load config: %v", err) + } + fmt.Println(config.DatabasePath) fmt.Println(config.ImageStorageDir) fmt.Println(config.JWTSecret) fmt.Println(config.LogFile) fmt.Println(config.ListeningPort) - if err != nil || config == nil { - log.Fatalf("Failed to load config: %v", err) - } // Connect to the database db, err = ConnectDB(config.DatabasePath)