web

package
v0.2.20 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: MIT Imports: 13 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

	OpenAPI          *openapi3.Spec
	OpenAPICollector *openapi.Collector
	DecoderFactory   *request.DecoderFactory
}

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"`
}

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

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/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) 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