axyut commited on
Commit ·
65d037a
1
Parent(s): e3988b5
docker setup
Browse files- Dockerfile +50 -0
- main.go +12 -2
Dockerfile
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Builder
|
| 2 |
+
# Use a specific, stable Go version with Alpine for a smaller build environment
|
| 3 |
+
FROM golang:1.24.2-alpine AS builder
|
| 4 |
+
|
| 5 |
+
# Set the working directory inside the builder container
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
# Enable Go modules and disable CGO for a statically linked binary.
|
| 9 |
+
# CGO_ENABLED=0 is important for scratch or Alpine base images.
|
| 10 |
+
ENV GO111MODULE=on \
|
| 11 |
+
CGO_ENABLED=0
|
| 12 |
+
|
| 13 |
+
# Copy go.mod and go.sum first to leverage Docker's build cache.
|
| 14 |
+
# This ensures dependencies are downloaded only if these files change.
|
| 15 |
+
COPY go.mod go.sum ./
|
| 16 |
+
|
| 17 |
+
# Download Go module dependencies
|
| 18 |
+
RUN go mod download
|
| 19 |
+
|
| 20 |
+
# Copy the rest of your application source code
|
| 21 |
+
COPY . .
|
| 22 |
+
|
| 23 |
+
# Build the Go application.
|
| 24 |
+
# -o /app/niyam: Specifies the output path and name of the executable.
|
| 25 |
+
# -ldflags "-s -w": Strips debug symbols and DWARF tables, significantly reducing binary size.
|
| 26 |
+
# ./main.go: Assumes your main package is in main.go at the root. Adjust if it's elsewhere (e.g., ./cmd/niyam/main.go).
|
| 27 |
+
RUN go build -o /app/niyam -ldflags "-s -w" ./main.go
|
| 28 |
+
|
| 29 |
+
# ---
|
| 30 |
+
|
| 31 |
+
# Stage 2: Runner
|
| 32 |
+
# Use a minimal base image for the final production image.
|
| 33 |
+
# 'scratch' is the smallest possible image, containing only your executable.
|
| 34 |
+
FROM scratch
|
| 35 |
+
|
| 36 |
+
# Set the working directory in the final image
|
| 37 |
+
WORKDIR /app
|
| 38 |
+
|
| 39 |
+
# Copy only the compiled binary from the 'builder' stage
|
| 40 |
+
COPY --from=builder /app/niyam .
|
| 41 |
+
|
| 42 |
+
# If your Go application serves HTTP requests, you should expose the port.
|
| 43 |
+
# Hugging Face Spaces often expose port 7860 by default for Gradio/Streamlit,
|
| 44 |
+
# but for a custom Go app, you might use 8080 or another common port.
|
| 45 |
+
# Make sure your Go app listens on this port.
|
| 46 |
+
EXPOSE 7860
|
| 47 |
+
|
| 48 |
+
# Define the command to run your application.
|
| 49 |
+
# This should be your compiled executable.
|
| 50 |
+
CMD ["./niyam"]
|
main.go
CHANGED
|
@@ -33,6 +33,16 @@ func main() {
|
|
| 33 |
return resp, nil
|
| 34 |
})
|
| 35 |
|
| 36 |
-
//
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
|
|
|
| 33 |
return resp, nil
|
| 34 |
})
|
| 35 |
|
| 36 |
+
port := "7860" // Default port
|
| 37 |
+
// You might read the port from an environment variable for flexibility
|
| 38 |
+
// if p := os.Getenv("PORT"); p != "" {
|
| 39 |
+
// port = p
|
| 40 |
+
// }
|
| 41 |
+
|
| 42 |
+
addr := fmt.Sprintf(":%s", port) // Listen on all interfaces
|
| 43 |
+
fmt.Printf("Server starting on http://localhost%s...\n", addr)
|
| 44 |
+
err := http.ListenAndServe(addr, router) // <--- THIS IS KEY
|
| 45 |
+
if err != nil {
|
| 46 |
+
fmt.Printf("Server failed to start: %v\n", err)
|
| 47 |
+
}
|
| 48 |
}
|