api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ROLE_GUEST string = "guest"
	ROLE_ADMIN string = "admin"
)
View Source
const (
	//GUEST
	PERMISSION_SNAPSHOT_VIEW string = "view_snapshot"

	//ADMIN
	PERMISSION_ADMIN_MINIMAL string = "admin_minimal"
)

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(h httprouter.Handle, requiredUser, requiredPassword string) httprouter.Handle

BasicAuth register api with basic auth

func CheckPermission

func CheckPermission(w http.ResponseWriter, r *http.Request, requiredPermission string) bool

func CheckPermissions

func CheckPermissions(w http.ResponseWriter, r *http.Request, requiredPermissions []string) bool

func EnableAuth

func EnableAuth(enable bool)

func GetFlash

func GetFlash(w http.ResponseWriter, r *http.Request) (bool, []interface{})

GetFlash get flash value

func GetLoginInfo

func GetLoginInfo(w http.ResponseWriter, r *http.Request) (user, role string)

func GetPermissionsByRole

func GetPermissionsByRole(role string) (*hashset.Set, error)

func GetSession

func GetSession(w http.ResponseWriter, r *http.Request, key string) (bool, interface{})

GetSession return session by session key

func GetSessionStore

func GetSessionStore(r *http.Request, key string) (*sessions.Session, error)

func HandleAPIFunc

func HandleAPIFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleAPIFunc register api handler to specify pattern

func HandleAPIMethod

func HandleAPIMethod(method Method, pattern string, handler func(w http.ResponseWriter, req *http.Request, ps httprouter.Params))

HandleAPIMethod register api handler

func IsAuthEnable

func IsAuthEnable() bool

func Login

func Login(w http.ResponseWriter, r *http.Request, user, role string)

func Logout

func Logout(w http.ResponseWriter, r *http.Request)

func NeedPermission

func NeedPermission(permission string, h httprouter.Handle) httprouter.Handle

func SetFlash

func SetFlash(w http.ResponseWriter, r *http.Request, msg string) bool

SetFlash set flash value

func SetSession

func SetSession(w http.ResponseWriter, r *http.Request, key string, value interface{}) bool

SetSession set session by session key and session value

func StartAPI

func StartAPI(cfg *config.Config)

StartAPI will start listen and act as the API server

Types

type APIConfig

type APIConfig struct {
	Enabled       bool                 `config:"enabled"`
	TLSConfig     config.TLSConfig     `config:"tls"`
	NetworkConfig config.NetworkConfig `config:"network"`
}

type Handler

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

Handler is the object of http handler

func (Handler) DecodeJSON

func (handler Handler) DecodeJSON(r *http.Request, o interface{}) error

func (Handler) EncodeJSON

func (handler Handler) EncodeJSON(v interface{}) (b []byte, err error)

EncodeJSON encode the object to json string

func (Handler) Error

func (handler Handler) Error(w http.ResponseWriter, err error)

Error output custom error

func (Handler) Error404

func (handler Handler) Error404(w http.ResponseWriter)

Error404 output 404 response

func (Handler) Error500

func (handler Handler) Error500(w http.ResponseWriter, msg string)

Error500 output 500 response

func (Handler) Flush

func (handler Handler) Flush(w http.ResponseWriter)

Flush flush response message

func (Handler) Get

func (handler Handler) Get(req *http.Request, key string, defaultValue string) string

Get http parameter or return default value

func (Handler) GetHeader

func (handler Handler) GetHeader(req *http.Request, key string, defaultValue string) string

GetHeader return specify http header or return default value if not set

func (Handler) GetIntOrDefault

func (handler Handler) GetIntOrDefault(r *http.Request, key string, defaultValue int) int

GetIntOrDefault return parameter or default, data type is int

func (Handler) GetJSON

func (handler Handler) GetJSON(r *http.Request) (*jsonq.JsonQuery, error)

GetJSON return json input

func (Handler) GetParameter

func (handler Handler) GetParameter(r *http.Request, key string) string

GetParameter return query parameter with argument name

func (Handler) GetParameterOrDefault

func (handler Handler) GetParameterOrDefault(r *http.Request, key string, defaultValue string) string

GetParameterOrDefault return query parameter or return default value

func (Handler) GetRawBody

func (handler Handler) GetRawBody(r *http.Request) ([]byte, error)

GetRawBody return raw http request body

func (Handler) Write

func (handler Handler) Write(w http.ResponseWriter, b []byte) (int, error)

Write response to client

func (Handler) WriteAckJSON

func (handler Handler) WriteAckJSON(w http.ResponseWriter, ack bool) error

func (Handler) WriteHeader

func (handler Handler) WriteHeader(w http.ResponseWriter, code int)

WriteHeader write status code to http header

func (Handler) WriteJSON

func (handler Handler) WriteJSON(w http.ResponseWriter, v interface{}, statusCode int) error

WriteJSON output signal result with json format

func (Handler) WriteJSONHeader

func (handler Handler) WriteJSONHeader(w http.ResponseWriter)

WriteJSONHeader will write standard json header

func (Handler) WriteJSONListResult

func (handler Handler) WriteJSONListResult(w http.ResponseWriter, total int, v interface{}, statusCode int) error

WriteJSONListResult output result list to json format

type Method

type Method string

Method is object of http method

const (
	// GET is http get method
	GET Method = "GET"
	// POST is http post method
	POST Method = "POST"
	// PUT is http put method
	PUT Method = "PUT"
	// DELETE is http delete method
	DELETE Method = "DELETE"
	// HEAD is http head method
	HEAD Method = "HEAD"

	OPTIONS Method = "OPTIONS"
)

func (Method) String

func (method Method) String() string

String return http method as string

type Result

type Result struct {
	Total  int         `json:"total"`
	Result interface{} `json:"result"`
}

Result is a general json result

Directories

Path Synopsis
Package httprouter is a trie based high performance HTTP request router.
Package httprouter is a trie based high performance HTTP request router.

Jump to

Keyboard shortcuts

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