2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 // SupervisionURL is the url where kubernetes posts alive queries
28 const SupervisionURL = "/supervision/"
30 // HTTPServer is the VesMgr HTTP server struct
31 type HTTPServer struct {
35 func (s *HTTPServer) init(address string) *HTTPServer {
37 s.listener, err = net.Listen("tcp", address)
39 panic("Cannot listen:" + err.Error())
44 func (s *HTTPServer) start(notifPath string, notifCh chan []byte, supCh chan chan string) {
45 go runHTTPServer(s.listener, notifPath, notifCh, supCh)
48 func (s *HTTPServer) addr() net.Addr {
49 return s.listener.Addr()
52 func runHTTPServer(listener net.Listener, xappNotifURL string, notifCh chan []byte, supervisionCh chan chan string) {
54 logger.Info("vesmgr http server serving at %s", listener.Addr())
56 http.HandleFunc(xappNotifURL, func(w http.ResponseWriter, r *http.Request) {
60 logger.Info("httpServer: POST in %s", xappNotifURL)
61 body, err := ioutil.ReadAll(r.Body)
64 logger.Error("httpServer: Invalid body in POST request")
70 logger.Error("httpServer: Invalid method %s to %s", r.Method, r.URL.Path)
71 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
76 http.HandleFunc(SupervisionURL, func(w http.ResponseWriter, r *http.Request) {
80 logger.Info("httpServer: GET supervision")
81 supervisionAckCh := make(chan string)
82 // send supervision to the main loop
83 supervisionCh <- supervisionAckCh
84 reply := <-supervisionAckCh
85 logger.Info("httpServer: supervision ack from the main loop: %s", reply)
89 logger.Error("httpServer: invalid method %s to %s", r.Method, r.URL.Path)
90 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
96 http.Serve(listener, nil)