-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from PDOK/fix-tiles-healthcheck
fix: re-enable tiles healthcheck
- Loading branch information
Showing
3 changed files
with
42 additions
and
10 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
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
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 |
---|---|---|
@@ -1,11 +1,44 @@ | ||
package engine | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
func newHealthEndpoint(e *Engine) { | ||
e.Router.Get("/health", func(w http.ResponseWriter, _ *http.Request) { | ||
SafeWrite(w.Write, []byte("OK")) | ||
}) | ||
var target *url.URL | ||
if tilesConfig := e.Config.OgcAPI.Tiles; tilesConfig != nil { | ||
var err error | ||
switch { | ||
case tilesConfig.DatasetTiles != nil: | ||
target, err = url.Parse(tilesConfig.DatasetTiles.TileServer.String() + *tilesConfig.DatasetTiles.HealthCheck.TilePath) | ||
case len(tilesConfig.Collections) > 0 && tilesConfig.Collections[0].Tiles != nil: | ||
target, err = url.Parse(tilesConfig.Collections[0].Tiles.GeoDataTiles.TileServer.String() + *tilesConfig.Collections[0].Tiles.GeoDataTiles.HealthCheck.TilePath) | ||
default: | ||
log.Println("cannot determine health check tilepath, falling back to basic check") | ||
} | ||
if err != nil { | ||
log.Fatalf("invalid health check tilepath: %v", err) | ||
} | ||
} | ||
if target != nil { | ||
client := &http.Client{Timeout: time.Duration(500) * time.Millisecond} | ||
e.Router.Get("/health", func(w http.ResponseWriter, _ *http.Request) { | ||
resp, err := client.Head(target.String()) | ||
if err != nil { | ||
// exact error is irrelevant for health monitoring, but log it for insight | ||
log.Printf("healthcheck failed: %v", err) | ||
w.WriteHeader(http.StatusNotFound) | ||
} else { | ||
w.WriteHeader(resp.StatusCode) | ||
resp.Body.Close() | ||
} | ||
}) | ||
} else { | ||
e.Router.Get("/health", func(w http.ResponseWriter, _ *http.Request) { | ||
SafeWrite(w.Write, []byte("OK")) | ||
}) | ||
} | ||
} |