http

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: MIT Imports: 8 Imported by: 40

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

Examples

Constants

View Source
const (
	HttpJson       = "application/json"
	HttpJavaScript = "application/javascript"
)

header["Content-Type"] in response.

Variables

View Source
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()}
}
View Source
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.

View Source
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
}
View Source
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()
}
View Source
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)}
}
View Source
var Server = "Oryx"

header["Server"] in response.

Functions

func ApiRequest

func ApiRequest(url string) (code int, body []byte, err error)

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

func CplxError(ctx ol.Context, code SystemError, message string) http.Handler

Wrapper for complex error use Error(ctx, SystemComplexError{}) @remark user can use WriteCplxError() for simple api.

func Data

func Data(ctx ol.Context, v interface{}) http.Handler

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

func Error(ctx ol.Context, err error) http.Handler

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 Success

func Success(ctx ol.Context, w http.ResponseWriter, r *http.Request)

Directly write success json response, same to WriteData(ctx, w, r, nil).

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

func WriteData(ctx ol.Context, w http.ResponseWriter, r *http.Request, v interface{})

Directly write json data, a wrapper for Data(). @remark user can use Data() for group of complex apis.

func WriteError

func WriteError(ctx ol.Context, w http.ResponseWriter, r *http.Request, err error)

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 AppError added in v0.0.3

type AppError interface {
	Code() int
	error
}

application level, with code.

type HTTPStatus added in v0.0.8

type HTTPStatus interface {
	Status() int
}

HTTP Status Code

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

Jump to

Keyboard shortcuts

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