Documentation ¶
Overview ¶
The oryx http package, the response parse service.
The oryx http package provides standard request and response in json.
Error, when error, use this handler. CplxError, for complex error, use this handler. Data, when no error, use this handler. SystemError, application level error code. SetHeader, for direclty response the raw stream.
The standard server response:
code, an int error code. data, specifies the data.
The api for simple api:
WriteVersion, to directly response the version. WriteData, to directly write the data in json. WriteError, to directly write the error. WriteCplxError, to directly write the complex error.
The global variables:
oh.Server, to set the response header["Server"].
Index ¶
- Constants
- Variables
- func ApiRequest(url string) (code int, body []byte, err error)
- func CplxError(ctx ol.Context, code SystemError, message string) http.Handler
- func Data(ctx ol.Context, v interface{}) http.Handler
- func Error(ctx ol.Context, err error) http.Handler
- func SetHeader(w http.ResponseWriter)
- func Success(ctx ol.Context, w http.ResponseWriter, r *http.Request)
- func WriteCplxError(ctx ol.Context, w http.ResponseWriter, r *http.Request, code SystemError, ...)
- func WriteData(ctx ol.Context, w http.ResponseWriter, r *http.Request, v interface{})
- func WriteError(ctx ol.Context, w http.ResponseWriter, r *http.Request, err error)
- func WriteVersion(w http.ResponseWriter, r *http.Request, version string)
- type AppError
- type SystemComplexError
- type SystemError
Examples ¶
Constants ¶
const ( HttpJson = "application/json" HttpJavaScript = "application/javascript" )
header["Content-Type"] in response.
Variables ¶
var FilterAppError = func(ctx ol.Context, w http.ResponseWriter, r *http.Request, err AppError) interface{} { ol.Ef(ctx, "Serve %v failed, err is %+v", r.URL, err) return map[string]interface{}{"code": err.Code(), "data": err.Error()} }
var FilterCplxSystemError = func(ctx ol.Context, w http.ResponseWriter, r *http.Request, o SystemComplexError) interface{} { ol.Ef(ctx, "Serve %v failed, err is %+v", r.URL, o) return o }
for hijack to define the response structure. user can redefine these functions for special response.
var FilterData = func(ctx ol.Context, w http.ResponseWriter, r *http.Request, o interface{}) interface{} { rv := map[string]interface{}{ "code": 0, "server": os.Getpid(), "data": o, } if v, ok := o.(string); ok { rv["data"] = v } return rv }
var FilterError = func(ctx ol.Context, w http.ResponseWriter, r *http.Request, err error) string { ol.Ef(ctx, "Serve %v failed, err is %+v", r.URL, err) return err.Error() }
var FilterSystemError = func(ctx ol.Context, w http.ResponseWriter, r *http.Request, o SystemError) interface{} { ol.Ef(ctx, "Serve %v failed, err is %+v", r.URL, o) return map[string]int{"code": int(o)} }
var Server = "Oryx"
header["Server"] in response.
Functions ¶
func ApiRequest ¶
Read http api by HTTP GET and parse the code/data.
Example ¶
package main import ( oh "github.com/ossrs/go-oryx-lib/http" ) func main() { var err error var body []byte if _, body, err = oh.ApiRequest("http://127.0.0.1985/api/v1/versions"); err != nil { return } // user can use the body to parse to specified struct. _ = body }
Output:
func CplxError ¶
Wrapper for complex error use Error(ctx, SystemComplexError{}) @remark user can use WriteCplxError() for simple api.
func Data ¶
http normal response. @remark user can use nil v to response success, which data is null. @remark user can use WriteData() for simple api.
func Error ¶
http standard error response. @remark for not SystemError, we will use logger.E to print it. @remark user can use WriteError() for simple api.
func SetHeader ¶
func SetHeader(w http.ResponseWriter)
set http header, for directly use the w, for example, user want to directly write raw text.
func WriteCplxError ¶
func WriteCplxError(ctx ol.Context, w http.ResponseWriter, r *http.Request, code SystemError, message string)
Directly write complex error, a wrappter for CplxError(). @remark user can use CplxError() for group of complex apis.
func WriteData ¶
Directly write json data, a wrapper for Data(). @remark user can use Data() for group of complex apis.
func WriteError ¶
Directly write error, a wrapper for Error(). @remark user can use Error() for group of complex apis.
func WriteVersion ¶
func WriteVersion(w http.ResponseWriter, r *http.Request, version string)
response the standard version info:
{code, server, data} where server is the server pid, and data is below object: {major, minor, revision, extra, version, signature}
@param version in {major.minor.revision-extra}, where -extra is optional,
for example: 1.0.0 or 1.0.0-0 or 1.0.0-1
Example ¶
package main import ( oh "github.com/ossrs/go-oryx-lib/http" "net/http" ) func main() { http.HandleFunc("/api/v1/version", func(w http.ResponseWriter, r *http.Request) { // version is major.minor.revisoin-extra oh.WriteVersion(w, r, "1.2.3-4") }) }
Output:
Types ¶
type SystemComplexError ¶
type SystemComplexError struct { // the system error code. Code SystemError `json:"code"` // the description for this error. Message string `json:"data"` }
system conplex error.
func (SystemComplexError) Error ¶
func (v SystemComplexError) Error() string
type SystemError ¶
type SystemError int
system int error.
func (SystemError) Error ¶
func (v SystemError) Error() string