()
| 30 | var bytesSchema = restfulspec.SchemaType{RawType: "string", Format: "byte"} |
| 31 | |
| 32 | func (s *Server) makeTubeService() *restful.WebService { |
| 33 | |
| 34 | ws := new(restful.WebService) |
| 35 | ws.Path("/api/v1"). |
| 36 | Consumes(restful.MIME_JSON). |
| 37 | Produces(restful.MIME_JSON) |
| 38 | |
| 39 | tags := []string{"tube"} |
| 40 | |
| 41 | tubeName := ws.PathParameter("name", "tube name").DataType("string") |
| 42 | |
| 43 | ws.Route(ws.POST("/produce/{name}"). |
| 44 | To(func(request *restful.Request, response *restful.Response) { |
| 45 | name := request.PathParameter("name") |
| 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.WriteErrorString(http.StatusInternalServerError, err.Error())) |
| 54 | return |
| 55 | } |
| 56 | err = s.Manager.ProduceEvent(name, contube.NewRecordImpl(content, func() {})) |
| 57 | if err != nil { |
| 58 | s.handleRestError(response.WriteError(http.StatusInternalServerError, err)) |
| 59 | return |
| 60 | } |
| 61 | response.WriteHeader(http.StatusOK) |
| 62 | }). |
| 63 | Doc("produce a message"). |
| 64 | Metadata(restfulspec.KeyOpenAPITags, tags). |
| 65 | Operation("produceMessage"). |
| 66 | Reads(bytesSchema). |
| 67 | Param(tubeName)) |
| 68 | |
| 69 | ws.Route(ws.GET("/consume/{name}"). |
| 70 | To(func(request *restful.Request, response *restful.Response) { |
| 71 | name := request.PathParameter("name") |
| 72 | record, err := s.Manager.ConsumeEvent(name) |
| 73 | if err != nil { |
| 74 | s.handleRestError(response.WriteError(http.StatusInternalServerError, err)) |
| 75 | return |
| 76 | } |
| 77 | _, err = response.Write(record.GetPayload()) |
| 78 | s.handleRestError(err) |
| 79 | }). |
| 80 | Doc("consume a message"). |
| 81 | Metadata(restfulspec.KeyOpenAPITags, tags). |
| 82 | Operation("consumeMessage"). |
| 83 | Writes(bytesSchema). |
| 84 | Returns(http.StatusOK, "OK", bytesSchema). |
| 85 | Param(tubeName)) |
| 86 | |
| 87 | return ws |
| 88 | } |
no test coverage detected