blob: 2564ef346397ffd078f531abbb7a2860281be437 [file] [log] [blame]
Katri Turunen412df962019-09-16 08:48:18 +03001/*
2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package main
19
20import (
21 "fmt"
22 "io/ioutil"
23 "net"
24 "net/http"
25)
26
Roni Riskafc77ebb2019-09-26 08:20:44 +030027// SupervisionURL is the url where kubernetes posts alive queries
28const SupervisionURL = "/supervision/"
Katri Turunen412df962019-09-16 08:48:18 +030029
Roni Riskafc77ebb2019-09-26 08:20:44 +030030// HTTPServer is the VesMgr HTTP server struct
31type HTTPServer struct {
32 listener net.Listener
Katri Turunen412df962019-09-16 08:48:18 +030033}
34
Roni Riskafc77ebb2019-09-26 08:20:44 +030035func (s *HTTPServer) init(address string) *HTTPServer {
36 var err error
37 s.listener, err = net.Listen("tcp", address)
38 if err != nil {
39 panic("Cannot listen:" + err.Error())
40 }
41 return s
42}
43
44func (s *HTTPServer) start(notifPath string, notifCh chan []byte, supCh chan chan string) {
45 go runHTTPServer(s.listener, notifPath, notifCh, supCh)
46}
47
48func (s *HTTPServer) addr() net.Addr {
49 return s.listener.Addr()
50}
51
52func runHTTPServer(listener net.Listener, xappNotifURL string, notifCh chan []byte, supervisionCh chan chan string) {
Katri Turunen412df962019-09-16 08:48:18 +030053
54 logger.Info("vesmgr http server serving at %s", listener.Addr())
55
Roni Riskafc77ebb2019-09-26 08:20:44 +030056 http.HandleFunc(xappNotifURL, func(w http.ResponseWriter, r *http.Request) {
Katri Turunen412df962019-09-16 08:48:18 +030057
58 switch r.Method {
59 case "POST":
Roni Riskafc77ebb2019-09-26 08:20:44 +030060 logger.Info("httpServer: POST in %s", xappNotifURL)
Katri Turunen412df962019-09-16 08:48:18 +030061 body, err := ioutil.ReadAll(r.Body)
62 defer r.Body.Close()
63 if err != nil {
64 logger.Error("httpServer: Invalid body in POST request")
65 return
66 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030067 notifCh <- body
Katri Turunen412df962019-09-16 08:48:18 +030068 return
69 default:
70 logger.Error("httpServer: Invalid method %s to %s", r.Method, r.URL.Path)
71 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
72 return
73 }
74 })
75
Roni Riskafc77ebb2019-09-26 08:20:44 +030076 http.HandleFunc(SupervisionURL, func(w http.ResponseWriter, r *http.Request) {
Katri Turunen412df962019-09-16 08:48:18 +030077
78 switch r.Method {
79 case "GET":
80 logger.Info("httpServer: GET supervision")
Roni Riskafc77ebb2019-09-26 08:20:44 +030081 supervisionAckCh := make(chan string)
Katri Turunen412df962019-09-16 08:48:18 +030082 // send supervision to the main loop
Roni Riskafc77ebb2019-09-26 08:20:44 +030083 supervisionCh <- supervisionAckCh
84 reply := <-supervisionAckCh
Katri Turunen412df962019-09-16 08:48:18 +030085 logger.Info("httpServer: supervision ack from the main loop: %s", reply)
86 fmt.Fprintf(w, reply)
87 return
88 default:
89 logger.Error("httpServer: invalid method %s to %s", r.Method, r.URL.Path)
90 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
91 return
92 }
93
94 })
95
96 http.Serve(listener, nil)
97}