api

package
v0.0.0-...-381b1be Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 22 Imported by: 0

README

api

-- import "github.com/dockerian/go-coding/pkg/api"

Package api :: appError.go

api.Error interface wraps error with http status. api.AppError composes error and http status code for http handler without accessing to header in http.ResponseWriter.

Package api :: appHandler.go

api.AppHandler declares an extended http.Handler with configuration data and error (see api.AppError).

Package api :: appLogger.go - logging handlers

Package api :: appServer.go

Package api :: auth.go - auth handlers

Package api :: formatter.go - api formatters

Package api :: markdown.go - a Markdown handler

Package api :: params.go - http request parameters

Package api :: proxy.go - proxy handler

For each Proxy redirect/forward call, api.ProxyRoute defines a RedirectURL per prefix path. The api.ProxyRoute implements http.Handler interface so that the struct pointer itself can be wrapped in a routing configuration; optionally, a Proxy() can construct a http.Handler with prefix and predefined redirect URL.

Package api :: redirect.go - http redirect handlers

Package api :: route.go

Usage

func AppLogger
func AppLogger(handler http.Handler, name string) http.Handler

AppLogger is a middleware handler returns http.HandlerFunc

func Auth
func Auth(next http.Handler, token string) http.Handler

Auth creates a http.Handler wrapper to check api token in request

func BasicAuth
func BasicAuth(next http.Handler, user, password string) http.Handler

BasicAuth creates a http.Handler wrapper to check auth in request

func GetJSONEncoder
func GetJSONEncoder(w io.Writer, indent string) *json.Encoder

GetJSONEncoder returns JSON by specified indent

func NewLogger
func NewLogger(prefix string) *negroni.Logger

NewLogger returns a new negroni.Logger instance

func NewMarkdown
func NewMarkdown(prefix, dir, indexFile string) negroni.Handler

NewMarkdown constructs a MarkdownHandler

func NewMiddleware
func NewMiddleware() *negroni.Negroni

NewMiddleware returns a negroni middleware

func NewRecovery
func NewRecovery() *negroni.Recovery

NewRecovery returns a negroni recovery handler

func NewRouter
func NewRouter(ctx cfg.Context, routeConfigs RouteConfigs) *mux.Router

NewRouter returns *mux.Router

func Proxy
func Proxy(prefix, redirectURL string, w http.ResponseWriter, r *http.Request) error

Proxy generates new request with prefix path to redirected url

func ProxyHandler
func ProxyHandler(prefix, redirectURL string) http.Handler

ProxyHandler constructs an http.Handler by prefix path and redirect URL

func Redirect
func Redirect(prefix, redirectURL string, w http.ResponseWriter, r *http.Request) error

Redirect forwards call by prefix path to redirected url

func RedirectHandler
func RedirectHandler(prefix, redirectURL string) http.Handler

RedirectHandler constructs an http.Handler by prefix path and redirect URL

func RedirectToHTTPS
func RedirectToHTTPS(next http.Handler) http.Handler

RedirectToHTTPS is a middleware handler returns http.HandlerFunc

func WriteJSON
func WriteJSON(w http.ResponseWriter, code int, data interface{})

WriteJSON writes status code and response data

func WriteZIP
func WriteZIP(w http.ResponseWriter, data []byte, filename string)

WriteZIP writes a zip from buffer

type AppError
type AppError struct {
	// Err inherits standard error interface
	Err error `json:"-"`
	// ErrorMessage represents Err.Error()
	ErrorMessage string `json:"message,omitempty"`
	// StatusCode is http status code
	StatusCode int `json:"code,omitempty"`
}

AppError represents an error with an associated HTTP status code.

func NewAppError
func NewAppError(code int, message string) *AppError

NewAppError constructs an AppError

func (AppError) Error
func (ae AppError) Error() string

Error for AppError to implement the error interface.

func (AppError) Status
func (ae AppError) Status() int

Status returns HTTP status code.

func (AppError) StatusText
func (ae AppError) StatusText() string

StatusText returns HTTP status text.

type AppHandler
type AppHandler struct {
	cfg.Context
	Handle func(e cfg.Context, w http.ResponseWriter, r *http.Request) error
}

AppHandler struct wraps Env and Handle function implementing http.Handler

