// NewService creates and returns a new Service, initalized with a new // restful.WebService configured with a route that dispatches to the supplied // handler. The new Service must be registered before accepting traffic by // calling Register. func NewService(handler restful.RouteFunction) *Service { restful.EnableTracing(true) webService := new(restful.WebService) webService.Consumes(restful.MIME_JSON, restful.MIME_XML) webService.Produces(restful.MIME_JSON, restful.MIME_XML) webService.Route(webService.POST("/expand").To(handler). Doc("Expand a template."). Reads(&expander.Template{})) return &Service{webService} }
// NewService encapsulates code to open an HTTP server on the given address:port that serves the // expansion API using the given Expander backend to do the actual expansion. After calling // NewService, call ListenAndServe to start the returned service. func NewService(address string, port int, backend Expander) *Service { restful.EnableTracing(true) webService := new(restful.WebService) webService.Consumes(restful.MIME_JSON) webService.Produces(restful.MIME_JSON) handler := func(req *restful.Request, resp *restful.Response) { util.LogHandlerEntry("expansion service", req.Request) request := &ServiceRequest{} if err := req.ReadEntity(&request); err != nil { badRequest(resp, err.Error()) return } reqMsg := fmt.Sprintf("\nhandling request:\n%s\n", util.ToYAMLOrError(request)) util.LogHandlerText("expansion service", reqMsg) response, err := backend.ExpandChart(request) if err != nil { badRequest(resp, fmt.Sprintf("error expanding chart: %s", err)) return } util.LogHandlerExit("expansion service", http.StatusOK, "OK", resp.ResponseWriter) respMsg := fmt.Sprintf("\nreturning response:\n%s\n", util.ToYAMLOrError(response.Resources)) util.LogHandlerText("expansion service", respMsg) resp.WriteEntity(response) } webService.Route( webService.POST("/expand"). To(handler). Doc("Expand a chart."). Reads(&ServiceRequest{}). Writes(&ServiceResponse{})) container := restful.DefaultContainer container.Add(webService) server := &http.Server{ Addr: fmt.Sprintf("%s:%d", address, port), Handler: container, } return &Service{ webService: webService, server: server, container: container, } }