Connecting a Frontend to a Backend Tutorial.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
FROM alpine:3.1
|
||||
MAINTAINER Carter Morgan <askcarter@google.com>
|
||||
COPY hello /usr/bin/
|
||||
CMD ["/usr/bin/hello"]
|
||||
@@ -0,0 +1,7 @@
|
||||
Build hello go binary first
|
||||
|
||||
go build -tags netgo -ldflags "-extldflags '-lm -lstdc++ -static'" .
|
||||
|
||||
Then build docker image
|
||||
|
||||
docker build -t hello .
|
||||
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/braintree/manners"
|
||||
"github.com/GoogleCloudPlatform/kubernetes-workshops/bundles/kubernetes-101/workshop/app/handlers"
|
||||
"github.com/GoogleCloudPlatform/kubernetes-workshops/bundles/kubernetes-101/workshop/app/health"
|
||||
)
|
||||
|
||||
const version = "1.0.0"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
httpAddr = flag.String("http", "0.0.0.0:80", "HTTP service address.")
|
||||
healthAddr = flag.String("health", "0.0.0.0:81", "Health service address.")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
log.Println("Starting server...")
|
||||
log.Printf("Health service listening on %s", *healthAddr)
|
||||
log.Printf("HTTP service listening on %s", *httpAddr)
|
||||
|
||||
errChan := make(chan error, 10)
|
||||
|
||||
hmux := http.NewServeMux()
|
||||
hmux.HandleFunc("/healthz", health.HealthzHandler)
|
||||
hmux.HandleFunc("/readiness", health.ReadinessHandler)
|
||||
hmux.HandleFunc("/healthz/status", health.HealthzStatusHandler)
|
||||
hmux.HandleFunc("/readiness/status", health.ReadinessStatusHandler)
|
||||
healthServer := manners.NewServer()
|
||||
healthServer.Addr = *healthAddr
|
||||
healthServer.Handler = handlers.LoggingHandler(hmux)
|
||||
|
||||
go func() {
|
||||
errChan <- healthServer.ListenAndServe()
|
||||
}()
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", handlers.HelloHandler)
|
||||
mux.Handle("/secure", handlers.JWTAuthHandler(handlers.HelloHandler))
|
||||
mux.Handle("/version", handlers.VersionHandler(version))
|
||||
|
||||
httpServer := manners.NewServer()
|
||||
httpServer.Addr = *httpAddr
|
||||
httpServer.Handler = handlers.LoggingHandler(mux)
|
||||
|
||||
go func() {
|
||||
errChan <- httpServer.ListenAndServe()
|
||||
}()
|
||||
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
for {
|
||||
select {
|
||||
case err := <-errChan:
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case s := <-signalChan:
|
||||
log.Println(fmt.Sprintf("Captured %v. Exiting...", s))
|
||||
health.SetReadinessStatus(http.StatusServiceUnavailable)
|
||||
httpServer.BlockingClose()
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user