use goscaffold for API standup, glide fixes
diff --git a/api/api.go b/api/api.go
index 9a1c9c0..bc98f61 100644
--- a/api/api.go
+++ b/api/api.go
@@ -6,6 +6,7 @@
"net/http"
"expvar"
"fmt"
+ "github.com/30x/goscaffold"
)
// todo: handle TLS
@@ -14,6 +15,8 @@
const (
configAPIPort = "api_port"
configExpVarPath = "api_expvar_path"
+ configReadyPath = "api_ready"
+ configHealthyPath = "api_healthy"
)
var log apid.LogService
@@ -25,24 +28,55 @@
log = apid.Log().ForModule("api")
config.SetDefault(configAPIPort, 9000)
+ config.SetDefault(configReadyPath, "/ready")
+ config.SetDefault(configHealthyPath, "/healthy")
r := mux.NewRouter()
rw := &router{r}
- return &service{rw}
+ scaffold := goscaffold.CreateHTTPScaffold()
+ return &service{rw, scaffold}
}
type service struct {
*router
+ scaffold *goscaffold.HTTPScaffold
}
func (s *service) Listen() error {
- port := config.GetString(configAPIPort)
- log.Infof("opening api port %s", port)
+ port := config.GetInt(configAPIPort)
+ log.Infof("opening api port %d", port)
s.InitExpVar()
- apid.Events().Emit(apid.SystemEventsSelector, apid.APIListeningEvent) // todo: run after successful listen?
- return http.ListenAndServe(":"+port, s.r)
+ s.scaffold.SetInsecurePort(port)
+
+ // Direct the scaffold to catch common signals and trigger a graceful shutdown.
+ s.scaffold.CatchSignals()
+
+ // Set an URL that may be used by a load balancer to test if the server is ready to handle requests
+ if config.GetString(configReadyPath) != "" {
+ s.scaffold.SetReadyPath(config.GetString(configReadyPath))
+ }
+
+ // Set an URL that may be used by infrastructure to test
+ // if the server is working or if it needs to be restarted or replaced
+ if config.GetString(configHealthyPath) != "" {
+ s.scaffold.SetReadyPath(config.GetString(configHealthyPath))
+ }
+
+ err := s.scaffold.StartListen(s.r)
+ if err != nil {
+ return err
+ }
+
+ apid.Events().Emit(apid.SystemEventsSelector, apid.APIListeningEvent)
+
+ return s.scaffold.WaitForShutdown()
+}
+
+func (s *service) Close() {
+ s.scaffold.Shutdown(nil)
+ s.scaffold = nil
}
func (s *service) InitExpVar() {
diff --git a/cmd/apid/glide.yaml b/cmd/apid/glide.yaml
index 92d42b6..8809a98 100644
--- a/cmd/apid/glide.yaml
+++ b/cmd/apid/glide.yaml
@@ -2,8 +2,6 @@
import:
- package: github.com/30x/apid
version: master
-- package: github.com/spf13/viper
-- package: github.com/apigee-labs/transicator
- package: github.com/30x/apidApigeeSync
version: master
- package: github.com/30x/apidGatewayDeploy
diff --git a/cmd/apid/main.go b/cmd/apid/main.go
index 0d6ba1c..b9995b5 100644
--- a/cmd/apid/main.go
+++ b/cmd/apid/main.go
@@ -34,6 +34,8 @@
log.Debug("listening...")
api := apid.API()
- err := api.Listen() // doesn't return if no error
- log.Fatal("Is APID already running?", err)
+ err := api.Listen()
+ if err != nil {
+ log.Print(err)
+ }
}
diff --git a/glide.yaml b/glide.yaml
index b1e35a2..b5294fd 100644
--- a/glide.yaml
+++ b/glide.yaml
@@ -1,9 +1,15 @@
package: github.com/30x/apid
import:
- package: github.com/Sirupsen/logrus
+ version: master
- package: github.com/spf13/viper
+ version: master
- package: github.com/mattn/go-sqlite3
+ version: master
- package: github.com/gorilla/mux
+ version: master
+- package: github.com/30x/goscaffold
+ version: master
testImport:
- package: github.com/onsi/ginkgo/ginkgo
- package: github.com/onsi/gomega