web

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package web implements common functionality for web APIs using Gin and Gorm.

Modules

Modules provide functionality for a web server. A module is a function executed before starting a server, accessing the Service and the Gin Engine. Each Service maintains a list of modules. When it runs, it executes all of its modules.

Index

Constants

View Source
const (
	ContentTypeJSON = "application/json"
	ContentTypeJS   = "application/javascript"
	ContentTypeXML  = "text/xml"
	ContentTypeYAML = "text/yaml"
	ContentTypeHTML = "text/html"
)

MIME type strings.

Variables

View Source
var (
	ErrAPIInvalidRequestFormat = errors.New("Invalid Request Format")
	ErrAPIInternalDatabase     = errors.New("Internal Database Error")
	ErrAPINotFound             = errors.New("Not found")
)

Error strings used for HTTPError.Message.

Functions

func JSONRequest

func JSONRequest(url string, value interface{}) error

JSONRequest issues a GET request to the specified URL and reads the returned JSON into value. See json.Unmarshal for the rules for converting JSON into a value.

func NewRequestWithFile

func NewRequestWithFile(url, filename string) (*http.Request, error)

NewRequestWithFile Create a Request with file as body

func Response

func Response(ctx *gin.Context, statusCode int, data interface{}, templateName string)

Response sends an HTTP response.

statusCode is the respone's status.

If the request's Content-Type is JavaScript, JSON, YAML, or XML, it returns data serialized as JSONP, JSON, YAML, or XML, respectively. If the Content-Type is HTML, it returns the HTML template templateName rendered with data.

Types

type HTTPError

type HTTPError struct {
	Message string      `json:"message" example:"invalid format"`
	Error   string      `json:"error,omitempty" example:"<internal error message>"`
	Data    interface{} `json:"data,omitempty" swaggerignore:"true"`
}

HTTPError is returned in HTTP error responses.

type ModuleRegisterFunc

type ModuleRegisterFunc func(*gin.Engine, *Service)

A ModuleRegisterFunc is a module.

type Service

type Service struct {
	// config
	Listen              string          `config:"listen" toml:"listen"`
	AccessLog           bool            `config:"access_log" toml:"access_log"`
	WebrootIndexDisable bool            `config:"webroot_index_disable" toml:"webroot_index_disable"`
	Webroot             string          `config:"webroot" toml:"webroot"`
	WebrootFS           http.FileSystem `config:"-" toml:"-"`
	ACME                struct {
		Enable  bool     `config:"enable" toml:"enable"`
		Domains []string `config:"domains" toml:"domains"`
		Cache   string   `config: "cache" toml:"cache"`
	} `config:"acme" toml:"acme"`
	Session struct {
		Name   string `config:"name" toml:"name"`
		Secret string `config: "secret" toml:"secret"`
	} `config:"session" toml:"session"`
	// internal
	DB     *gorm.DB        `config:"-" toml:"-"`
	Mailer *mailer.Service `config:"-" toml:"-"`
	// contains filtered or unexported fields
}

A Service stores configuration of a server.

func (*Service) Bind

func (ws *Service) Bind(r *gin.Engine)

Bind executes all of ws's modules with r.

func (*Service) LoadSession

func (s *Service) LoadSession(r *gin.Engine)

LoadSession starts session handling for s.

func (*Service) Log

func (s *Service) Log() *zap.Logger

Log - get current logger

func (*Service) ModuleRegister

func (ws *Service) ModuleRegister(f ModuleRegisterFunc)

ModuleRegister adds f to ws's list of modules.

func (*Service) Run

func (s *Service) Run(log *zap.Logger) error

Run creates, configures, and runs a new gin.Engine using its registered modules.

func (*Service) SetLog

func (s *Service) SetLog(l *zap.Logger)

SetLog - set new logger

Directories

Path Synopsis
api
status
Package status implements for web the module to publish the status with version and other extras
Package status implements for web the module to publish the status with version and other extras
Package auth implements for web the module for authentification
Package auth implements for web the module for authentification
Package file abstracts non-hierarchical file stores.
Package file abstracts non-hierarchical file stores.
fs
Package fs implements a non-hierarchical file store using the underlying (disk) file system.
Package fs implements a non-hierarchical file store using the underlying (disk) file system.
s3
Package s3 implements a non-hierarchical file store using Amazon s3.
Package s3 implements a non-hierarchical file store using Amazon s3.
Package metrics implements for web the module to publish prometheus metrics
Package metrics implements for web the module to publish prometheus metrics
Package webtest implements an easy way to write test for web modules
Package webtest implements an easy way to write test for web modules

Jump to

Keyboard shortcuts

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