Skip to content

Commit

Permalink
healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
kiberdruzhinnik committed Mar 3, 2024
1 parent 80dda74 commit 6fafa67
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
go-exchange-api
redis
redis
!cmd/*
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ WORKDIR /src/app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN go build -ldflags "-s -w" -o go-exchange-api
RUN go build -ldflags "-s -w" -o go-exchange-api cmd/go-exchange-api/main.go
RUN go build -ldflags "-s -w" -o healthcheck cmd/healthcheck/main.go

FROM alpine
ENV GIN_MODE release
WORKDIR /app
COPY --from=builder /src/app/go-exchange-api ./go-exchange-api
COPY --from=builder /src/app/healthcheck ./healthcheck
RUN addgroup -S appgroup && \
adduser -S appuser -G appgroup
USER appuser
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD ["./healthcheck"]
ENTRYPOINT ["./go-exchange-api"]
3 changes: 2 additions & 1 deletion api/moex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"time"

"github.com/kiberdruzhinnik/go-exchange-api/constants"
custom_errors "github.com/kiberdruzhinnik/go-exchange-api/errors"
"github.com/kiberdruzhinnik/go-exchange-api/utils"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ type MoexPriceJSON struct {

func NewMoexAPI(redis utils.RedisClient) MoexAPI {
return MoexAPI{
BaseURL: "https://iss.moex.com",
BaseURL: constants.MoexBaseApiURL,
Redis: redis,
}
}
Expand Down
3 changes: 2 additions & 1 deletion api/spbex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"time"

"github.com/kiberdruzhinnik/go-exchange-api/constants"
custom_errors "github.com/kiberdruzhinnik/go-exchange-api/errors"
"github.com/kiberdruzhinnik/go-exchange-api/utils"
)
Expand All @@ -30,7 +31,7 @@ type SpbexSecurityJSON struct {

func NewSpbexAPI() SpbexAPI {
return SpbexAPI{
BaseURL: "https://investcab.ru/api",
BaseURL: constants.SpbexBaseApiURL,
}
}

Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions cmd/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"io"
"log"
"net/http"
)

func main() {
resp, err := http.Get("http://127.0.0.1:8080/healthcheck")

if err != nil {
log.Fatalln(err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)

if err != nil {
log.Fatalln(err)
}

status := string(body)
if status != "ok" {
log.Fatalln("status is not ok")
}
}
4 changes: 4 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package constants

const MoexBaseApiURL = "https://iss.moex.com"
const SpbexBaseApiURL = "https://investcab.ru/api"
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ services:
image: redis:7-alpine
volumes:
- redis:/data
healthcheck:
test: [ "CMD-SHELL", "redis-cli ping | grep PONG" ]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s

exchange-api:
image: kotasha/go-exchange-api:latest
Expand All @@ -14,6 +20,12 @@ services:
- 8080:8080/tcp
depends_on:
- redis-cache
healthcheck:
test: [ "./healthcheck" ]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s

volumes:
redis:
2 changes: 2 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ var ErrorNoData = errors.New("no data")

var ErrorRedisNotConnected = errors.New("redis is not connected")
var ErrorRedisNotFound = errors.New("not found in redis")

var ErrorNotAllowed = errors.New("not allowed")
22 changes: 22 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@ import (
"net/http"
"strings"
"unicode"

"github.com/kiberdruzhinnik/go-exchange-api/constants"
"github.com/kiberdruzhinnik/go-exchange-api/errors"
)

var URLS_ALLOW_LIST []string = []string{
constants.MoexBaseApiURL,
constants.SpbexBaseApiURL,
}

func CheckSafeURL(url string) bool {
for _, u := range URLS_ALLOW_LIST {
if strings.HasPrefix(url, u) {
return true
}
}
return false
}

func HttpGet(url string) ([]byte, error) {

if !CheckSafeURL(url) {
return nil, errors.ErrorNotAllowed
}

resp, err := http.Get(url)

if err != nil {
Expand Down

0 comments on commit 6fafa67

Please sign in to comment.