Skip to content
Snippets Groups Projects
Commit 7e320e65 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

remove server, was wrong direction

parent 3936be61
No related branches found
No related tags found
1 merge request!137Resolve "Implement QKDN manager dummy"
package managerServer
import (
"encoding/json"
"net/http"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
type RequestBody struct {
KMS_ID uuid.UUID `json:"KMS_ID"`
Peers []Peer `json:"peers,omitempty"`
Running bool `json:"running,omitempty"`
}
type Peer struct {
Peer_ID uuid.UUID `json:"peer_ID"`
Running bool `json:"running"`
}
func handleHttpOperationalState(w http.ResponseWriter, r *http.Request) {
logrus.Infof("Handler for OperationalState got request")
var data RequestBody
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
logrus.Infof("ID: %s, running: %t", data.KMS_ID, data.Running)
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("OK\n"))
if err != nil {
logrus.Error(err)
}
}
func handleHttpQkdInterface(w http.ResponseWriter, r *http.Request) {
logrus.Infof("Handler for QkdInterface got request")
var data RequestBody
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
logrus.Infof("ID: %s, running: %t", data.KMS_ID, data.Running)
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("OK\n"))
if err != nil {
logrus.Error(err)
}
}
func handleHttpInterCKMSInterface(w http.ResponseWriter, r *http.Request) {
logrus.Infof("Handler for InterCKMSInterface got request")
var data RequestBody
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
for _, peer := range data.Peers {
logrus.Infof("ID: %s, peerID:%s, running: %t", data.KMS_ID, peer.Peer_ID, peer.Running)
}
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("OK\n"))
if err != nil {
logrus.Error(err)
}
}
func handleHttpAkmsInterface(w http.ResponseWriter, r *http.Request) {
logrus.Infof("Handler for AkmsInterface got request")
var data RequestBody
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
logrus.Infof("ID: %s, running: %t", data.KMS_ID, data.Running)
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("OK\n"))
if err != nil {
logrus.Error(err)
}
}
package managerServer
import (
"errors"
"net/http"
"time"
"code.fbi.h-da.de/danet/quant/qkdnManager-simulator/config"
"github.com/sirupsen/logrus"
)
func SetupHTTPServer(conf config.ManagerServer) *http.Server {
logrus.Info("Setting up server for management information...")
mux := http.NewServeMux()
addHandlersToMux(mux, conf.Endpoints)
srv := http.Server{
Addr: conf.Address,
Handler: mux,
ReadHeaderTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
}
go startServer(&srv)
logrus.Infof("QKDN manager server running on %s ...", conf.Address)
return &srv
}
func startServer(srv *http.Server) {
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logrus.Error(err)
}
logrus.Info("Stopped serving new connections.")
}
func addHandlersToMux(mux *http.ServeMux, endpoints map[string]string) {
// add additional handlers if necessary
mux.HandleFunc(config.APIPrefix+endpoints["operationalState"], handleHttpOperationalState)
mux.HandleFunc(config.APIPrefix+endpoints["qkdInterface"], handleHttpQkdInterface)
mux.HandleFunc(config.APIPrefix+endpoints["interCKMSInterface"], handleHttpInterCKMSInterface)
mux.HandleFunc(config.APIPrefix+endpoints["akmsInterface"], handleHttpAkmsInterface)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment