()
| 26 | ) |
| 27 | |
| 28 | func (s *Server) makeStateService() *restful.WebService { |
| 29 | ws := new(restful.WebService) |
| 30 | ws.Path("/api/v1/state") |
| 31 | |
| 32 | tags := []string{"state"} |
| 33 | |
| 34 | keyParam := ws.PathParameter("key", "state key").DataType("string") |
| 35 | |
| 36 | ws.Route(ws.POST("/{key}"). |
| 37 | To(func(request *restful.Request, response *restful.Response) { |
| 38 | key := request.PathParameter("key") |
| 39 | |
| 40 | state := s.Manager.GetStateStore() |
| 41 | if state == nil { |
| 42 | s.handleRestError(response.WriteErrorString(http.StatusBadRequest, "No state store configured")) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | body := request.Request.Body |
| 47 | defer func() { |
| 48 | s.handleRestError(body.Close()) |
| 49 | }() |
| 50 | |
| 51 | content, err := io.ReadAll(body) |
| 52 | if err != nil { |
| 53 | s.handleRestError(response.WriteError(http.StatusBadRequest, errors.Wrap(err, "Failed to read body"))) |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | err = state.PutState(key, content) |
| 58 | if err != nil { |
| 59 | s.handleRestError(response.WriteError(http.StatusInternalServerError, err)) |
| 60 | return |
| 61 | } |
| 62 | }). |
| 63 | Doc("set a state"). |
| 64 | Metadata(restfulspec.KeyOpenAPITags, tags). |
| 65 | Operation("setState"). |
| 66 | Param(keyParam). |
| 67 | Reads(bytesSchema)) |
| 68 | |
| 69 | ws.Route(ws.GET("/{key}"). |
| 70 | To(func(request *restful.Request, response *restful.Response) { |
| 71 | key := request.PathParameter("key") |
| 72 | state := s.Manager.GetStateStore() |
| 73 | if state == nil { |
| 74 | s.handleRestError(response.WriteErrorString(http.StatusBadRequest, "No state store configured")) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | content, err := state.GetState(key) |
| 79 | if err != nil { |
| 80 | s.handleRestError(response.WriteError(http.StatusInternalServerError, err)) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | _, err = response.Write(content) |
| 85 | s.handleRestError(err) |
no test coverage detected