func (AppHandler) ServeHTTP
func (ah AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type AppLoggerResponseWriter
type AppLoggerResponseWriter struct {
	http.ResponseWriter
	StatusCode int
}

AppLoggerResponseWriter implements http.ResponseWriter

func (*AppLoggerResponseWriter) WriteHeader
func (alw *AppLoggerResponseWriter) WriteHeader(code int)

WriteHeader implements http.ResponseWriter interface

type AppServer
type AppServer struct {
	Ctx     *cfg.Context       // app context
	Doc     negroni.Handler    // negroni static handler
	Handler *negroni.Negroni   // negroni handler
	Logger  *negroni.Logger    // negroni logger
	Router  *mux.Router        // mux router
	Server  AppServerInterface // http.Server implements ListenAndServe
}

AppServer represents partial http.Server

type AppServerInterface
type AppServerInterface interface {
	ListenAndServe() error
	ListenAndServeTLS(string, string) error
}

AppServerInterface represents partial http.Server interface

type BodyParams
type BodyParams map[string]interface{}

BodyParams struct contains an JSON key/value map object

type Error
type Error interface {
	error
	Status() int
}

Error represents a handler error to provide Status() and embed the built-in error interface.

func WriteAppError
func WriteAppError(w http.ResponseWriter, appError AppError) Error

WriteAppError writes status code and returns Error

func WriteError
func WriteError(w http.ResponseWriter, code int, message string) Error

WriteError writes status code and returns Error

type HandlerFunc
type HandlerFunc func(ctx cfg.Context, w http.ResponseWriter, r *http.Request) error

HandlerFunc represents a handler function with cfg.Context

type MarkdownHandler
type MarkdownHandler struct {
}

MarkdownHandler struct is a negroni.Handler

func (*MarkdownHandler) ServeHTTP
func (m *MarkdownHandler) ServeHTTP(
	rw http.ResponseWriter, req *http.Request, next http.HandlerFunc)

ServeHTTP implements negroni.Handler interface

type Params
type Params struct {
	Form url.Values
	Body []byte
	Path string
	Post BodyParams
	Vars map[string]string
}

Params struct contains key/value pairs from URL path, request body, and query string

func NewParams
func NewParams(r *http.Request) *Params

NewParams returns pointer to an instance of Params struct with parsed http.Request

func (*Params) GetBody
func (params *Params) GetBody(key string) interface{}

GetBody method returns pointer to body param by key name

func (*Params) GetDateRange
func (params *Params) GetDateRange(key string) ([]time.Time, error)

GetDateRange method returns a date range by the key name

func (*Params) GetDateValues
func (params *Params) GetDateValues(key string) ([]time.Time, error)

GetDateValues method returns sorted date values by the key name

func (*Params) GetInt
func (params *Params) GetInt(key string, defaultValues ...int) (int, error)

GetInt method returns int value by the key name or the second parameter as default value

func (*Params) GetIntByRange
func (params *Params) GetIntByRange(key string, rangeValues ...int) int

GetIntByRange method returns int value by the key name and within the range of rangeValues parameters

func (*Params) GetNextPageURL
func (params *Params) GetNextPageURL(pgOffsetKey string, pgOffset int) string

GetNextPageURL returns next page URL per current page offset

func (*Params) GetValue
func (params *Params) GetValue(key string, defaultValues ...string) string

GetValue method returns the value string by the key name or the second parameter as default value

func (*Params) GetValues
func (params *Params) GetValues(key string) []string

GetValues method returns values by the key name

func (*Params) HasKey
func (params *Params) HasKey(key string) bool

HasKey returns true if the params has the key; otherwise, return false

type ProxyClient
type ProxyClient interface {
	Do(*http.Request) (*http.Response, error)
}

ProxyClient interface

type ProxyRoute
type ProxyRoute struct {
	// Prefix defines the matching prefix path to be replaced
	Prefix string
	// RedirectOnly specifies to use httpRedirect rather than a proxy client
	RedirectOnly bool
	// RedirectURL defines the replacing URL path
	RedirectURL string
}

ProxyRoute struct defines a redirecting URL based on pattern by converting matched Prefix path to RedirectURL

func (*ProxyRoute) ServeHTTP
func (p *ProxyRoute) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type RouteConfig
type RouteConfig struct {
	Pattern     string
	Method      string
	Name        string
	HandlerFunc func(e cfg.Context, w http.ResponseWriter, r *http.Request) error
	Proxy       ProxyRoute
}

RouteConfig struct

type RouteConfigs
type RouteConfigs []RouteConfig

RouteConfigs is a list of RouteConfig struct

Documentation

Overview

Package api :: appError.go

api.Error interface wraps error with http status. api.AppError composes error and http status code for http handler without accessing to header in http.ResponseWriter.

Package api :: appHandler.go

api.AppHandler declares an extended http.Handler with configuration data and error (see api.AppError).

Package api :: appLogger.go - logging handlers

Package api :: appServer.go

Package api :: auth.go - auth handlers

Package api :: formatter.go - api formatters

Package api :: markdown.go - a Markdown handler

Package api :: params.go - http request parameters

Package api :: proxy.go - proxy handler

For each Proxy redirect/forward call, api.ProxyRoute defines a RedirectURL per prefix path. The api.ProxyRoute implements http.Handler interface so that the struct pointer itself can be wrapped in a routing configuration; optionally, a Proxy() can construct a http.Handler with prefix and predefined redirect URL.

Package api :: redirect.go - http redirect handlers

Package api :: route.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppLogger

func AppLogger(handler http.Handler, name string) http.Handler

AppLogger is a middleware handler returns http.HandlerFunc

func Auth

func Auth(next http.Handler, token string) http.Handler

Auth creates a http.Handler wrapper to check api token in request

func BasicAuth

func BasicAuth(next http.Handler, user, password string) http.Handler

BasicAuth creates a http.Handler wrapper to check auth in request

func GetJSONEncoder

func GetJSONEncoder(w io.Writer, indent string) *json.Encoder

GetJSONEncoder returns JSON by specified indent

func NewLogger

func NewLogger(prefix string) *negroni.Logger

NewLogger returns a new negroni.Logger instance

func NewMarkdown

func NewMarkdown(prefix, dir, indexFile string) negroni.Handler

NewMarkdown constructs a MarkdownHandler

func NewMiddleware

func NewMiddleware() *negroni.Negroni

NewMiddleware returns a negroni middleware

func NewRecovery

func NewRecovery() *negroni.Recovery

NewRecovery returns a negroni recovery handler

func NewRouter

func NewRouter(ctx cfg.Context, routeConfigs RouteConfigs) *mux.Router

NewRouter returns *mux.Router

func Proxy

func Proxy(prefix, redirectURL string, w http.ResponseWriter, r *http.Request) error

Proxy generates new request with prefix path to redirected url

func ProxyHandler

func ProxyHandler(prefix, redirectURL string) http.Handler

ProxyHandler constructs an http.Handler by prefix path and redirect URL

func Redirect

func Redirect(prefix, redirectURL string, w http.ResponseWriter, r *http.Request) error

Redirect forwards call by prefix path to redirected url

func RedirectHandler

func RedirectHandler(prefix, redirectURL string) http.Handler

RedirectHandler constructs an http.Handler by prefix path and redirect URL

func RedirectToHTTPS

func RedirectToHTTPS(next http.Handler) http.Handler

RedirectToHTTPS is a middleware handler returns http.HandlerFunc

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, data interface{})

