X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fserver%2Fserver.go;h=0b5e5b8c9c6befc2c5ff84f1967d147b298e9afd;hb=fc15c05eeaedbb68fd58b7387ff707bd23f49eef;hp=ca30d73ec60c9aa476ddfb84597dd4de7b1cb486;hpb=64654a9b0e63b7e36249bef4f542cdf7fac56020;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/server/server.go b/dmaap-mediator-producer/internal/server/server.go index ca30d73e..0b5e5b8c 100644 --- a/dmaap-mediator-producer/internal/server/server.go +++ b/dmaap-mediator-producer/internal/server/server.go @@ -21,20 +21,53 @@ package server import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" + + "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" ) +const StatusCallbackPath = "/status" +const JobsCallbackPath = "/jobs" + func StatusHandler(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { + if r.URL.Path != StatusCallbackPath { http.Error(w, "404 not found.", http.StatusNotFound) return } 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 != JobsCallbackPath { + 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) + } +}