Skip to content
Permalink
Browse files

feat: add healthcheck

  • Loading branch information...
giuem committed Feb 9, 2019
1 parent 615f87d commit 36e8cd8a95c1eec076c6119ce1558555ca9bae10
Showing with 43 additions and 12 deletions.
  1. +6 −4 Dockerfile
  2. +33 −0 main.go
  3. +4 −8 server/server.go
@@ -15,13 +15,15 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w -s" -o ga-proxy
FROM alpine:3.8
LABEL maintainer "giuem <i@giuem.com>"

EXPOSE 80
ENV IP=0.0.0.0
ENV PORT=80

COPY --from=BUILD /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=BUILD /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=BUILD /src/ga_proxy /ga_proxy

EXPOSE 80

# HEALTHCHECK --interval=1m --timeout=10s --start-period=1s --retries=2 \
# CMD curl -X HEAD -If http://localhost/detect || exit 1
HEALTHCHECK --interval=1m --timeout=10s --start-period=1s --retries=2 \
CMD /ga-proxy ping

CMD ["/ga-proxy", "-i", "0.0.0.0", "-p", "80" ]
33 main.go
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/giuem/ga-proxy/server"
@@ -31,5 +33,36 @@ func main() {
return nil
}

app.Commands = []cli.Command{
cli.Command{
Name: "ping",
Flags: []cli.Flag{
cli.StringFlag{
Name: "ip, i",
Value: "127.0.0.1",
Usage: "server `IP`",
EnvVar: "IP",
},
cli.StringFlag{
Name: "port, p",
Value: "9080",
Usage: "server `port`",
EnvVar: "PORT",
},
},
Action: func(c *cli.Context) error {
resp, err := http.Get(fmt.Sprintf("http://%v:%v/ping", c.String("ip"), c.String("port")))
if err != nil {
return cli.NewExitError(err, 1)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return cli.NewExitError(fmt.Errorf("server returns non-200 status code"), 1)
}
return nil
},
},
}

app.Run(os.Args)
}
@@ -6,22 +6,18 @@ import (
"github.com/gin-gonic/gin"
)

var r *gin.Engine
// Run starts a HTTP server
func Run(ip, port string) {
addr := fmt.Sprintf("%v:%v", ip, port)

func init() {
r = gin.New()
r := gin.New()
r.Use(gin.Logger())

r.NoRoute(handleRedirect)

r.GET("/", handlePageView)
r.GET("/ping", handlePing)
r.HEAD("/ping", handlePing)
}

// Run starts a HTTP server
func Run(ip, port string) {
addr := fmt.Sprintf("%v:%v", ip, port)

r.Run(addr)
}

0 comments on commit 36e8cd8

Please sign in to comment.
You can’t perform that action at this time.