585319e421e21004e9f4108c40a77e48e2cb5679
[ric-plt/vespamgr.git] / cmd / vesmgr / httpserver.go
1 /*
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
18 package main
19
20 import (
21         "fmt"
22         "io/ioutil"
23         "net"
24         "net/http"
25 )
26
27 const SupervisionUrl = "/supervision/"
28
29 func startHttpServer(listener net.Listener, xappnotifUrl string, notif_ch chan []byte, supervision_ch chan chan string) {
30         go runHttpServer(listener, xappnotifUrl, notif_ch, supervision_ch)
31 }
32
33 func runHttpServer(listener net.Listener, xappNotifUrl string, notif_ch chan []byte, supervision_ch chan chan string) {
34
35         logger.Info("vesmgr http server serving at %s", listener.Addr())
36
37         http.HandleFunc(xappNotifUrl, func(w http.ResponseWriter, r *http.Request) {
38
39                 switch r.Method {
40                 case "POST":
41                         logger.Info("httpServer: POST in %s", xappNotifUrl)
42                         body, err := ioutil.ReadAll(r.Body)
43                         defer r.Body.Close()
44                         if err != nil {
45                                 logger.Error("httpServer: Invalid body in POST request")
46                                 return
47                         }
48                         notif_ch <- body
49                         return
50                 default:
51                         logger.Error("httpServer: Invalid method %s to %s", r.Method, r.URL.Path)
52                         http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
53                         return
54                 }
55         })
56
57         http.HandleFunc(SupervisionUrl, func(w http.ResponseWriter, r *http.Request) {
58
59                 switch r.Method {
60                 case "GET":
61                         logger.Info("httpServer: GET supervision")
62                         supervision_ack_ch := make(chan string)
63                         // send supervision to the main loop
64                         supervision_ch <- supervision_ack_ch
65                         reply := <-supervision_ack_ch
66                         logger.Info("httpServer: supervision ack from the main loop: %s", reply)
67                         fmt.Fprintf(w, reply)
68                         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         })
76
77         http.Serve(listener, nil)
78 }