-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
64 lines (52 loc) · 1.35 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"net/http"
"os"
"github.com/facebookgo/grace/gracehttp"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
func getEnvDefault(key, fallback string) string {
// handles returning env vars or the specified default
// if it doest not exist
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
var host string = getEnvDefault("K_SERVICE", "localhost")
func main() {
e := echo.New()
// middleware
e.Use(middleware.Logger())
e.Pre(middleware.AddTrailingSlash())
e.Use(middleware.Secure())
e.Use(middleware.CORS())
e.Use(middleware.BodyLimit("1M"))
e.Use(middleware.Recover())
// setup logging
e.Logger.SetLevel(log.INFO)
// enable HTTPS redirect middleware if not running locally
if host != "localhost" {
e.Logger.Info("Enabled HTTPS redirect middleware")
e.Pre(middleware.HTTPSRedirect())
}
// routes
e.GET("/health/", health)
e.GET("/", helloWorld)
// run server with graceful termination
e.Server.Addr = ":8080"
e.Logger.Fatal(gracehttp.Serve(e.Server))
}
func health(c echo.Context) error {
return c.JSONPretty(http.StatusOK, map[string]interface{}{
"status": "OK",
}, " ")
}
func helloWorld(c echo.Context) error {
return c.JSONPretty(http.StatusOK, map[string]interface{}{
"message": "Hello world!",
"region": host,
}, " ")
}