README
JUMPER
Jumper is Go Module to help Developer handling HTTP Request & Response.
go get git.teknoku.digital/teknoku/jumper
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
}
name := req.Get("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.GetFloat("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.teknoku.digital/teknoku/jumper"
// 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 ¶
- type File
- type JSON
- type Jumper
- type Params
- type Request
- func (r *Request) Append(key string, val string)
- func (r *Request) Filled(keys ...string) (found bool)
- func (r *Request) Get(key string) string
- func (r *Request) GetAll() map[string]interface{}
- func (r *Request) GetArray(key string) []interface{}
- func (r *Request) GetArrayUniquify(key string) []interface{}
- func (r *Request) GetBool(key string) bool
- func (r *Request) GetFile(key string) (*File, error)
- func (r *Request) GetFiles(key string) ([]*File, error)
- func (r *Request) GetFloat(key string) float32
- func (r *Request) GetFloat64(key string) float64
- func (r *Request) GetFragment() string
- func (r *Request) GetFullUrl() string
- func (r *Request) GetHost() string
- func (r *Request) GetInt(key string) int
- func (r *Request) GetInt32(key string) int32
- func (r *Request) GetInt64(key string) int64
- func (r *Request) GetJSON(key string) JSON
- func (r *Request) GetMap(key string) map[string]interface{}
- func (r *Request) GetOpaque() string
- func (r *Request) GetPassword() string
- func (r *Request) GetPath() string
- func (r *Request) GetPort() string
- func (r *Request) GetRawPath() string
- func (r *Request) GetRawQuery() string
- func (r *Request) GetScheme() string
- func (r *Request) GetSegment(key string) string
- func (r *Request) GetSegmentInt(key string) int
- func (r *Request) GetSegmentInt32(key string) int32
- func (r *Request) GetSegmentInt64(key string) int64
- func (r *Request) GetSegmentUint(key string) uint
- func (r *Request) GetSegmentUint32(key string) uint32
- func (r *Request) GetSegmentUint64(key string) uint64
- func (r *Request) GetStruct(obj interface{}) error
- func (r *Request) GetTime(key string) (*time.Time, error)
- func (r *Request) GetTimeNE(key string) *time.Time
- func (r *Request) GetUint(key string) uint
- func (r *Request) GetUint32(key string) uint32
- func (r *Request) GetUint64(key string) uint64
- func (r *Request) GetUrl() string
- func (r *Request) GetUsername() string
- func (r *Request) Has(keys ...string) (found bool)
- func (r *Request) HasFile(keys ...string) (found bool)
- func (r *Request) HasHeader(keys ...string) (found bool)
- func (r *Request) HasUser() bool
- func (r *Request) Header(key string) string
- func (r *Request) HeaderFilled(keys ...string) (found bool)
- type Response
- func (r *Response) Reply(status int, number string, code string, message string, data ...interface{}) error
- func (r *Response) ReplyFailed(number string, code string, message string, data ...interface{}) error
- func (r *Response) ReplySuccess(number string, code string, message string, data ...interface{}) error
- func (r *Response) SetHttpCode(code int) *Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
func (*File) GetFileHeader ¶
func (f *File) GetFileHeader() *multipart.FileHeader
type Request ¶
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
func (*Request) GetArrayUniquify ¶
func (*Request) GetFloat64 ¶
func (*Request) GetFragment ¶
func (*Request) GetFullUrl ¶
func (*Request) GetPassword ¶
func (*Request) GetRawPath ¶
func (*Request) GetRawQuery ¶
func (*Request) GetSegment ¶
func (*Request) GetSegmentInt ¶
func (*Request) GetSegmentInt32 ¶
func (*Request) GetSegmentInt64 ¶
func (*Request) GetSegmentUint ¶
func (*Request) GetSegmentUint32 ¶
func (*Request) GetSegmentUint64 ¶
func (*Request) GetUsername ¶
func (*Request) HeaderFilled ¶
type Response ¶
type Response struct { Status int `json:"status"` StatusNumber string `json:"status_number"` StatusCode string `json:"status_code"` StatusMessage string `json:"status_message"` Data interface{} `json:"data"` // contains filtered or unexported fields }
func PlugResponse ¶
func PlugResponse(w http.ResponseWriter) *Response
func (*Response) Reply ¶
func (r *Response) Reply(status int, number string, code string, message string, data ...interface{}) error
'data' arguments only used on index 0
func (*Response) ReplyFailed ¶
func (*Response) ReplySuccess ¶
func (*Response) SetHttpCode ¶
Directories
Path | Synopsis |
---|---|