web

package
v0.2.66 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 14 Imported by: 35

Documentation

Overview

Package web provides default facades for web service bootstrap.

Index

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

func DefaultService(options ...func(s *Service, initialized bool)) *Service

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

func NewService(refl oapi.Reflector, options ...func(s *Service)) *Service

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

func (s *Service) Docs(pattern string, swgui func(title, schemaURL, basePath string) http.Handler)

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

func (s *Service) Get(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Get adds the route `pattern` that matches a GET http method to invoke use case interactor.

func (*Service) Head

func (s *Service) Head(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Head adds the route `pattern` that matches a HEAD http method to invoke use case interactor.

func (*Service) OnMethodNotAllowed added in v0.2.61

func (s *Service) OnMethodNotAllowed(uc usecase.Interactor, options ...func(h *nethttp.Handler))

OnMethodNotAllowed registers usecase interactor as a handler for method not allowed conditions.

func (*Service) OnNotFound added in v0.2.61

func (s *Service) OnNotFound(uc usecase.Interactor, options ...func(h *nethttp.Handler))

OnNotFound registers usecase interactor as a handler for not found conditions.

func (*Service) OpenAPIReflector added in v0.2.54

func (s *Service) OpenAPIReflector() oapi.Reflector

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

func (s *Service) Patch(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Patch adds the route `pattern` that matches a PATCH http method to invoke use case interactor.

func (*Service) Post

func (s *Service) Post(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Post adds the route `pattern` that matches a POST http method to invoke use case interactor.

func (*Service) Put

func (s *Service) Put(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Put adds the route `pattern` that matches a PUT http method to invoke use case interactor.

func (*Service) Trace

func (s *Service) Trace(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler))

Trace adds the route `pattern` that matches a TRACE http method to invoke use case interactor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL