adding lifecycle hook for test exit
This commit is contained in:
@@ -28,7 +28,9 @@ func addMiddleware(mux *chi.Mux) {
|
|||||||
|
|
||||||
func addRoutes(mux *chi.Mux) {
|
func addRoutes(mux *chi.Mux) {
|
||||||
// Add a simple resource
|
// Add a simple resource
|
||||||
mux.Mount("/api/id", AppResource{}.Routes())
|
mux.Mount("/api/id", AppResource{}.Routes())
|
||||||
|
// lifecycle management hooks
|
||||||
|
mux.Mount("/.service", AppLifecycleResource{}.Routes())
|
||||||
// Live a healthy life!
|
// Live a healthy life!
|
||||||
mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("yolo"))
|
w.Write([]byte("yolo"))
|
||||||
|
|||||||
26
api/lifecycle.go
Normal file
26
api/lifecycle.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AppLifecycleResource struct{}
|
||||||
|
|
||||||
|
// Routes creates a REST router for the todos resource
|
||||||
|
func (self AppLifecycleResource) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Route("/lifecycle", func(r chi.Router) {
|
||||||
|
r.Post("/stop", self.stop)
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self AppLifecycleResource) stop(w http.ResponseWriter, r *http.Request) {
|
||||||
|
render.Status(r, http.StatusOK)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
@@ -1,13 +1,16 @@
|
|||||||
sleep 5
|
# Check if it is alive
|
||||||
|
|
||||||
ping -c 5 target
|
ping -c 5 target
|
||||||
|
|
||||||
curl -X POST target:8080/api/id/1
|
EXIT_CODE=0
|
||||||
|
# RUN THE TEST
|
||||||
if curl -X POST target:8080/api/id/1 | grep -q '{"op":"POST","id":1}'; then
|
if curl -X POST target:8080/api/id/1 | grep -q '{"op":"POST","id":1}'; then
|
||||||
echo "Tests passed!"
|
echo "Tests passed!"
|
||||||
exit 0
|
|
||||||
else
|
else
|
||||||
echo "Tests failed!"
|
echo "Tests failed!"
|
||||||
exit 1
|
let EXIT_CODE=1
|
||||||
fi
|
fi
|
||||||
|
# GOOD API DESIGN OF A SERVICE INCLUDES A LIFECYCLE MGMT INTERFACE
|
||||||
|
# WHICH WE CAN LEVERAGE FOR TESTING... INVOKE IT TO STOP THE SERVICE
|
||||||
|
curl -X POST target:8080/.service/lifecycle/stop
|
||||||
|
sleep 1
|
||||||
|
exit $EXIT_CODE
|
||||||
|
|||||||
Reference in New Issue
Block a user