-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: base web server with healthz endpoint and tests
- Loading branch information
1 parent
9a67ee6
commit fc02a17
Showing
10 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,10 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
|
||
# env file | ||
.env | ||
|
||
# binary | ||
url-short |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
FROM alpine:3.20.0 AS base | ||
|
||
RUN apk update | ||
RUN apk upgrade | ||
RUN apk add --update go=1.22.3-r0 | ||
|
||
FROM base AS tester | ||
|
||
WORKDIR /opt/url-short | ||
|
||
ADD . /opt/url-short | ||
|
||
CMD ["go", "test"] | ||
|
||
FROM base AS builder | ||
|
||
WORKDIR /build | ||
|
||
ADD . /build | ||
|
||
RUN go build -o main . | ||
|
||
FROM builder AS production | ||
|
||
WORKDIR /opt/url-short/ | ||
|
||
COPY --from=builder /build/main . | ||
|
||
CMD ["./main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
fmt: | ||
go fmt ./... | ||
.PHONY:fmt | ||
|
||
lint: fmt | ||
golangci-lint run -v | ||
.PHONY:lint | ||
|
||
vet: lint | ||
go vet ./... | ||
.PHONY:vet | ||
|
||
build: | ||
docker build . -t "url-short:latest" | ||
.PHONY:build | ||
|
||
run: | ||
docker compose up -d | ||
.PHONY:run | ||
|
||
stop: | ||
docker compose down | ||
.PHONY:stop | ||
|
||
test: | ||
docker build . -t "url-short:test" --target tester | ||
docker run -t "url-short:test" | ||
.PHONY:test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: '3.1' | ||
|
||
services: | ||
url-short: | ||
image: url-short:latest | ||
env_file: | ||
- .env | ||
ports: | ||
- 5001:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module url-short | ||
|
||
go 1.22.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
type apiConfig struct{} | ||
|
||
func (apiCfg *apiConfig) healthz(w http.ResponseWriter, r *http.Request) { | ||
payload := struct { | ||
Status string `json:"status"` | ||
}{ | ||
Status: "ok", | ||
} | ||
|
||
respondWithJSON(w, http.StatusOK, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHealthEndpoint(t *testing.T){ | ||
t.Run("test healthz endpoint", func(t *testing.T){ | ||
request, _ := http.NewRequest(http.MethodGet, "/api/v1/healthz", nil) | ||
response := httptest.NewRecorder() | ||
|
||
apiCfg := apiConfig{} | ||
|
||
apiCfg.healthz(response, request) | ||
|
||
got := response.Body.String() | ||
want := `{"status":"ok"}` | ||
|
||
if got != want { | ||
t.Errorf("got %q wanted %q", got, want) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func respondWithJSON(w http.ResponseWriter, status int, payload interface{}) { | ||
data, err := json.Marshal(payload) | ||
|
||
if err != nil { | ||
log.Printf("can not Marshal payload %v", payload) | ||
return | ||
} | ||
|
||
w.Header().Set("content-type", "application/json") | ||
w.WriteHeader(status) | ||
w.Write(data) | ||
} | ||
|
||
func respondWithError(w http.ResponseWriter, code int, msg string) { | ||
errorResponse := struct { | ||
Error string `json:"error"` | ||
}{ | ||
Error: msg, | ||
} | ||
|
||
respondWithJSON(w, code, errorResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func main() { | ||
serverPort := os.Getenv("SERVER_PORT") | ||
|
||
mux := http.NewServeMux() | ||
|
||
server := &http.Server{ | ||
Addr: ":" + serverPort, | ||
Handler: mux, | ||
} | ||
|
||
apiCfg := apiConfig{} | ||
|
||
mux.HandleFunc("GET /api/v1/healthz", apiCfg.healthz) | ||
|
||
log.Printf("Serving port : %v \n", serverPort) | ||
log.Fatal(server.ListenAndServe()) | ||
} |