jumper

package module
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 18 Imported by: 1

README

JUMPER

Github go.mod Github Release Github Pre-Release

Jumper is Go Module to help Developer handling HTTP Request & Response.

go get git.verzth.work/go/jumper/v2
Usage
Request Parser
func SomeHandler(w http.ResponseWriter, r *http.Request) {
    var req = jumper.PlugRequest(r, w) // Request Parser

    if req.HasHeader("X-Custom") {
        // Check whether 'X-Custom' header exist without check the value
    }
    if req.HeaderFilled("X-Custom") {
        // Check whether 'X-Custom' header exist and filled
    }
    customHeader := req.Header("X-Custom") // Get header value

    // http://localhost/service/{id:[0-9]+}/{segment...}
    id := req.GetSegment("id") // Named segment from mux router
    id := req.GetSegmentUint64("id") // Named segment from mux router
    id := req.GetSegmentUint32("id") // Named segment from mux router
    id := req.GetSegmentUint("id") // Named segment from mux router
    id := req.GetSegmentInt64("id") // Named segment from mux router
    id := req.GetSegmentInt32("id") // Named segment from mux router
    id := req.GetSegmentInt("id") // Named segment from mux router

    if req.Has("name") {
        // Check whether 'name' exist without check the value
    }
    if req.Filled("name") {
        // Check whether 'name' exist and filled
    }

    // Add Ptr to get the Pointer
    someVr := req.Get("key") // Get key value as interface
    name := req.GetString("name") // Get name value as string
    id := req.GetUint64("id") // Get id value as uint64
    id := req.GetUint32("id") // Get id value as uint32
    id := req.GetUint("id") // Get id value as uint
    id := req.GetInt64("id") // Get id value as int64
    id := req.GetInt32("id") // Get id value as int32
    id := req.GetInt("id") // Get id value as int
    price := req.GetFloat64("price") // Get price value as float64
    price := req.GetFloat32("price") // Get price value as float32
    status := req.GetBool("active") // Get active value as bool
    birthdate, err := req.GetTime("birthdate") // Get birthdate value as *time.Time with Error handler
    birthdate := req.GetTimeNE("birthdate") // Get birthdate value as *time.Time with No Error
    ids := req.GetArray("ids") // Get ids value as Array of interface{}
    ids := req.GetArrayUniquify("ids") // Get ids value as Array of interface{} and uniquify if possible
    obj := req.GetMap("object") // Get object value as Map of map[string]interface{}
    obj := req.GetStruct("object") // Get object value as struct of interface{}
    json := req.GetJSON("jsonstring") // Get jsonstring value as jumper.JSON
    file := req.GetFile("file") // Get file value as jumper.File
    files := req.GetFiles("files") // Get files value as Array of jumper.File
}
Response Writer

Response Failed sample:

{
  "status": 0,
  "status_number": "1000001",
  "status_code": "ABCDEF",
  "status_message": "Error occurred",
  "data": null
}

Response Success sample:

{
  "status": 1,
  "status_number": "F000002",
  "status_code": "SSSSSS",
  "status_message": "Success",
  "data": {
    "id": 1,
    "name": "json"
  }
}

Plug Response Writer

package mypackage

import (
    // SOME PACKAGES
	"git.verzth.work/go/jumper/v2"
    // SOME PACKAGES
)

func SomeHandler(w http.ResponseWriter, r *http.Request) {
    var res = jumper.PlugResponse(w) // Response Writer
    var data interface{}

    res.SetHttpCode(200) // Set HTTP Response Code. HTTP/1.1 standard (RFC 7231)

    res.Reply(0, "1000001", "ABCDEF", "Error Occurred")
    res.Reply(1, "F000002", "SSSSSS", "Success", data)
    res.ReplyFailed("1000001", "ABCDEF", "Error Occurred")
    res.ReplySuccess("F000002", "SSSSSS", "Success", data)
}

Demo Link

http://localhost:9999/?list={"obj":{"id":[1,2,3]}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseOf

func ParseOf[T any](r *Request, en *T) error

func ParseTo

func ParseTo[T any](r *Request) (T, error)

Types

type File

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

func (*File) GetFile

func (f *File) GetFile() multipart.File

func (*File) GetFileHeader

func (f *File) GetFileHeader() *multipart.FileHeader

func (*File) Name

func (f *File) Name() string

func (*File) Store

func (f *File) Store(path string, pattern string, perm os.FileMode) (string, error)

func (*File) StoreAs

func (f *File) StoreAs(path string, name string, perm os.FileMode) error

type JSON

type JSON []byte

func (JSON) Equals

func (j JSON) Equals(j1 JSON) bool

func (JSON) IsNull

func (j JSON) IsNull() bool

func (JSON) MarshalJSON

func (j JSON) MarshalJSON() ([]byte, error)

func (*JSON) Scan

func (j *JSON) Scan(value interface{}) error

