api

package
v2.4.4+incompatible Latest Latest
Warning

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

Go to latest
Published: May 11, 2018 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(r *http.Request, name string, value bool) bool

GetBool search the boolean argument by given name from the URL and PostForm, if not exists, return the given value as default.

func GetInt

func GetInt(r *http.Request, name string, value int) int

GetInt search the integer argument by given name from the URL and PostForm, if not exists, return the given value as default.

func GetString

func GetString(r *http.Request, name string, value string) string

GetString search the string argument by given name from the URL and PostForm, if not exists, return the given value as default.

func Return

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

Return write response to w, code is the http code, data is the content will write into responseBody, can be string or []byte.

Types

type API

type API interface {
	// decode the data
	Decode([]byte) error

	// encode the data
	Encode() ([]byte, error)

	// the http route uri for this api
	URI() string

	// which watcher want to use
	WatcherName() string

	// the key used to watch,
	// eg. return '/lain/config/vip' for coreinfoWatcher,
	//     return 'console' as appname for coreifnoWacher and podgroupWatcher
	Key(r *http.Request) (string, error)

	// create new api by data
	// return a new instance of API with real data in it
	// second boolean value represent if the data really having some changes
	Make(data map[string]interface{}) (API, bool, error)
}

API is a common interface which a api instance must realize

type BanWatcher

type BanWatcher interface {
	BanWatch()
}

BanWatcher is a interface to ban watch feature. you can realize this interface if your do not want your api support watch request.

type EventData

type EventData interface {
	Encode() ([]byte, error)
	Decode(content []byte) error
}

EventData is a type which can be send by SendEvent(), SendEvent() will call Encode() and then write result into connection.

type EventSource

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

EventSource is a component which can hajick http connection and then send event to client.

func NewEventSource

func NewEventSource(w http.ResponseWriter) (*EventSource, error)

NewEventSource create a new event source, event source will hijack w.conn; so you can not write anything to this responseWriter after calling this.

func (*EventSource) Close

func (es *EventSource) Close()

Close the event source, this function also close the http connection.

func (*EventSource) CloseNotify

func (es *EventSource) CloseNotify() <-chan bool

CloseNotify return a channel used to notify the connection's close event. the return channel will be closed when connection was closed, nothing send into it.

func (*EventSource) SendEvent

func (es *EventSource) SendEvent(id uint64, event string, data interface{}) error

SendEvent send a event to client. data can be []byte, string or EventData. it should not contain '\n' in it, otherwize only the first line will be send. the given data can be string or []byte or EventData

type Server

type Server struct {
	*martini.Martini
	martini.Router
}

Server used by lainlet, based on martini

func New

func New(ip, version string, watchers map[string]watcher.Watcher) (*Server, error)

New create a http api server; ip is the server ip, it was used by some query; version is the lainlet version, used to return by `/version` api; st is the backend store, it must be valided, should not be a empty interface or a nil value. This function return error when fail to initialize some backend watcher.

func (*Server) Register

func (s *Server) Register(api API)

Register a new api. apiserve will auto create a handler for it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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