Documentation ¶
Overview ¶
Package web provides default facades for web service bootstrap.
Index ¶
- type Service
- func (s *Service) Delete(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Docs(pattern string, swgui func(title, schemaURL, basePath string) http.Handler)
- func (s *Service) Get(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Head(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) OpenAPIReflector() oapi.Reflector
- func (s *Service) OpenAPISchema() oapi.SpecSchema
- func (s *Service) Options(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Patch(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Post(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Put(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
- func (s *Service) Trace(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Service ¶
type Service struct { *chirouter.Wrapper PanicRecoveryMiddleware func(handler http.Handler) http.Handler // Default is middleware.Recoverer. // Deprecated: use OpenAPISchema(). OpenAPI *openapi3.Spec OpenAPICollector *openapi.Collector DecoderFactory *request.DecoderFactory // Response validation is not enabled by default for its less justifiable performance impact. // This field is populated so that response.ValidatorMiddleware(s.ResponseValidatorFactory) can be // added to service via Wrap. ResponseValidatorFactory rest.ResponseValidatorFactory }
Service keeps instrumented router and documentation collector.
func DefaultService
deprecated
DefaultService initializes router and other basic components of web service.
Provided functional options are invoked twice, before and after initialization.
Deprecated: use NewService.
Example ¶
package main import ( "context" "log" "net/http" "github.com/go-chi/chi/v5/middleware" "github.com/swaggest/openapi-go/openapi3" "github.com/swaggest/rest/nethttp" "github.com/swaggest/rest/web" "github.com/swaggest/usecase" ) // album represents data about a record album. type album struct { ID int `json:"id"` Title string `json:"title"` Artist string `json:"artist"` Price float64 `json:"price"` Locale string `query:"locale"` } func postAlbums() usecase.Interactor { u := usecase.NewIOI(new(album), new(album), func(ctx context.Context, input, output interface{}) error { log.Println("Creating album") return nil }) u.SetTags("Album") return u } func main() { // Service initializes router with required middlewares. service := web.NewService(openapi3.NewReflector()) // It allows OpenAPI configuration. service.OpenAPISchema().SetTitle("Albums API") service.OpenAPISchema().SetDescription("This service provides API to manage albums.") service.OpenAPISchema().SetVersion("v1.0.0") // Additional middlewares can be added. service.Use( middleware.StripSlashes, // cors.AllowAll().Handler, // "github.com/rs/cors", 3rd-party CORS middleware can also be configured here. ) service.Wrap() // Use cases can be mounted using short syntax .<Method>(...). service.Post("/albums", postAlbums(), nethttp.SuccessStatus(http.StatusCreated)) log.Println("Starting service at http://localhost:8080") if err := http.ListenAndServe("localhost:8080", service); err != nil { log.Fatal(err) } }
Output:
func NewService ¶ added in v0.2.55
NewService initializes router and other basic components of web service.
func (*Service) Delete ¶
func (s *Service) Delete(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
Delete adds the route `pattern` that matches a DELETE http method to invoke use case interactor.
func (*Service) Docs ¶
Docs adds the route `pattern` that serves API documentation with Swagger UI.
Swagger UI should be provided by `swgui` handler constructor, you can use one of these functions
github.com/swaggest/swgui/v5emb.New github.com/swaggest/swgui/v5cdn.New github.com/swaggest/swgui/v5.New github.com/swaggest/swgui/v4emb.New github.com/swaggest/swgui/v4cdn.New github.com/swaggest/swgui/v4.New github.com/swaggest/swgui/v3emb.New github.com/swaggest/swgui/v3cdn.New github.com/swaggest/swgui/v3.New
or create your own.
func (*Service) Get ¶
Get adds the route `pattern` that matches a GET http method to invoke use case interactor.
func (*Service) Head ¶
Head adds the route `pattern` that matches a HEAD http method to invoke use case interactor.
func (*Service) OpenAPIReflector ¶ added in v0.2.54
OpenAPIReflector returns OpenAPI structure reflector for customizations.
func (*Service) OpenAPISchema ¶ added in v0.2.54
func (s *Service) OpenAPISchema() oapi.SpecSchema
OpenAPISchema returns OpenAPI schema.
Returned value can be type asserted to *openapi3.Spec, *openapi31.Spec or marshaled.
func (*Service) Options ¶
func (s *Service) Options(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))
Options adds the route `pattern` that matches a OPTIONS http method to invoke use case interactor.
func (*Service) Patch ¶
Patch adds the route `pattern` that matches a PATCH http method to invoke use case interactor.
func (*Service) Post ¶
Post adds the route `pattern` that matches a POST http method to invoke use case interactor.