WriteJSON writes status code and response data

func WriteZIP

func WriteZIP(w http.ResponseWriter, data []byte, filename string)

WriteZIP writes a zip from buffer

Types

type AppError

type AppError struct {
	// Err inherits standard error interface
	Err error `json:"-"`
	// ErrorMessage represents Err.Error()
	ErrorMessage string `json:"message,omitempty"`
	// StatusCode is http status code
	StatusCode int `json:"code,omitempty"`
}

AppError represents an error with an associated HTTP status code.

func NewAppError

func NewAppError(code int, message string) *AppError

NewAppError constructs an AppError

func (AppError) Error

func (ae AppError) Error() string

Error for AppError to implement the error interface.

func (AppError) Status

func (ae AppError) Status() int

Status returns HTTP status code.

func (AppError) StatusText

func (ae AppError) StatusText() string

StatusText returns HTTP status text.

type AppHandler

type AppHandler struct {
	cfg.Context
	Handle func(e cfg.Context, w http.ResponseWriter, r *http.Request) error
}

AppHandler struct wraps Env and Handle function implementing http.Handler

func (AppHandler) ServeHTTP

func (ah AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type AppLoggerResponseWriter

type AppLoggerResponseWriter struct {
	http.ResponseWriter
	StatusCode int
}

AppLoggerResponseWriter implements http.ResponseWriter

func (*AppLoggerResponseWriter) WriteHeader

func (alw *AppLoggerResponseWriter) WriteHeader(code int)

WriteHeader implements http.ResponseWriter interface

type AppServer

type AppServer struct {
	Ctx     *cfg.Context       // app context
	Doc     negroni.Handler    // negroni static handler
	Handler *negroni.Negroni   // negroni handler
	Logger  *negroni.Logger    // negroni logger
	Router  *mux.Router        // mux router
	Server  AppServerInterface // http.Server implements ListenAndServe
}

AppServer represents partial http.Server

type AppServerInterface

type AppServerInterface interface {
	ListenAndServe() error
	ListenAndServeTLS(string, string) error
}

AppServerInterface represents partial http.Server interface

type BodyParams

type BodyParams map[string]interface{}

BodyParams struct contains an JSON key/value map object

type Error

type Error interface {
	error
	Status() int
}

Error represents a handler error to provide Status() and embed the built-in error interface.

func WriteAppError

func WriteAppError(w http.ResponseWriter, appError AppError) Error

WriteAppError writes status code and returns Error

func WriteError

func WriteError(w http.ResponseWriter, code int, message string) Error

WriteError writes status code and returns Error

type HandlerFunc

type HandlerFunc func(ctx cfg.Context, w http.ResponseWriter, r *http.Request) error

HandlerFunc represents a handler function with cfg.Context

type MarkdownHandler

type MarkdownHandler struct {
	// contains filtered or unexported fields
}

MarkdownHandler struct is a negroni.Handler

func (*MarkdownHandler) ServeHTTP

func (m *MarkdownHandler) ServeHTTP(
	rw http.ResponseWriter, req *http.Request, next http.HandlerFunc)

ServeHTTP implements negroni.Handler interface

type Params

type Params struct {
	Form url.Values
	Body []byte
	Path string
	Post BodyParams
	Vars map[string]string
}

Params struct contains key/value pairs from URL path, request body, and query string

func NewParams

func NewParams(r *http.Request) *Params

NewParams returns pointer to an instance of Params struct with parsed http.Request

func (*Params) GetBody

func (params *Params) GetBody(key string) interface{}

GetBody method returns pointer to body param by key name

func (*Params) GetDateRange

func (params *Params) GetDateRange(key string) ([]time.Time, error)

GetDateRange method returns a date range by the key name

func (*Params) GetDateValues

func (params *Params) GetDateValues(key string) ([]time.Time, error)

GetDateValues method returns sorted date values by the key name

func (*Params) GetInt

func (params *Params) GetInt(key string, defaultValues ...int) (int, error)

GetInt method returns int value by the key name or the second parameter as default value

func (*Params) GetIntByRange

func (params *Params) GetIntByRange(key string, rangeValues ...int) int

GetIntByRange method returns int value by the key name and within the range of rangeValues parameters

func (*Params) GetNextPageURL

func (params *Params) GetNextPageURL(pgOffsetKey string, pgOffset int) string

GetNextPageURL returns next page URL per current page offset

func (*Params) GetValue

func (params *Params) GetValue(key string, defaultValues ...string) string

GetValue method returns the value string by the key name or the second parameter as default value

func (*Params) GetValues

func (params *Params) GetValues(key string) []string

GetValues method returns values by the key name

func (*Params) HasKey

func (params *Params) HasKey(key string) bool

HasKey returns true if the params has the key; otherwise, return false

type ProxyClient

type ProxyClient interface {
	Do(*http.Request) (*http.Response, error)
}

ProxyClient interface

type ProxyRoute

type ProxyRoute struct {
	// Prefix defines the matching prefix path to be replaced
	Prefix string
	// RedirectOnly specifies to use httpRedirect rather than a proxy client
	RedirectOnly bool
	// RedirectURL defines the replacing URL path
	RedirectURL string
}

ProxyRoute struct defines a redirecting URL based on pattern by converting matched Prefix path to RedirectURL

func (*ProxyRoute) ServeHTTP

func (p *ProxyRoute) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type RouteConfig

type RouteConfig struct {
	Pattern     string
	Method      string
	Name        string
	HandlerFunc func(e cfg.Context, w http.ResponseWriter, r *http.Request) error
	Proxy       ProxyRoute
}

RouteConfig struct

type RouteConfigs

type RouteConfigs []RouteConfig

RouteConfigs is a list of RouteConfig struct

Jump to

Keyboard shortcuts

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