X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fserver%2Fserver.go;h=c3a1331cabc1f779495ae1971d9f29d335ae5090;hb=c94a2e14a26dd4e20808189f44e567b982f92d32;hp=ca30d73ec60c9aa476ddfb84597dd4de7b1cb486;hpb=ba96d847ab72a882bfe3acd39bc70c8f544f094a;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/server/server.go b/dmaap-mediator-producer/internal/server/server.go index ca30d73e..c3a1331c 100644 --- a/dmaap-mediator-producer/internal/server/server.go +++ b/dmaap-mediator-producer/internal/server/server.go @@ -21,8 +21,12 @@ package server import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" + + "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" ) func StatusHandler(w http.ResponseWriter, r *http.Request) { @@ -32,9 +36,46 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) { } if r.Method != "GET" { - http.Error(w, "Method is not supported.", http.StatusNotFound) + http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed) return } fmt.Fprintf(w, "All is well!") } + +func CreateInfoJobHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/producer_simulator/info_job" { + http.Error(w, "404 not found.", http.StatusNotFound) + return + } + + if r.Method != "POST" { + http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed) + return + } + + b, readErr := ioutil.ReadAll(r.Body) + if readErr != nil { + http.Error(w, fmt.Sprintf("Unable to read body due to: %v", readErr), http.StatusBadRequest) + return + } + jobInfo := jobs.JobInfo{} + if unmarshalErr := json.Unmarshal(b, &jobInfo); unmarshalErr != nil { + http.Error(w, fmt.Sprintf("Invalid json body. Cause: %v", unmarshalErr), http.StatusBadRequest) + return + } + if err := jobs.AddJob(jobInfo); err != nil { + http.Error(w, fmt.Sprintf("Invalid job info. Cause: %v", err), http.StatusBadRequest) + } +} + +func CreateServer(port int, handlerFunc func(http.ResponseWriter, *http.Request)) *http.Server { + + mux := http.NewServeMux() + mux.HandleFunc("/", handlerFunc) + server := http.Server{ + Addr: fmt.Sprintf(":%v", port), // :{port} + Handler: mux, + } + return &server +}