Quick fixes
This commit is contained in:
15
Dockerfile
15
Dockerfile
@@ -1,12 +1,17 @@
|
|||||||
FROM golang:1.24 AS build
|
FROM golang:1.23-alpine AS build
|
||||||
WORKDIR /go/src/github.com/codemowers/hello-gin/
|
WORKDIR /app
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
COPY cmd ./
|
COPY cmd ./cmd/
|
||||||
RUN go build -tags netgo -ldflags "-linkmode 'external' -extldflags '-static'" -o /go/server .
|
COPY templates ./templates/
|
||||||
|
COPY static ./static/
|
||||||
|
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /app/server ./cmd
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
WORKDIR /
|
WORKDIR /
|
||||||
COPY --from=build /go/server /server
|
COPY --from=build /app/server /server
|
||||||
|
COPY --from=build /app/templates /templates
|
||||||
|
COPY --from=build /app/static /static
|
||||||
ENV GIN_MODE=release
|
ENV GIN_MODE=release
|
||||||
|
EXPOSE 8000 8080
|
||||||
ENTRYPOINT ["/server"]
|
ENTRYPOINT ["/server"]
|
||||||
|
|||||||
23
cmd/main.go
23
cmd/main.go
@@ -9,14 +9,33 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
|
// Load HTML templates
|
||||||
|
router.LoadHTMLGlob("templates/*")
|
||||||
|
|
||||||
|
// Serve static files (for CSS, JS, etc.)
|
||||||
|
router.Static("/static", "./static")
|
||||||
|
|
||||||
metricRouter := gin.Default()
|
metricRouter := gin.Default()
|
||||||
metrics := ginmetrics.GetMonitor()
|
metrics := ginmetrics.GetMonitor()
|
||||||
metrics.SetMetricPath("/metrics")
|
metrics.SetMetricPath("/metrics")
|
||||||
metrics.Use(router)
|
metrics.UseWithoutExposingEndpoint(router)
|
||||||
metrics.Expose(metricRouter)
|
metrics.Expose(metricRouter)
|
||||||
|
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Hello " + os.Getenv("USERNAME") + " from " + os.Getenv("MY_POD_NAME"))
|
// Get environment variables
|
||||||
|
username := "suvakelbas"
|
||||||
|
|
||||||
|
|
||||||
|
podName := os.Getenv("MY_POD_NAME")
|
||||||
|
if podName == "" {
|
||||||
|
podName = "Unknown Pod"
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||||
|
"username": username,
|
||||||
|
"podName": podName,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user