本文整理匯總了Golang中github.com/emicklei/go-restful.WebService類的典型用法代碼示例。如果您正苦於以下問題:Golang WebService類的具體用法?Golang WebService怎麽用?Golang WebService使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了WebService類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setupHandlers
func setupHandlers(metricSink *metricsink.MetricSink, podLister *cache.StoreToPodLister) http.Handler {
runningInKubernetes := true
// Make API handler.
wsContainer := restful.NewContainer()
wsContainer.EnableContentEncoding(true)
wsContainer.Router(restful.CurlyRouter{})
a := v1.NewApi(runningInKubernetes, metricSink)
a.Register(wsContainer)
// Metrics API
m := metricsApi.NewApi(metricSink, podLister)
m.Register(wsContainer)
handlePprofEndpoint := func(req *restful.Request, resp *restful.Response) {
name := strings.TrimPrefix(req.Request.URL.Path, pprofBasePath)
switch name {
case "profile":
pprof.Profile(resp, req.Request)
case "symbol":
pprof.Symbol(resp, req.Request)
case "cmdline":
pprof.Cmdline(resp, req.Request)
default:
pprof.Index(resp, req.Request)
}
}
// Setup pporf handlers.
ws := new(restful.WebService).Path(pprofBasePath)
ws.Route(ws.GET("/{subpath:*}").To(metrics.InstrumentRouteFunc("pprof", handlePprofEndpoint))).Doc("pprof endpoint")
wsContainer.Add(ws)
return wsContainer
}
示例2: initAPIVersionRoute
// initAPIVersionRoute initializes the osapi endpoint to behave similar to the upstream api endpoint
func initAPIVersionRoute(root *restful.WebService, prefix string, versions ...string) {
versionHandler := apiserver.APIVersionHandler(versions...)
root.Route(root.GET(prefix).To(versionHandler).
Doc("list supported server API versions").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON))
}
示例3: initMetricsRoute
// initHealthCheckRoute initalizes an HTTP endpoint for health checking.
// OpenShift is deemed healthy if the API server can respond with an OK messages
func initMetricsRoute(root *restful.WebService, path string) {
h := prometheus.Handler()
root.Route(root.GET(path).To(func(req *restful.Request, resp *restful.Response) {
h.ServeHTTP(resp.ResponseWriter, req.Request)
}).Doc("return metrics for this process").
Returns(http.StatusOK, "if metrics are available", nil).
Produces("text/plain"))
}
示例4: initHealthCheckRoute
// initHealthCheckRoute initalizes an HTTP endpoint for health checking.
// OpenShift is deemed healthy if the API server can respond with an OK messages
func initHealthCheckRoute(root *restful.WebService, path string) {
root.Route(root.GET(path).To(func(req *restful.Request, resp *restful.Response) {
resp.ResponseWriter.WriteHeader(http.StatusOK)
resp.ResponseWriter.Write([]byte("ok"))
}).Doc("return the health state of the master").
Returns(http.StatusOK, "if master is healthy", nil).
Produces(restful.MIME_JSON))
}
示例5: initOAuthAuthorizationServerMetadataRoute
// initOAuthAuthorizationServerMetadataRoute initializes an HTTP endpoint for OAuth 2.0 Authorization Server Metadata discovery
// https://tools.ietf.org/id/draft-ietf-oauth-discovery-04.html#rfc.section.2
// masterPublicURL should be internally and externally routable to allow all users to discover this information
func initOAuthAuthorizationServerMetadataRoute(apiContainer *genericmux.APIContainer, path, masterPublicURL string) {
// Build OAuth metadata once
metadata, err := json.MarshalIndent(discovery.Get(masterPublicURL, OpenShiftOAuthAuthorizeURL(masterPublicURL), OpenShiftOAuthTokenURL(masterPublicURL)), "", " ")
if err != nil {
glog.Errorf("Unable to initialize OAuth authorization server metadata route: %v", err)
return
}
secretContainer := restful.Container{
ServeMux: apiContainer.SecretRoutes.(*http.ServeMux), // we know it's a *http.ServeMux. In kube 1.6, the type will actually be correct.
}
// Set up a service to return the OAuth metadata.
ws := new(restful.WebService)
ws.Path(path)
ws.Doc("OAuth 2.0 Authorization Server Metadata")
ws.Route(
ws.GET("/").To(func(_ *restful.Request, resp *restful.Response) {
writeJSON(resp, metadata)
}).
Doc("get the server's OAuth 2.0 Authorization Server Metadata").
Operation("getOAuthAuthorizationServerMetadata").
Produces(restful.MIME_JSON))
secretContainer.Add(ws)
}
示例6: initAPIVersionRoute
// initAPIVersionRoute initializes the osapi endpoint to behave similar to the upstream api endpoint
func initAPIVersionRoute(root *restful.WebService, prefix string, versions ...string) {
versionHandler := apiserver.APIVersionHandler(kapi.Codecs, func(req *restful.Request) *unversioned.APIVersions {
apiVersionsForDiscovery := unversioned.APIVersions{
// TODO: ServerAddressByClientCIDRs: s.getServerAddressByClientCIDRs(req.Request),
Versions: versions,
}
return &apiVersionsForDiscovery
})
root.Route(root.GET(prefix).To(versionHandler).
Doc("list supported server API versions").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON))
}
示例7: initMetricsRoute
// initMetricsRoute initializes an HTTP endpoint for metrics.
func initMetricsRoute(apiContainer *genericmux.APIContainer, path string) {
ws := new(restful.WebService).
Path(path).
Doc("return metrics for this process")
h := prometheus.Handler()
ws.Route(ws.GET("/").To(func(req *restful.Request, resp *restful.Response) {
h.ServeHTTP(resp.ResponseWriter, req.Request)
}).Doc("return metrics for this process").
Returns(http.StatusOK, "if metrics are available", nil).
Produces("text/plain"))
apiContainer.Add(ws)
}
示例8: NewService
// 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}
}
示例9: initReadinessCheckRoute
// initReadinessCheckRoute initializes an HTTP endpoint for readiness checking
func initReadinessCheckRoute(root *restful.WebService, path string, readyFunc func() bool) {
root.Route(root.GET(path).To(func(req *restful.Request, resp *restful.Response) {
if readyFunc() {
resp.ResponseWriter.WriteHeader(http.StatusOK)
resp.ResponseWriter.Write([]byte("ok"))
} else {
resp.ResponseWriter.WriteHeader(http.StatusServiceUnavailable)
}
}).Doc("return the readiness state of the master").
Returns(http.StatusOK, "if the master is ready", nil).
Returns(http.StatusServiceUnavailable, "if the master is not ready", nil).
Produces(restful.MIME_JSON))
}
示例10: initVersionRoute
// initReadinessCheckRoute initializes an HTTP endpoint for readiness checking
func initVersionRoute(container *restful.Container, path string) {
// Set up a service to return the git code version.
versionWS := new(restful.WebService)
versionWS.Path(path)
versionWS.Doc("git code version from which this is built")
versionWS.Route(
versionWS.GET("/").To(handleVersion).
Doc("get the code version").
Operation("getCodeVersion").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON))
container.Add(versionWS)
}
示例11: CreateApiHandler
// Creates a new HTTP handler that handles all requests to the API of the backend.
func CreateApiHandler(client *client.Client) http.Handler {
wsContainer := restful.NewContainer()
// TODO(bryk): This is for tests only. Replace with real implementation once ready.
ws := new(restful.WebService)
ws.Path("/api/deploy").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
ws.Route(ws.POST("").To(func(request *restful.Request, response *restful.Response) {
cfg := new(DeployAppConfig)
if err := request.ReadEntity(cfg); err != nil {
HandleInternalError(response, err)
return
}
if err := DeployApp(cfg, client); err != nil {
HandleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusCreated, cfg)
}).Reads(DeployAppConfig{}).Writes(DeployAppConfig{}))
wsContainer.Add(ws)
return wsContainer
}
示例12: initAPIVersionRoute
// initAPIVersionRoute initializes the osapi endpoint to behave similar to the upstream api endpoint
func initAPIVersionRoute(apiContainer *genericmux.APIContainer, prefix string, versions ...string) {
versionHandler := apiserver.APIVersionHandler(kapi.Codecs, func(req *restful.Request) *unversioned.APIVersions {
apiVersionsForDiscovery := unversioned.APIVersions{
// TODO: ServerAddressByClientCIDRs: s.getServerAddressByClientCIDRs(req.Request),
Versions: versions,
}
return &apiVersionsForDiscovery
})
ws := new(restful.WebService).
Path(prefix).
Doc("list supported server API versions")
ws.Route(ws.GET("/").To(versionHandler).
Doc("list supported server API versions").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON).
Operation("get" + strings.Title(prefix[1:]) + "Version"))
apiContainer.Add(ws)
}
示例13: initReadinessCheckRoute
// initReadinessCheckRoute initializes an HTTP endpoint for readiness checking
func initReadinessCheckRoute(apiContainer *genericmux.APIContainer, path string, readyFunc func() bool) {
ws := new(restful.WebService).
Path(path).
Doc("return the readiness state of the master")
ws.Route(ws.GET("/").To(func(req *restful.Request, resp *restful.Response) {
if readyFunc() {
resp.ResponseWriter.WriteHeader(http.StatusOK)
resp.ResponseWriter.Write([]byte("ok"))
} else {
resp.ResponseWriter.WriteHeader(http.StatusServiceUnavailable)
}
}).Doc("return the readiness state of the master").
Returns(http.StatusOK, "if the master is ready", nil).
Returns(http.StatusServiceUnavailable, "if the master is not ready", nil).
Produces(restful.MIME_JSON))
apiContainer.Add(ws)
}
示例14: NewService
// 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,
}
}
示例15: initOAuthAuthorizationServerMetadataRoute
// initOAuthAuthorizationServerMetadataRoute initializes an HTTP endpoint for OAuth 2.0 Authorization Server Metadata discovery
// https://tools.ietf.org/id/draft-ietf-oauth-discovery-04.html#rfc.section.2
// masterPublicURL should be internally and externally routable to allow all users to discover this information
func initOAuthAuthorizationServerMetadataRoute(container *restful.Container, path, masterPublicURL string) {
// Build OAuth metadata once
metadata, err := json.MarshalIndent(discovery.Get(masterPublicURL, OpenShiftOAuthAuthorizeURL(masterPublicURL), OpenShiftOAuthTokenURL(masterPublicURL)), "", " ")
if err != nil {
glog.Errorf("Unable to initialize OAuth authorization server metadata route: %v", err)
return
}
// Set up a service to return the OAuth metadata.
oauthWS := new(restful.WebService)
oauthWS.Path(path)
oauthWS.Doc("OAuth 2.0 Authorization Server Metadata")
oauthWS.Route(
oauthWS.GET("/").To(func(_ *restful.Request, resp *restful.Response) {
writeJSON(resp, metadata)
}).
Doc("get the server's OAuth 2.0 Authorization Server Metadata").
Operation("getOAuthAuthorizationServerMetadata").
Produces(restful.MIME_JSON))
container.Add(oauthWS)
}