func (JSON) String

func (j JSON) String() string

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(data []byte) error

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

type Jumper

type Jumper struct {
	Request
	Response
}

type Number added in v2.4.0

type Number float64

func (Number) Float64 added in v2.4.0

func (n Number) Float64() float64

func (Number) Int64 added in v2.4.0

func (n Number) Int64() int64

func (Number) String added in v2.4.0

func (n Number) String() string

type Params

type Params map[string]interface{}

type Request added in v2.0.1

type Request struct {
	Method     string
	ClientIP   string
	ClientPort string
	// contains filtered or unexported fields
}

func PlugRequest

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

func TouchRequest

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

TouchRequest touch request with rewrite to reader, so handler can reuse the reader.

func (*Request) Append added in v2.0.1

func (r *Request) Append(key string, val string)

func (*Request) Filled added in v2.0.1

func (r *Request) Filled(keys ...string) (found bool)

func (*Request) Get added in v2.0.1

func (r *Request) Get(key string) interface{}

func (*Request) GetAll added in v2.0.1

func (r *Request) GetAll() map[string]interface{}

func (*Request) GetArray added in v2.0.1

func (r *Request) GetArray(key string) []interface{}

func (*Request) GetArrayUniquify added in v2.0.1

func (r *Request) GetArrayUniquify(key string) []interface{}

func (*Request) GetBool added in v2.0.1

func (r *Request) GetBool(key string) bool

func (*Request) GetBoolPtr added in v2.0.1

func (r *Request) GetBoolPtr(key string) *bool

func (*Request) GetFile added in v2.0.1

func (r *Request) GetFile(key string) (*File, error)

func (*Request) GetFiles added in v2.0.1

func (r *Request) GetFiles(key string) ([]*File, error)

func (*Request) GetFloat added in v2.0.1

func (r *Request) GetFloat(key string) float32

func (*Request) GetFloat32Ptr added in v2.0.1

func (r *Request) GetFloat32Ptr(key string) *float32

func (*Request) GetFloat64 added in v2.0.1

func (r *Request) GetFloat64(key string) float64

func (*Request) GetFloat64Ptr added in v2.0.1

func (r *Request) GetFloat64Ptr(key string) *float64

func (*Request) GetFragment added in v2.0.1

func (r *Request) GetFragment() string

func (*Request) GetFullUrl added in v2.0.1

func (r *Request) GetFullUrl() string

func (*Request) GetHost added in v2.0.1

func (r *Request) GetHost() string

func (*Request) GetInt added in v2.0.1

func (r *Request) GetInt(key string) int

func (*Request) GetInt32 added in v2.0.1

func (r *Request) GetInt32(key string) int32

func (*Request) GetInt32Ptr added in v2.0.1

func (r *Request) GetInt32Ptr(key string) *int32

func (*Request) GetInt64 added in v2.0.1

func (r *Request) GetInt64(key string) int64

func (*Request) GetInt64Ptr added in v2.0.1

func (r *Request) GetInt64Ptr(key string) *int64

func (*Request) GetIntPtr added in v2.0.1

func (r *Request) GetIntPtr(key string) *int

func (*Request) GetJSON added in v2.0.1

func (r *Request) GetJSON(key string) JSON

func (*Request) GetMap added in v2.0.1

func (r *Request) GetMap(key string) map[string]interface{}

func (*Request) GetOpaque added in v2.0.1

func (r *Request) GetOpaque() string

func (*Request) GetPassword added in v2.0.1

func (r *Request) GetPassword() string

func (*Request) GetPath added in v2.0.1

func (r *Request) GetPath() string

func (*Request) GetPort added in v2.0.1

func (r *Request) GetPort() string

func (*Request) GetPtr added in v2.0.1

func (r *Request) GetPtr(key string) *interface{}

func (*Request) GetRawPath added in v2.0.1

func (r *Request) GetRawPath() string

func (*Request) GetRawQuery added in v2.0.1

func (r *Request) GetRawQuery() string

func (*Request) GetScheme added in v2.0.1

func (r *Request) GetScheme() string

func (*Request) GetSegment added in v2.0.1

func (r *Request) GetSegment(key string) string

func (*Request) GetSegmentInt added in v2.0.1

func (r *Request) GetSegmentInt(key string) int

func (*Request) GetSegmentInt32 added in v2.0.1

func (r *Request) GetSegmentInt32(key string) int32

func (*Request) GetSegmentInt64 added in v2.0.1

func (r *Request) GetSegmentInt64(key string) int64

func (*Request) GetSegmentUint added in v2.0.1

func (r *Request) GetSegmentUint(key string) uint

func (*Request) GetSegmentUint32 added in v2.0.1

func (r *Request) GetSegmentUint32(key string) uint32

func (*Request) GetSegmentUint64 added in v2.0.1

func (r *Request) GetSegmentUint64(key string) uint64

