Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
github.com/gin-contrib/pprof v1.5.3 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBv
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY=
github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk=
github.com/gin-contrib/pprof v1.5.3 h1:Bj5SxJ3kQDVez/s/+f9+meedJIqLS+xlkIVDe/lcvgM=
github.com/gin-contrib/pprof v1.5.3/go.mod h1:0+LQSZ4SLO0B6+2n6JBzaEygpTBxe/nI+YEYpfQQ6xY=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Depado/ginprom"
"github.com/aurowora/compress"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
healthcheck "github.com/tavsec/gin-healthcheck"
Expand All @@ -22,24 +23,26 @@ func main() {
var err error
var zipFile string
var port int
var debug bool

rootCmd := &cobra.Command{
Use: "http",
Short: "Postcode Polygons API server",
Run: func(cmd *cobra.Command, args []string) {
server(zipFile, port)
server(zipFile, port, debug)
},
}

rootCmd.Flags().StringVar(&zipFile, "codepoint", "./data/codepo_gb.zip", "Path to CodePoint Open zip file")
rootCmd.Flags().IntVar(&port, "port", 8080, "Port to run HTTP server on")
rootCmd.Flags().BoolVar(&debug, "debug", false, "Enable debugging (pprof) - WARING: do not enable in production")

if err = rootCmd.Execute(); err != nil {
log.Fatalf("failed to execute root command: %v", err)
}
}

func server(zipFile string, port int) {
func server(zipFile string, port int, debug bool) {
if _, err := os.Stat(zipFile); os.IsNotExist(err) {
log.Fatalf("CodePoint zip file does not exist: %s", zipFile)
}
Expand Down Expand Up @@ -70,6 +73,11 @@ func server(zipFile string, port int) {
cors.Default(),
)

if debug {
log.Println("WARNING: pprof endpoints are enabled and exposed. Do not run with this flag in production.")
pprof.Register(r)
Comment thread
rm-hull marked this conversation as resolved.
}

err = healthcheck.New(r, hc_config.DefaultConfig(), []checks.Check{})
if err != nil {
log.Fatalf("failed to initialize healthcheck: %v", err)
Expand Down