Skip to content

Instantly share code, notes, and snippets.

@temirov
Last active October 9, 2018 19:47
Show Gist options
  • Save temirov/bcacf87aa0fb2cba1b42687d8aea779d to your computer and use it in GitHub Desktop.
Save temirov/bcacf87aa0fb2cba1b42687d8aea779d to your computer and use it in GitHub Desktop.
ServiceApi
package serviceapi
import (
"fmt"
"net/http"
)
const (
// RootPath is the root path of the server
RootPath = "/"
// HealthCheckPath is the health check path for k8s
HealthCheckPath = "/_ah/health"
// PingPath is the ping path for k8s
PingPath = "/services/ping"
// OkResponse is the answer k8s health check expects
OkResponse = "ok"
// PingResponse is the answer k8s ping expects
PingResponse = "PONG"
)
// HealthCheck handles health checks responding with OkResponse
func HealthCheck(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, OkResponse)
}
// Ping handles pings responding with PingResponse
func Ping(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, PingResponse)
}
// Root redirects to health check path
func Root(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != RootPath {
http.NotFound(w, r)
return
}
http.Redirect(w, r, HealthCheckPath, http.StatusFound)
}
//Handlers registers functions to handle known service pathes
func Handlers() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc(HealthCheckPath, HealthCheck)
mux.HandleFunc(PingPath, Ping)
mux.HandleFunc(RootPath, Root)
return mux
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment