blob: 1eab7b66bb58c4c3bd01968568ff641352a1cc26 [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package xapp
21
22import (
23 "encoding/json"
24 "github.com/gorilla/mux"
25 "net/http"
26)
27
28const (
29 ReadyURL = "/ric/v1/health/ready"
30 AliveURL = "/ric/v1/health/alive"
31)
32
33type StatusCb func() bool
34
35type Router struct {
36 router *mux.Router
37 cbMap []StatusCb
38}
39
40func NewRouter() *Router {
41 r := &Router{
42 router: mux.NewRouter().StrictSlash(true),
43 cbMap: make([]StatusCb, 0),
44 }
45
46 // Inject default routes for health probes
47 r.InjectRoute(ReadyURL, readyHandler, "GET")
48 r.InjectRoute(AliveURL, aliveHandler, "GET")
49
50 return r
51}
52
53func (r *Router) serviceChecker(inner http.HandlerFunc) http.HandlerFunc {
54 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
55 Logger.Info("restapi: method=%s url=%s", req.Method, req.URL.RequestURI())
Mohamed Abukar9cfde092020-04-28 17:11:33 +030056 if req.URL.RequestURI() == AliveURL || r.CheckStatus() {
Mohamed Abukar2e78e422019-06-02 11:45:52 +030057 inner.ServeHTTP(w, req)
58 } else {
59 respondWithJSON(w, http.StatusServiceUnavailable, nil)
60 }
61 })
62}
63
64func (r *Router) InjectRoute(url string, handler http.HandlerFunc, method string) *mux.Route {
65 return r.router.HandleFunc(url, r.serviceChecker(handler)).Methods(method)
66}
67
68func (r *Router) InjectQueryRoute(url string, h http.HandlerFunc, m string, q ...string) *mux.Route {
69 return r.router.HandleFunc(url, r.serviceChecker(h)).Methods(m).Queries(q...)
70}
71
72func (r *Router) InjectStatusCb(f StatusCb) {
73 r.cbMap = append(r.cbMap, f)
74}
75
76func (r *Router) CheckStatus() (status bool) {
77 if len(r.cbMap) == 0 {
78 return true
79 }
80
81 for _, f := range r.cbMap {
82 status = f()
83 }
84 return
85}
86
87func readyHandler(w http.ResponseWriter, r *http.Request) {
88 respondWithJSON(w, http.StatusOK, nil)
89}
90
91func aliveHandler(w http.ResponseWriter, r *http.Request) {
92 respondWithJSON(w, http.StatusOK, nil)
93}
94
95func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
96 w.Header().Set("Content-Type", "application/json")
97 w.WriteHeader(code)
98 if payload != nil {
99 response, _ := json.Marshal(payload)
100 w.Write(response)
101 }
102}