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) 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 OpenAPI *openapi3.Spec OpenAPICollector *openapi.Collector DecoderFactory *request.DecoderFactory ResponseValidatorFactory rest.ResponseValidatorFactory }
Service keeps instrumented router and documentation collector.
func DefaultService ¶
func DefaultService() *Service
DefaultService initializes router and other basic components of web service.
Example ¶
package main import ( "context" "log" "net/http" "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 := web.DefaultService() service.OpenAPI.Info.Title = "Albums API" service.OpenAPI.Info.WithDescription("This service provides API to manage albums.") service.OpenAPI.Info.Version = "v1.0.0" 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 (*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/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) 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.