From 9dde03fc8b14f1c82d398e99ce9e09058880f680 Mon Sep 17 00:00:00 2001 From: Quang nd Date: Thu, 29 Aug 2024 16:03:52 +0700 Subject: [PATCH] a --- .vscode/launch.json | 2 +- Dockerfile | 2 +- Makefile | 4 +--- main.go | 33 ++++++++++++++++++++------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index aab038e..cffe80b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${fileDirname}" + "program": "${workspaceFolder}" } ] } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 314ed00..5ae98b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN go build -o main ./ +RUN go build -ldflags "-X main.Version=1.0.0 -X main.Commit=$(git rev-parse HEAD) -X main.BuildTime=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" -o main ./ FROM alpine:latest WORKDIR /app diff --git a/Makefile b/Makefile index eb83541..b26b1e7 100644 --- a/Makefile +++ b/Makefile @@ -39,11 +39,9 @@ merge: gh pr create --base $$targetBranch; \ " -docker-build: +docker-run: @echo "Building the Docker image..." docker build -t $(DOCKER_REPO):$(VERSION) . - -docker-run: docker run -d -p ${PORT}:${PORT} --name ${APP_NAME} $(DOCKER_REPO):$(VERSION) docker-push: docker-build diff --git a/main.go b/main.go index c3391c0..12c7eed 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "net/http" - "strconv" "strings" "github.com/elastic/go-elasticsearch/v8" @@ -155,20 +154,28 @@ func handleSearch(es *elasticsearch.Client) gin.HandlerFunc { } } +var ( + Version = "dev" + Commit = "none" + BuildTime = "unknown" +) + +func HealthCheckHandler(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "version": Version, + "commit": Commit, + "buildTime": BuildTime, + }) +} + func main() { - var elasticSearchHost = readConsulConfig() - esClient := connectElasticSearch(elasticSearchHost) - fmt.Println(esClient) + // var elasticSearchHost = readConsulConfig() + // esClient := connectElasticSearch(elasticSearchHost) + // fmt.Println(esClient) router := gin.Default() - version := 4 - router.GET("/ping/:name", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "pong " + c.Params.ByName("name"), - "version": "v" + strconv.Itoa(version), - }) - }) + router.GET("/health", HealthCheckHandler) - router.POST("/search", insertSearch(esClient)) - router.GET("/search", handleSearch(esClient)) + // router.POST("/search", insertSearch(esClient)) + // router.GET("/search", handleSearch(esClient)) router.Run(":8080") }