func (*Request) GetString added in v2.0.1

func (r *Request) GetString(key string) string

func (*Request) GetStringPtr added in v2.0.1

func (r *Request) GetStringPtr(key string) *string

func (*Request) GetStruct added in v2.0.1

func (r *Request) GetStruct(obj interface{}) error

func (*Request) GetTime added in v2.0.1

func (r *Request) GetTime(key string) (*time.Time, error)

func (*Request) GetTimeNE added in v2.0.1

func (r *Request) GetTimeNE(key string) *time.Time

func (*Request) GetUint added in v2.0.1

func (r *Request) GetUint(key string) uint

func (*Request) GetUint32 added in v2.0.1

func (r *Request) GetUint32(key string) uint32

func (*Request) GetUint32Ptr added in v2.0.1

func (r *Request) GetUint32Ptr(key string) *uint32

func (*Request) GetUint64 added in v2.0.1

func (r *Request) GetUint64(key string) uint64

func (*Request) GetUint64Ptr added in v2.0.1

func (r *Request) GetUint64Ptr(key string) *uint64

func (*Request) GetUintPtr added in v2.0.1

func (r *Request) GetUintPtr(key string) *uint

func (*Request) GetUrl added in v2.0.1

func (r *Request) GetUrl() string

func (*Request) GetUsername added in v2.0.1

func (r *Request) GetUsername() string

func (*Request) Has added in v2.0.1

func (r *Request) Has(keys ...string) (found bool)

func (*Request) HasFile added in v2.0.1

func (r *Request) HasFile(keys ...string) (found bool)

func (*Request) HasHeader added in v2.0.1

func (r *Request) HasHeader(keys ...string) (found bool)

func (*Request) HasUser added in v2.0.1

func (r *Request) HasUser() bool

func (*Request) Header added in v2.0.1

func (r *Request) Header(key string) string

func (*Request) HeaderFilled added in v2.0.1

func (r *Request) HeaderFilled(keys ...string) (found bool)

type Response

type Response interface {
	SetHttpCode(code int) Response
	ReplyAs(res Response) error
	Reply(status int, number string, code string, message string, data ...any) error
	ReplyFailed(number string, code string, message string, data ...any) error
	ReplySuccess(number string, code string, message string, data ...any) error
	ReplyCustom(httpStatusCode int, res any) error
	HttpStatusCode() int
	SetHttpStatusCode(httpStatusCode int) Response
	GetStatus() int
	GetStatusNumber() string
	GetStatusCode() string
	GetStatusMessage() string
	GetData() any
}

func NewResponse added in v2.2.0

func NewResponse(httpStatusCode int, Status int, StatusNumber string, StatusCode string, StatusMessage string, Data ...any) Response

func PlugResponse

func PlugResponse(w http.ResponseWriter) Response

type ResponseX added in v2.2.0

type ResponseX struct {
	Status        int    `json:"status"`
	StatusNumber  string `json:"status_number"`
	StatusCode    string `json:"status_code"`
	StatusMessage string `json:"status_message"`
	Data          any    `json:"data"`
	// contains filtered or unexported fields
}

func (*ResponseX) GetData added in v2.2.0

func (r *ResponseX) GetData() any

func (*ResponseX) GetStatus added in v2.2.0

func (r *ResponseX) GetStatus() int

func (*ResponseX) GetStatusCode added in v2.2.0

func (r *ResponseX) GetStatusCode() string

func (*ResponseX) GetStatusMessage added in v2.2.0

func (r *ResponseX) GetStatusMessage() string

func (*ResponseX) GetStatusNumber added in v2.2.0

func (r *ResponseX) GetStatusNumber() string

func (*ResponseX) HttpStatusCode added in v2.2.0

func (r *ResponseX) HttpStatusCode() int

func (*ResponseX) Reply added in v2.2.0

func (r *ResponseX) Reply(status int, number string, code string, message string, data ...any) error

Reply 'data' arguments only used on index 0 */

func (*ResponseX) ReplyAs added in v2.2.0

func (r *ResponseX) ReplyAs(res Response) error

func (*ResponseX) ReplyCustom added in v2.2.3

func (r *ResponseX) ReplyCustom(httpStatusCode int, res any) error

func (*ResponseX) ReplyFailed added in v2.2.0

func (r *ResponseX) ReplyFailed(number string, code string, message string, data ...any) error

ReplyFailed 'data' arguments only used on index 0 */

func (*ResponseX) ReplySuccess added in v2.2.0

func (r *ResponseX) ReplySuccess(number string, code string, message string, data ...any) error

ReplySuccess 'data' arguments only used on index 0 */

func (*ResponseX) SetHttpCode added in v2.2.0

func (r *ResponseX) SetHttpCode(code int) Response

func (*ResponseX) SetHttpStatusCode added in v2.2.0

func (r *ResponseX) SetHttpStatusCode(httpStatusCode int) Response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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