id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
listlengths 21
1.41k
| docstring
stringlengths 6
2.61k
| docstring_tokens
listlengths 3
215
| sha
stringlengths 40
40
| url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|---|
18,300 |
yosida95/golang-jenkins
|
jenkins.go
|
CreateView
|
func (jenkins *Jenkins) CreateView(listView ListView) error {
xmlListView, _ := xml.Marshal(listView)
reader := bytes.NewReader(xmlListView)
params := url.Values{"name": []string{listView.Name}}
return jenkins.postXml("/createView", params, reader, nil)
}
|
go
|
func (jenkins *Jenkins) CreateView(listView ListView) error {
xmlListView, _ := xml.Marshal(listView)
reader := bytes.NewReader(xmlListView)
params := url.Values{"name": []string{listView.Name}}
return jenkins.postXml("/createView", params, reader, nil)
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"CreateView",
"(",
"listView",
"ListView",
")",
"error",
"{",
"xmlListView",
",",
"_",
":=",
"xml",
".",
"Marshal",
"(",
"listView",
")",
"\n",
"reader",
":=",
"bytes",
".",
"NewReader",
"(",
"xmlListView",
")",
"\n",
"params",
":=",
"url",
".",
"Values",
"{",
"\"",
"\"",
":",
"[",
"]",
"string",
"{",
"listView",
".",
"Name",
"}",
"}",
"\n\n",
"return",
"jenkins",
".",
"postXml",
"(",
"\"",
"\"",
",",
"params",
",",
"reader",
",",
"nil",
")",
"\n",
"}"
] |
// Create a new view
|
[
"Create",
"a",
"new",
"view"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L256-L262
|
18,301 |
yosida95/golang-jenkins
|
jenkins.go
|
Build
|
func (jenkins *Jenkins) Build(job Job, params url.Values) error {
if hasParams(job) {
return jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
} else {
return jenkins.post(fmt.Sprintf("/job/%s/build", job.Name), params, nil)
}
}
|
go
|
func (jenkins *Jenkins) Build(job Job, params url.Values) error {
if hasParams(job) {
return jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
} else {
return jenkins.post(fmt.Sprintf("/job/%s/build", job.Name), params, nil)
}
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"Build",
"(",
"job",
"Job",
",",
"params",
"url",
".",
"Values",
")",
"error",
"{",
"if",
"hasParams",
"(",
"job",
")",
"{",
"return",
"jenkins",
".",
"post",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"job",
".",
"Name",
")",
",",
"params",
",",
"nil",
")",
"\n",
"}",
"else",
"{",
"return",
"jenkins",
".",
"post",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"job",
".",
"Name",
")",
",",
"params",
",",
"nil",
")",
"\n",
"}",
"\n",
"}"
] |
// Create a new build for this job.
// Params can be nil.
|
[
"Create",
"a",
"new",
"build",
"for",
"this",
"job",
".",
"Params",
"can",
"be",
"nil",
"."
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L266-L272
|
18,302 |
yosida95/golang-jenkins
|
jenkins.go
|
GetBuildConsoleOutput
|
func (jenkins *Jenkins) GetBuildConsoleOutput(build Build) ([]byte, error) {
requestUrl := fmt.Sprintf("%s/consoleText", build.Url)
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return nil, err
}
res, err := jenkins.sendRequest(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
|
go
|
func (jenkins *Jenkins) GetBuildConsoleOutput(build Build) ([]byte, error) {
requestUrl := fmt.Sprintf("%s/consoleText", build.Url)
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return nil, err
}
res, err := jenkins.sendRequest(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"GetBuildConsoleOutput",
"(",
"build",
"Build",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"requestUrl",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"build",
".",
"Url",
")",
"\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"requestUrl",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"jenkins",
".",
"sendRequest",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"defer",
"res",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"return",
"ioutil",
".",
"ReadAll",
"(",
"res",
".",
"Body",
")",
"\n",
"}"
] |
// Get the console output from a build.
|
[
"Get",
"the",
"console",
"output",
"from",
"a",
"build",
"."
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L275-L289
|
18,303 |
yosida95/golang-jenkins
|
jenkins.go
|
GetQueue
|
func (jenkins *Jenkins) GetQueue() (queue Queue, err error) {
err = jenkins.get(fmt.Sprintf("/queue"), nil, &queue)
return
}
|
go
|
func (jenkins *Jenkins) GetQueue() (queue Queue, err error) {
err = jenkins.get(fmt.Sprintf("/queue"), nil, &queue)
return
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"GetQueue",
"(",
")",
"(",
"queue",
"Queue",
",",
"err",
"error",
")",
"{",
"err",
"=",
"jenkins",
".",
"get",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
")",
",",
"nil",
",",
"&",
"queue",
")",
"\n",
"return",
"\n",
"}"
] |
// GetQueue returns the current build queue from Jenkins
|
[
"GetQueue",
"returns",
"the",
"current",
"build",
"queue",
"from",
"Jenkins"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L292-L295
|
18,304 |
yosida95/golang-jenkins
|
jenkins.go
|
SetBuildDescription
|
func (jenkins *Jenkins) SetBuildDescription(build Build, description string) error {
requestUrl := fmt.Sprintf("%ssubmitDescription?description=%s", build.Url, url.QueryEscape(description))
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return err
}
res, err := jenkins.sendRequest(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("Unexpected response: expected '200' but received '%d'", res.StatusCode)
}
return nil
}
|
go
|
func (jenkins *Jenkins) SetBuildDescription(build Build, description string) error {
requestUrl := fmt.Sprintf("%ssubmitDescription?description=%s", build.Url, url.QueryEscape(description))
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return err
}
res, err := jenkins.sendRequest(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("Unexpected response: expected '200' but received '%d'", res.StatusCode)
}
return nil
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"SetBuildDescription",
"(",
"build",
"Build",
",",
"description",
"string",
")",
"error",
"{",
"requestUrl",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"build",
".",
"Url",
",",
"url",
".",
"QueryEscape",
"(",
"description",
")",
")",
"\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"requestUrl",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"jenkins",
".",
"sendRequest",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"res",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"if",
"res",
".",
"StatusCode",
"!=",
"200",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"res",
".",
"StatusCode",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// SetBuildDescription sets the description of a build
|
[
"SetBuildDescription",
"sets",
"the",
"description",
"of",
"a",
"build"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L315-L333
|
18,305 |
yosida95/golang-jenkins
|
jenkins.go
|
GetComputerObject
|
func (jenkins *Jenkins) GetComputerObject() (co ComputerObject, err error) {
err = jenkins.get(fmt.Sprintf("/computer"), nil, &co)
return
}
|
go
|
func (jenkins *Jenkins) GetComputerObject() (co ComputerObject, err error) {
err = jenkins.get(fmt.Sprintf("/computer"), nil, &co)
return
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"GetComputerObject",
"(",
")",
"(",
"co",
"ComputerObject",
",",
"err",
"error",
")",
"{",
"err",
"=",
"jenkins",
".",
"get",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
")",
",",
"nil",
",",
"&",
"co",
")",
"\n",
"return",
"\n",
"}"
] |
// GetComputerObject returns the main ComputerObject
|
[
"GetComputerObject",
"returns",
"the",
"main",
"ComputerObject"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L336-L339
|
18,306 |
yosida95/golang-jenkins
|
jenkins.go
|
GetComputers
|
func (jenkins *Jenkins) GetComputers() ([]Computer, error) {
var payload = struct {
Computers []Computer `json:"computer"`
}{}
err := jenkins.get("/computer", nil, &payload)
return payload.Computers, err
}
|
go
|
func (jenkins *Jenkins) GetComputers() ([]Computer, error) {
var payload = struct {
Computers []Computer `json:"computer"`
}{}
err := jenkins.get("/computer", nil, &payload)
return payload.Computers, err
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"GetComputers",
"(",
")",
"(",
"[",
"]",
"Computer",
",",
"error",
")",
"{",
"var",
"payload",
"=",
"struct",
"{",
"Computers",
"[",
"]",
"Computer",
"`json:\"computer\"`",
"\n",
"}",
"{",
"}",
"\n",
"err",
":=",
"jenkins",
".",
"get",
"(",
"\"",
"\"",
",",
"nil",
",",
"&",
"payload",
")",
"\n",
"return",
"payload",
".",
"Computers",
",",
"err",
"\n",
"}"
] |
// GetComputers returns the list of all Computer objects
|
[
"GetComputers",
"returns",
"the",
"list",
"of",
"all",
"Computer",
"objects"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L342-L348
|
18,307 |
yosida95/golang-jenkins
|
jenkins.go
|
GetComputer
|
func (jenkins *Jenkins) GetComputer(name string) (computer Computer, err error) {
err = jenkins.get(fmt.Sprintf("/computer/%s", name), nil, &computer)
return
}
|
go
|
func (jenkins *Jenkins) GetComputer(name string) (computer Computer, err error) {
err = jenkins.get(fmt.Sprintf("/computer/%s", name), nil, &computer)
return
}
|
[
"func",
"(",
"jenkins",
"*",
"Jenkins",
")",
"GetComputer",
"(",
"name",
"string",
")",
"(",
"computer",
"Computer",
",",
"err",
"error",
")",
"{",
"err",
"=",
"jenkins",
".",
"get",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"name",
")",
",",
"nil",
",",
"&",
"computer",
")",
"\n",
"return",
"\n",
"}"
] |
// GetComputer returns a Computer object with a specified name.
|
[
"GetComputer",
"returns",
"a",
"Computer",
"object",
"with",
"a",
"specified",
"name",
"."
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L351-L354
|
18,308 |
yosida95/golang-jenkins
|
jenkins.go
|
hasParams
|
func hasParams(job Job) bool {
for _, action := range job.Actions {
if len(action.ParameterDefinitions) > 0 {
return true
}
}
return false
}
|
go
|
func hasParams(job Job) bool {
for _, action := range job.Actions {
if len(action.ParameterDefinitions) > 0 {
return true
}
}
return false
}
|
[
"func",
"hasParams",
"(",
"job",
"Job",
")",
"bool",
"{",
"for",
"_",
",",
"action",
":=",
"range",
"job",
".",
"Actions",
"{",
"if",
"len",
"(",
"action",
".",
"ParameterDefinitions",
")",
">",
"0",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// hasParams returns a boolean value indicating if the job is parameterized
|
[
"hasParams",
"returns",
"a",
"boolean",
"value",
"indicating",
"if",
"the",
"job",
"is",
"parameterized"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/jenkins.go#L357-L364
|
18,309 |
yosida95/golang-jenkins
|
job.go
|
UnmarshalXML
|
func (iscm *Scm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, v := range start.Attr {
if v.Name.Local == "class" {
iscm.Class = v.Value
} else if v.Name.Local == "plugin" {
iscm.Plugin = v.Value
}
}
switch iscm.Class {
case "hudson.scm.SubversionSCM":
iscm.ScmContent = &ScmSvn{}
err := d.DecodeElement(&iscm.ScmContent, &start)
if err != nil {
return err
}
case "hudson.plugins.git.GitSCM":
iscm.ScmContent = &ScmGit{}
err := d.DecodeElement(&iscm.ScmContent, &start)
if err != nil {
return err
}
}
return nil
}
|
go
|
func (iscm *Scm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, v := range start.Attr {
if v.Name.Local == "class" {
iscm.Class = v.Value
} else if v.Name.Local == "plugin" {
iscm.Plugin = v.Value
}
}
switch iscm.Class {
case "hudson.scm.SubversionSCM":
iscm.ScmContent = &ScmSvn{}
err := d.DecodeElement(&iscm.ScmContent, &start)
if err != nil {
return err
}
case "hudson.plugins.git.GitSCM":
iscm.ScmContent = &ScmGit{}
err := d.DecodeElement(&iscm.ScmContent, &start)
if err != nil {
return err
}
}
return nil
}
|
[
"func",
"(",
"iscm",
"*",
"Scm",
")",
"UnmarshalXML",
"(",
"d",
"*",
"xml",
".",
"Decoder",
",",
"start",
"xml",
".",
"StartElement",
")",
"error",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"start",
".",
"Attr",
"{",
"if",
"v",
".",
"Name",
".",
"Local",
"==",
"\"",
"\"",
"{",
"iscm",
".",
"Class",
"=",
"v",
".",
"Value",
"\n",
"}",
"else",
"if",
"v",
".",
"Name",
".",
"Local",
"==",
"\"",
"\"",
"{",
"iscm",
".",
"Plugin",
"=",
"v",
".",
"Value",
"\n",
"}",
"\n",
"}",
"\n",
"switch",
"iscm",
".",
"Class",
"{",
"case",
"\"",
"\"",
":",
"iscm",
".",
"ScmContent",
"=",
"&",
"ScmSvn",
"{",
"}",
"\n",
"err",
":=",
"d",
".",
"DecodeElement",
"(",
"&",
"iscm",
".",
"ScmContent",
",",
"&",
"start",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"case",
"\"",
"\"",
":",
"iscm",
".",
"ScmContent",
"=",
"&",
"ScmGit",
"{",
"}",
"\n",
"err",
":=",
"d",
".",
"DecodeElement",
"(",
"&",
"iscm",
".",
"ScmContent",
",",
"&",
"start",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
//UnmarshalXML implements xml.UnmarshalXML intrface
//Decode between multiple types of Scm. for now only SVN is supported
|
[
"UnmarshalXML",
"implements",
"xml",
".",
"UnmarshalXML",
"intrface",
"Decode",
"between",
"multiple",
"types",
"of",
"Scm",
".",
"for",
"now",
"only",
"SVN",
"is",
"supported"
] |
4772716c47ca769dea7f92bc28dc607eeb16d8a8
|
https://github.com/yosida95/golang-jenkins/blob/4772716c47ca769dea7f92bc28dc607eeb16d8a8/job.go#L258-L281
|
18,310 |
teambition/gear
|
response.go
|
Get
|
func (r *Response) Get(key string) string {
return r.handlerHeader.Get(key)
}
|
go
|
func (r *Response) Get(key string) string {
return r.handlerHeader.Get(key)
}
|
[
"func",
"(",
"r",
"*",
"Response",
")",
"Get",
"(",
"key",
"string",
")",
"string",
"{",
"return",
"r",
".",
"handlerHeader",
".",
"Get",
"(",
"key",
")",
"\n",
"}"
] |
// Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "". To access multiple values of a key, access the map directly with CanonicalHeaderKey.
|
[
"Get",
"gets",
"the",
"first",
"value",
"associated",
"with",
"the",
"given",
"key",
".",
"If",
"there",
"are",
"no",
"values",
"associated",
"with",
"the",
"key",
"Get",
"returns",
".",
"To",
"access",
"multiple",
"values",
"of",
"a",
"key",
"access",
"the",
"map",
"directly",
"with",
"CanonicalHeaderKey",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/response.go#L30-L32
|
18,311 |
teambition/gear
|
const.go
|
ErrByStatus
|
func ErrByStatus(status int) *Error {
switch status {
case 400:
return ErrBadRequest
case 401:
return ErrUnauthorized
case 402:
return ErrPaymentRequired
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 405:
return ErrMethodNotAllowed
case 406:
return ErrNotAcceptable
case 407:
return ErrProxyAuthRequired
case 408:
return ErrRequestTimeout
case 409:
return ErrConflict
case 410:
return ErrGone
case 411:
return ErrLengthRequired
case 412:
return ErrPreconditionFailed
case 413:
return ErrRequestEntityTooLarge
case 414:
return ErrRequestURITooLong
case 415:
return ErrUnsupportedMediaType
case 416:
return ErrRequestedRangeNotSatisfiable
case 417:
return ErrExpectationFailed
case 418:
return ErrTeapot
case 421:
return ErrMisdirectedRequest
case 422:
return ErrUnprocessableEntity
case 423:
return ErrLocked
case 424:
return ErrFailedDependency
case 426:
return ErrUpgradeRequired
case 428:
return ErrPreconditionRequired
case 429:
return ErrTooManyRequests
case 431:
return ErrRequestHeaderFieldsTooLarge
case 451:
return ErrUnavailableForLegalReasons
case 499:
return ErrClientClosedRequest
case 500:
return ErrInternalServerError
case 501:
return ErrNotImplemented
case 502:
return ErrBadGateway
case 503:
return ErrServiceUnavailable
case 504:
return ErrGatewayTimeout
case 505:
return ErrHTTPVersionNotSupported
case 506:
return ErrVariantAlsoNegotiates
case 507:
return ErrInsufficientStorage
case 508:
return ErrLoopDetected
case 510:
return ErrNotExtended
case 511:
return ErrNetworkAuthenticationRequired
default:
return Err.WithCode(status)
}
}
|
go
|
func ErrByStatus(status int) *Error {
switch status {
case 400:
return ErrBadRequest
case 401:
return ErrUnauthorized
case 402:
return ErrPaymentRequired
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 405:
return ErrMethodNotAllowed
case 406:
return ErrNotAcceptable
case 407:
return ErrProxyAuthRequired
case 408:
return ErrRequestTimeout
case 409:
return ErrConflict
case 410:
return ErrGone
case 411:
return ErrLengthRequired
case 412:
return ErrPreconditionFailed
case 413:
return ErrRequestEntityTooLarge
case 414:
return ErrRequestURITooLong
case 415:
return ErrUnsupportedMediaType
case 416:
return ErrRequestedRangeNotSatisfiable
case 417:
return ErrExpectationFailed
case 418:
return ErrTeapot
case 421:
return ErrMisdirectedRequest
case 422:
return ErrUnprocessableEntity
case 423:
return ErrLocked
case 424:
return ErrFailedDependency
case 426:
return ErrUpgradeRequired
case 428:
return ErrPreconditionRequired
case 429:
return ErrTooManyRequests
case 431:
return ErrRequestHeaderFieldsTooLarge
case 451:
return ErrUnavailableForLegalReasons
case 499:
return ErrClientClosedRequest
case 500:
return ErrInternalServerError
case 501:
return ErrNotImplemented
case 502:
return ErrBadGateway
case 503:
return ErrServiceUnavailable
case 504:
return ErrGatewayTimeout
case 505:
return ErrHTTPVersionNotSupported
case 506:
return ErrVariantAlsoNegotiates
case 507:
return ErrInsufficientStorage
case 508:
return ErrLoopDetected
case 510:
return ErrNotExtended
case 511:
return ErrNetworkAuthenticationRequired
default:
return Err.WithCode(status)
}
}
|
[
"func",
"ErrByStatus",
"(",
"status",
"int",
")",
"*",
"Error",
"{",
"switch",
"status",
"{",
"case",
"400",
":",
"return",
"ErrBadRequest",
"\n",
"case",
"401",
":",
"return",
"ErrUnauthorized",
"\n",
"case",
"402",
":",
"return",
"ErrPaymentRequired",
"\n",
"case",
"403",
":",
"return",
"ErrForbidden",
"\n",
"case",
"404",
":",
"return",
"ErrNotFound",
"\n",
"case",
"405",
":",
"return",
"ErrMethodNotAllowed",
"\n",
"case",
"406",
":",
"return",
"ErrNotAcceptable",
"\n",
"case",
"407",
":",
"return",
"ErrProxyAuthRequired",
"\n",
"case",
"408",
":",
"return",
"ErrRequestTimeout",
"\n",
"case",
"409",
":",
"return",
"ErrConflict",
"\n",
"case",
"410",
":",
"return",
"ErrGone",
"\n",
"case",
"411",
":",
"return",
"ErrLengthRequired",
"\n",
"case",
"412",
":",
"return",
"ErrPreconditionFailed",
"\n",
"case",
"413",
":",
"return",
"ErrRequestEntityTooLarge",
"\n",
"case",
"414",
":",
"return",
"ErrRequestURITooLong",
"\n",
"case",
"415",
":",
"return",
"ErrUnsupportedMediaType",
"\n",
"case",
"416",
":",
"return",
"ErrRequestedRangeNotSatisfiable",
"\n",
"case",
"417",
":",
"return",
"ErrExpectationFailed",
"\n",
"case",
"418",
":",
"return",
"ErrTeapot",
"\n",
"case",
"421",
":",
"return",
"ErrMisdirectedRequest",
"\n",
"case",
"422",
":",
"return",
"ErrUnprocessableEntity",
"\n",
"case",
"423",
":",
"return",
"ErrLocked",
"\n",
"case",
"424",
":",
"return",
"ErrFailedDependency",
"\n",
"case",
"426",
":",
"return",
"ErrUpgradeRequired",
"\n",
"case",
"428",
":",
"return",
"ErrPreconditionRequired",
"\n",
"case",
"429",
":",
"return",
"ErrTooManyRequests",
"\n",
"case",
"431",
":",
"return",
"ErrRequestHeaderFieldsTooLarge",
"\n",
"case",
"451",
":",
"return",
"ErrUnavailableForLegalReasons",
"\n",
"case",
"499",
":",
"return",
"ErrClientClosedRequest",
"\n",
"case",
"500",
":",
"return",
"ErrInternalServerError",
"\n",
"case",
"501",
":",
"return",
"ErrNotImplemented",
"\n",
"case",
"502",
":",
"return",
"ErrBadGateway",
"\n",
"case",
"503",
":",
"return",
"ErrServiceUnavailable",
"\n",
"case",
"504",
":",
"return",
"ErrGatewayTimeout",
"\n",
"case",
"505",
":",
"return",
"ErrHTTPVersionNotSupported",
"\n",
"case",
"506",
":",
"return",
"ErrVariantAlsoNegotiates",
"\n",
"case",
"507",
":",
"return",
"ErrInsufficientStorage",
"\n",
"case",
"508",
":",
"return",
"ErrLoopDetected",
"\n",
"case",
"510",
":",
"return",
"ErrNotExtended",
"\n",
"case",
"511",
":",
"return",
"ErrNetworkAuthenticationRequired",
"\n",
"default",
":",
"return",
"Err",
".",
"WithCode",
"(",
"status",
")",
"\n",
"}",
"\n",
"}"
] |
// ErrByStatus returns a gear.Error by http status.
|
[
"ErrByStatus",
"returns",
"a",
"gear",
".",
"Error",
"by",
"http",
"status",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/const.go#L160-L245
|
18,312 |
teambition/gear
|
router.go
|
Use
|
func (r *Router) Use(handle Middleware) *Router {
r.mds = append(r.mds, handle)
r.middleware = Compose(r.mds...)
return r
}
|
go
|
func (r *Router) Use(handle Middleware) *Router {
r.mds = append(r.mds, handle)
r.middleware = Compose(r.mds...)
return r
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Use",
"(",
"handle",
"Middleware",
")",
"*",
"Router",
"{",
"r",
".",
"mds",
"=",
"append",
"(",
"r",
".",
"mds",
",",
"handle",
")",
"\n",
"r",
".",
"middleware",
"=",
"Compose",
"(",
"r",
".",
"mds",
"...",
")",
"\n",
"return",
"r",
"\n",
"}"
] |
// Use registers a new Middleware in the router, that will be called when router mathed.
|
[
"Use",
"registers",
"a",
"new",
"Middleware",
"in",
"the",
"router",
"that",
"will",
"be",
"called",
"when",
"router",
"mathed",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L195-L199
|
18,313 |
teambition/gear
|
router.go
|
Get
|
func (r *Router) Get(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodGet, pattern, handlers...)
}
|
go
|
func (r *Router) Get(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodGet, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Get",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodGet",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Get registers a new GET route for a path with matching handler in the router.
|
[
"Get",
"registers",
"a",
"new",
"GET",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L220-L222
|
18,314 |
teambition/gear
|
router.go
|
Head
|
func (r *Router) Head(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodHead, pattern, handlers...)
}
|
go
|
func (r *Router) Head(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodHead, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Head",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodHead",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Head registers a new HEAD route for a path with matching handler in the router.
|
[
"Head",
"registers",
"a",
"new",
"HEAD",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L225-L227
|
18,315 |
teambition/gear
|
router.go
|
Post
|
func (r *Router) Post(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPost, pattern, handlers...)
}
|
go
|
func (r *Router) Post(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPost, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Post",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodPost",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Post registers a new POST route for a path with matching handler in the router.
|
[
"Post",
"registers",
"a",
"new",
"POST",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L230-L232
|
18,316 |
teambition/gear
|
router.go
|
Put
|
func (r *Router) Put(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPut, pattern, handlers...)
}
|
go
|
func (r *Router) Put(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPut, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Put",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodPut",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Put registers a new PUT route for a path with matching handler in the router.
|
[
"Put",
"registers",
"a",
"new",
"PUT",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L235-L237
|
18,317 |
teambition/gear
|
router.go
|
Patch
|
func (r *Router) Patch(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPatch, pattern, handlers...)
}
|
go
|
func (r *Router) Patch(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodPatch, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Patch",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodPatch",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Patch registers a new PATCH route for a path with matching handler in the router.
|
[
"Patch",
"registers",
"a",
"new",
"PATCH",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L240-L242
|
18,318 |
teambition/gear
|
router.go
|
Delete
|
func (r *Router) Delete(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodDelete, pattern, handlers...)
}
|
go
|
func (r *Router) Delete(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodDelete, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Delete",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodDelete",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Delete registers a new DELETE route for a path with matching handler in the router.
|
[
"Delete",
"registers",
"a",
"new",
"DELETE",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L245-L247
|
18,319 |
teambition/gear
|
router.go
|
Options
|
func (r *Router) Options(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodOptions, pattern, handlers...)
}
|
go
|
func (r *Router) Options(pattern string, handlers ...Middleware) *Router {
return r.Handle(http.MethodOptions, pattern, handlers...)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Options",
"(",
"pattern",
"string",
",",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"return",
"r",
".",
"Handle",
"(",
"http",
".",
"MethodOptions",
",",
"pattern",
",",
"handlers",
"...",
")",
"\n",
"}"
] |
// Options registers a new OPTIONS route for a path with matching handler in the router.
|
[
"Options",
"registers",
"a",
"new",
"OPTIONS",
"route",
"for",
"a",
"path",
"with",
"matching",
"handler",
"in",
"the",
"router",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L250-L252
|
18,320 |
teambition/gear
|
router.go
|
Otherwise
|
func (r *Router) Otherwise(handlers ...Middleware) *Router {
if len(handlers) == 0 {
panic(Err.WithMsg("invalid middleware"))
}
r.otherwise = Compose(handlers...)
return r
}
|
go
|
func (r *Router) Otherwise(handlers ...Middleware) *Router {
if len(handlers) == 0 {
panic(Err.WithMsg("invalid middleware"))
}
r.otherwise = Compose(handlers...)
return r
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Otherwise",
"(",
"handlers",
"...",
"Middleware",
")",
"*",
"Router",
"{",
"if",
"len",
"(",
"handlers",
")",
"==",
"0",
"{",
"panic",
"(",
"Err",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"r",
".",
"otherwise",
"=",
"Compose",
"(",
"handlers",
"...",
")",
"\n",
"return",
"r",
"\n",
"}"
] |
// Otherwise registers a new Middleware handler in the router
// that will run if there is no other handler matching.
|
[
"Otherwise",
"registers",
"a",
"new",
"Middleware",
"handler",
"in",
"the",
"router",
"that",
"will",
"run",
"if",
"there",
"is",
"no",
"other",
"handler",
"matching",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L256-L262
|
18,321 |
teambition/gear
|
router.go
|
Serve
|
func (r *Router) Serve(ctx *Context) error {
path := ctx.Path
method := ctx.Method
var handler Middleware
if !strings.HasPrefix(path, r.root) && path != r.rt {
return nil
}
if path == r.rt {
path = "/"
} else if l := len(r.rt); l > 0 {
path = path[l:]
}
matched := r.trie.Match(path)
if matched.Node == nil {
// FixedPathRedirect or TrailingSlashRedirect
if matched.TSR != "" || matched.FPR != "" {
ctx.Req.URL.Path = matched.TSR
if matched.FPR != "" {
ctx.Req.URL.Path = matched.FPR
}
if len(r.root) > 1 {
ctx.Req.URL.Path = r.root + ctx.Req.URL.Path[1:]
}
code := http.StatusMovedPermanently
if method != "GET" {
code = http.StatusTemporaryRedirect
}
ctx.Status(code)
return ctx.Redirect(ctx.Req.URL.String())
}
if r.otherwise == nil {
return nil
}
handler = r.otherwise
} else {
ok := false
if handler, ok = matched.Node.GetHandler(method).(Middleware); !ok {
// OPTIONS support
if method == http.MethodOptions {
ctx.SetHeader(HeaderAllow, matched.Node.GetAllow())
return ctx.End(http.StatusNoContent)
}
if r.otherwise == nil {
// If no route handler is returned, it's a 405 error
ctx.SetHeader(HeaderAllow, matched.Node.GetAllow())
return ErrMethodNotAllowed.WithMsgf(`"%s" is not allowed in "%s"`, method, ctx.Path)
}
handler = r.otherwise
}
}
ctx.SetAny(paramsKey, matched.Params)
ctx.SetAny(routerNodeKey, matched.Node)
ctx.SetAny(routerRootKey, r.rt)
if len(r.mds) > 0 {
handler = Compose(r.middleware, handler)
}
return handler(ctx)
}
|
go
|
func (r *Router) Serve(ctx *Context) error {
path := ctx.Path
method := ctx.Method
var handler Middleware
if !strings.HasPrefix(path, r.root) && path != r.rt {
return nil
}
if path == r.rt {
path = "/"
} else if l := len(r.rt); l > 0 {
path = path[l:]
}
matched := r.trie.Match(path)
if matched.Node == nil {
// FixedPathRedirect or TrailingSlashRedirect
if matched.TSR != "" || matched.FPR != "" {
ctx.Req.URL.Path = matched.TSR
if matched.FPR != "" {
ctx.Req.URL.Path = matched.FPR
}
if len(r.root) > 1 {
ctx.Req.URL.Path = r.root + ctx.Req.URL.Path[1:]
}
code := http.StatusMovedPermanently
if method != "GET" {
code = http.StatusTemporaryRedirect
}
ctx.Status(code)
return ctx.Redirect(ctx.Req.URL.String())
}
if r.otherwise == nil {
return nil
}
handler = r.otherwise
} else {
ok := false
if handler, ok = matched.Node.GetHandler(method).(Middleware); !ok {
// OPTIONS support
if method == http.MethodOptions {
ctx.SetHeader(HeaderAllow, matched.Node.GetAllow())
return ctx.End(http.StatusNoContent)
}
if r.otherwise == nil {
// If no route handler is returned, it's a 405 error
ctx.SetHeader(HeaderAllow, matched.Node.GetAllow())
return ErrMethodNotAllowed.WithMsgf(`"%s" is not allowed in "%s"`, method, ctx.Path)
}
handler = r.otherwise
}
}
ctx.SetAny(paramsKey, matched.Params)
ctx.SetAny(routerNodeKey, matched.Node)
ctx.SetAny(routerRootKey, r.rt)
if len(r.mds) > 0 {
handler = Compose(r.middleware, handler)
}
return handler(ctx)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Serve",
"(",
"ctx",
"*",
"Context",
")",
"error",
"{",
"path",
":=",
"ctx",
".",
"Path",
"\n",
"method",
":=",
"ctx",
".",
"Method",
"\n",
"var",
"handler",
"Middleware",
"\n\n",
"if",
"!",
"strings",
".",
"HasPrefix",
"(",
"path",
",",
"r",
".",
"root",
")",
"&&",
"path",
"!=",
"r",
".",
"rt",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"path",
"==",
"r",
".",
"rt",
"{",
"path",
"=",
"\"",
"\"",
"\n",
"}",
"else",
"if",
"l",
":=",
"len",
"(",
"r",
".",
"rt",
")",
";",
"l",
">",
"0",
"{",
"path",
"=",
"path",
"[",
"l",
":",
"]",
"\n",
"}",
"\n\n",
"matched",
":=",
"r",
".",
"trie",
".",
"Match",
"(",
"path",
")",
"\n\n",
"if",
"matched",
".",
"Node",
"==",
"nil",
"{",
"// FixedPathRedirect or TrailingSlashRedirect",
"if",
"matched",
".",
"TSR",
"!=",
"\"",
"\"",
"||",
"matched",
".",
"FPR",
"!=",
"\"",
"\"",
"{",
"ctx",
".",
"Req",
".",
"URL",
".",
"Path",
"=",
"matched",
".",
"TSR",
"\n",
"if",
"matched",
".",
"FPR",
"!=",
"\"",
"\"",
"{",
"ctx",
".",
"Req",
".",
"URL",
".",
"Path",
"=",
"matched",
".",
"FPR",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"root",
")",
">",
"1",
"{",
"ctx",
".",
"Req",
".",
"URL",
".",
"Path",
"=",
"r",
".",
"root",
"+",
"ctx",
".",
"Req",
".",
"URL",
".",
"Path",
"[",
"1",
":",
"]",
"\n",
"}",
"\n\n",
"code",
":=",
"http",
".",
"StatusMovedPermanently",
"\n",
"if",
"method",
"!=",
"\"",
"\"",
"{",
"code",
"=",
"http",
".",
"StatusTemporaryRedirect",
"\n",
"}",
"\n",
"ctx",
".",
"Status",
"(",
"code",
")",
"\n",
"return",
"ctx",
".",
"Redirect",
"(",
"ctx",
".",
"Req",
".",
"URL",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n\n",
"if",
"r",
".",
"otherwise",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"handler",
"=",
"r",
".",
"otherwise",
"\n",
"}",
"else",
"{",
"ok",
":=",
"false",
"\n",
"if",
"handler",
",",
"ok",
"=",
"matched",
".",
"Node",
".",
"GetHandler",
"(",
"method",
")",
".",
"(",
"Middleware",
")",
";",
"!",
"ok",
"{",
"// OPTIONS support",
"if",
"method",
"==",
"http",
".",
"MethodOptions",
"{",
"ctx",
".",
"SetHeader",
"(",
"HeaderAllow",
",",
"matched",
".",
"Node",
".",
"GetAllow",
"(",
")",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"http",
".",
"StatusNoContent",
")",
"\n",
"}",
"\n\n",
"if",
"r",
".",
"otherwise",
"==",
"nil",
"{",
"// If no route handler is returned, it's a 405 error",
"ctx",
".",
"SetHeader",
"(",
"HeaderAllow",
",",
"matched",
".",
"Node",
".",
"GetAllow",
"(",
")",
")",
"\n",
"return",
"ErrMethodNotAllowed",
".",
"WithMsgf",
"(",
"`\"%s\" is not allowed in \"%s\"`",
",",
"method",
",",
"ctx",
".",
"Path",
")",
"\n",
"}",
"\n",
"handler",
"=",
"r",
".",
"otherwise",
"\n",
"}",
"\n",
"}",
"\n\n",
"ctx",
".",
"SetAny",
"(",
"paramsKey",
",",
"matched",
".",
"Params",
")",
"\n",
"ctx",
".",
"SetAny",
"(",
"routerNodeKey",
",",
"matched",
".",
"Node",
")",
"\n",
"ctx",
".",
"SetAny",
"(",
"routerRootKey",
",",
"r",
".",
"rt",
")",
"\n",
"if",
"len",
"(",
"r",
".",
"mds",
")",
">",
"0",
"{",
"handler",
"=",
"Compose",
"(",
"r",
".",
"middleware",
",",
"handler",
")",
"\n",
"}",
"\n",
"return",
"handler",
"(",
"ctx",
")",
"\n",
"}"
] |
// Serve implemented gear.Handler interface
|
[
"Serve",
"implemented",
"gear",
".",
"Handler",
"interface"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/router.go#L265-L330
|
18,322 |
teambition/gear
|
middleware/favicon/favicon.go
|
NewWithIco
|
func NewWithIco(file []byte, times ...time.Time) gear.Middleware {
modTime := time.Now()
reader := bytes.NewReader(file)
if len(times) > 0 {
modTime = times[0]
}
return func(ctx *gear.Context) (err error) {
if ctx.Path != "/favicon.ico" {
return
}
if ctx.Method != http.MethodGet && ctx.Method != http.MethodHead {
status := 200
if ctx.Method != http.MethodOptions {
status = 405
}
ctx.SetHeader(gear.HeaderContentType, "text/plain; charset=utf-8")
ctx.SetHeader(gear.HeaderAllow, "GET, HEAD, OPTIONS")
return ctx.End(status)
}
ctx.Type("image/x-icon")
http.ServeContent(ctx.Res, ctx.Req, "favicon.ico", modTime, reader)
return
}
}
|
go
|
func NewWithIco(file []byte, times ...time.Time) gear.Middleware {
modTime := time.Now()
reader := bytes.NewReader(file)
if len(times) > 0 {
modTime = times[0]
}
return func(ctx *gear.Context) (err error) {
if ctx.Path != "/favicon.ico" {
return
}
if ctx.Method != http.MethodGet && ctx.Method != http.MethodHead {
status := 200
if ctx.Method != http.MethodOptions {
status = 405
}
ctx.SetHeader(gear.HeaderContentType, "text/plain; charset=utf-8")
ctx.SetHeader(gear.HeaderAllow, "GET, HEAD, OPTIONS")
return ctx.End(status)
}
ctx.Type("image/x-icon")
http.ServeContent(ctx.Res, ctx.Req, "favicon.ico", modTime, reader)
return
}
}
|
[
"func",
"NewWithIco",
"(",
"file",
"[",
"]",
"byte",
",",
"times",
"...",
"time",
".",
"Time",
")",
"gear",
".",
"Middleware",
"{",
"modTime",
":=",
"time",
".",
"Now",
"(",
")",
"\n",
"reader",
":=",
"bytes",
".",
"NewReader",
"(",
"file",
")",
"\n",
"if",
"len",
"(",
"times",
")",
">",
"0",
"{",
"modTime",
"=",
"times",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"return",
"func",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"(",
"err",
"error",
")",
"{",
"if",
"ctx",
".",
"Path",
"!=",
"\"",
"\"",
"{",
"return",
"\n",
"}",
"\n",
"if",
"ctx",
".",
"Method",
"!=",
"http",
".",
"MethodGet",
"&&",
"ctx",
".",
"Method",
"!=",
"http",
".",
"MethodHead",
"{",
"status",
":=",
"200",
"\n",
"if",
"ctx",
".",
"Method",
"!=",
"http",
".",
"MethodOptions",
"{",
"status",
"=",
"405",
"\n",
"}",
"\n",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderContentType",
",",
"\"",
"\"",
")",
"\n",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAllow",
",",
"\"",
"\"",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"status",
")",
"\n",
"}",
"\n",
"ctx",
".",
"Type",
"(",
"\"",
"\"",
")",
"\n",
"http",
".",
"ServeContent",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
",",
"\"",
"\"",
",",
"modTime",
",",
"reader",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}"
] |
// NewWithIco creates a favicon middleware with ico file and a optional modTime.
|
[
"NewWithIco",
"creates",
"a",
"favicon",
"middleware",
"with",
"ico",
"file",
"and",
"a",
"optional",
"modTime",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/middleware/favicon/favicon.go#L53-L77
|
18,323 |
teambition/gear
|
logging/color.go
|
colorString
|
func colorString(code int, str string) string {
return fmt.Sprintf("\x1b[%d;1m%s\x1b[39;22m", code, str)
}
|
go
|
func colorString(code int, str string) string {
return fmt.Sprintf("\x1b[%d;1m%s\x1b[39;22m", code, str)
}
|
[
"func",
"colorString",
"(",
"code",
"int",
",",
"str",
"string",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\\x1b",
"\\x1b",
"\"",
",",
"code",
",",
"str",
")",
"\n",
"}"
] |
// colorString convert a string to a color string with color code.
|
[
"colorString",
"convert",
"a",
"string",
"to",
"a",
"color",
"string",
"with",
"color",
"code",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/color.go#L34-L36
|
18,324 |
teambition/gear
|
context.go
|
NewContext
|
func NewContext(app *App, w http.ResponseWriter, r *http.Request) *Context {
ctx := Context{
app: app,
Req: r,
Res: &Response{w: w, rw: w, handlerHeader: w.Header()},
Host: r.Host,
Method: r.Method,
Path: r.URL.Path,
Cookies: cookie.New(w, r, app.keys...),
kv: make(map[interface{}]interface{}),
}
if app.serverName != "" {
ctx.SetHeader(HeaderServer, app.serverName)
}
if app.timeout <= 0 {
ctx.ctx, ctx.cancelCtx = context.WithCancel(r.Context())
} else {
ctx.ctx, ctx.cancelCtx = context.WithTimeout(r.Context(), app.timeout)
}
ctx.ctx = context.WithValue(ctx.ctx, isContext, isContext)
if app.withContext != nil {
ctx._ctx = app.withContext(r.WithContext(ctx.ctx))
if ctx._ctx.Value(isContext) == nil {
panic(Err.WithMsg("the context is not created from gear.Context"))
}
} else {
ctx._ctx = ctx.ctx
}
return &ctx
}
|
go
|
func NewContext(app *App, w http.ResponseWriter, r *http.Request) *Context {
ctx := Context{
app: app,
Req: r,
Res: &Response{w: w, rw: w, handlerHeader: w.Header()},
Host: r.Host,
Method: r.Method,
Path: r.URL.Path,
Cookies: cookie.New(w, r, app.keys...),
kv: make(map[interface{}]interface{}),
}
if app.serverName != "" {
ctx.SetHeader(HeaderServer, app.serverName)
}
if app.timeout <= 0 {
ctx.ctx, ctx.cancelCtx = context.WithCancel(r.Context())
} else {
ctx.ctx, ctx.cancelCtx = context.WithTimeout(r.Context(), app.timeout)
}
ctx.ctx = context.WithValue(ctx.ctx, isContext, isContext)
if app.withContext != nil {
ctx._ctx = app.withContext(r.WithContext(ctx.ctx))
if ctx._ctx.Value(isContext) == nil {
panic(Err.WithMsg("the context is not created from gear.Context"))
}
} else {
ctx._ctx = ctx.ctx
}
return &ctx
}
|
[
"func",
"NewContext",
"(",
"app",
"*",
"App",
",",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"*",
"Context",
"{",
"ctx",
":=",
"Context",
"{",
"app",
":",
"app",
",",
"Req",
":",
"r",
",",
"Res",
":",
"&",
"Response",
"{",
"w",
":",
"w",
",",
"rw",
":",
"w",
",",
"handlerHeader",
":",
"w",
".",
"Header",
"(",
")",
"}",
",",
"Host",
":",
"r",
".",
"Host",
",",
"Method",
":",
"r",
".",
"Method",
",",
"Path",
":",
"r",
".",
"URL",
".",
"Path",
",",
"Cookies",
":",
"cookie",
".",
"New",
"(",
"w",
",",
"r",
",",
"app",
".",
"keys",
"...",
")",
",",
"kv",
":",
"make",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
",",
"}",
"\n\n",
"if",
"app",
".",
"serverName",
"!=",
"\"",
"\"",
"{",
"ctx",
".",
"SetHeader",
"(",
"HeaderServer",
",",
"app",
".",
"serverName",
")",
"\n",
"}",
"\n\n",
"if",
"app",
".",
"timeout",
"<=",
"0",
"{",
"ctx",
".",
"ctx",
",",
"ctx",
".",
"cancelCtx",
"=",
"context",
".",
"WithCancel",
"(",
"r",
".",
"Context",
"(",
")",
")",
"\n",
"}",
"else",
"{",
"ctx",
".",
"ctx",
",",
"ctx",
".",
"cancelCtx",
"=",
"context",
".",
"WithTimeout",
"(",
"r",
".",
"Context",
"(",
")",
",",
"app",
".",
"timeout",
")",
"\n",
"}",
"\n",
"ctx",
".",
"ctx",
"=",
"context",
".",
"WithValue",
"(",
"ctx",
".",
"ctx",
",",
"isContext",
",",
"isContext",
")",
"\n\n",
"if",
"app",
".",
"withContext",
"!=",
"nil",
"{",
"ctx",
".",
"_ctx",
"=",
"app",
".",
"withContext",
"(",
"r",
".",
"WithContext",
"(",
"ctx",
".",
"ctx",
")",
")",
"\n",
"if",
"ctx",
".",
"_ctx",
".",
"Value",
"(",
"isContext",
")",
"==",
"nil",
"{",
"panic",
"(",
"Err",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"ctx",
".",
"_ctx",
"=",
"ctx",
".",
"ctx",
"\n",
"}",
"\n",
"return",
"&",
"ctx",
"\n",
"}"
] |
// NewContext creates an instance of Context. Export for testing middleware.
|
[
"NewContext",
"creates",
"an",
"instance",
"of",
"Context",
".",
"Export",
"for",
"testing",
"middleware",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L62-L96
|
18,325 |
teambition/gear
|
context.go
|
Cancel
|
func (ctx *Context) Cancel() {
ctx.Res.ended.setTrue() // end the middleware process
ctx.Res.afterHooks = nil
ctx.cancelCtx()
}
|
go
|
func (ctx *Context) Cancel() {
ctx.Res.ended.setTrue() // end the middleware process
ctx.Res.afterHooks = nil
ctx.cancelCtx()
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Cancel",
"(",
")",
"{",
"ctx",
".",
"Res",
".",
"ended",
".",
"setTrue",
"(",
")",
"// end the middleware process",
"\n",
"ctx",
".",
"Res",
".",
"afterHooks",
"=",
"nil",
"\n",
"ctx",
".",
"cancelCtx",
"(",
")",
"\n",
"}"
] |
// Cancel cancel the ctx and all it' children context.
// The ctx' process will ended too.
|
[
"Cancel",
"cancel",
"the",
"ctx",
"and",
"all",
"it",
"children",
"context",
".",
"The",
"ctx",
"process",
"will",
"ended",
"too",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L129-L133
|
18,326 |
teambition/gear
|
context.go
|
WithCancel
|
func (ctx *Context) WithCancel() (context.Context, context.CancelFunc) {
return context.WithCancel(ctx._ctx)
}
|
go
|
func (ctx *Context) WithCancel() (context.Context, context.CancelFunc) {
return context.WithCancel(ctx._ctx)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"WithCancel",
"(",
")",
"(",
"context",
".",
"Context",
",",
"context",
".",
"CancelFunc",
")",
"{",
"return",
"context",
".",
"WithCancel",
"(",
"ctx",
".",
"_ctx",
")",
"\n",
"}"
] |
// WithCancel returns a copy of the ctx with a new Done channel.
// The returned context's Done channel is closed when the returned cancel function is called or when the parent context's Done channel is closed, whichever happens first.
|
[
"WithCancel",
"returns",
"a",
"copy",
"of",
"the",
"ctx",
"with",
"a",
"new",
"Done",
"channel",
".",
"The",
"returned",
"context",
"s",
"Done",
"channel",
"is",
"closed",
"when",
"the",
"returned",
"cancel",
"function",
"is",
"called",
"or",
"when",
"the",
"parent",
"context",
"s",
"Done",
"channel",
"is",
"closed",
"whichever",
"happens",
"first",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L137-L139
|
18,327 |
teambition/gear
|
context.go
|
WithDeadline
|
func (ctx *Context) WithDeadline(deadline time.Time) (context.Context, context.CancelFunc) {
return context.WithDeadline(ctx._ctx, deadline)
}
|
go
|
func (ctx *Context) WithDeadline(deadline time.Time) (context.Context, context.CancelFunc) {
return context.WithDeadline(ctx._ctx, deadline)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"WithDeadline",
"(",
"deadline",
"time",
".",
"Time",
")",
"(",
"context",
".",
"Context",
",",
"context",
".",
"CancelFunc",
")",
"{",
"return",
"context",
".",
"WithDeadline",
"(",
"ctx",
".",
"_ctx",
",",
"deadline",
")",
"\n",
"}"
] |
// WithDeadline returns a copy of the ctx with the deadline adjusted to be no later than d.
|
[
"WithDeadline",
"returns",
"a",
"copy",
"of",
"the",
"ctx",
"with",
"the",
"deadline",
"adjusted",
"to",
"be",
"no",
"later",
"than",
"d",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L142-L144
|
18,328 |
teambition/gear
|
context.go
|
WithValue
|
func (ctx *Context) WithValue(key, val interface{}) context.Context {
return context.WithValue(ctx._ctx, key, val)
}
|
go
|
func (ctx *Context) WithValue(key, val interface{}) context.Context {
return context.WithValue(ctx._ctx, key, val)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"WithValue",
"(",
"key",
",",
"val",
"interface",
"{",
"}",
")",
"context",
".",
"Context",
"{",
"return",
"context",
".",
"WithValue",
"(",
"ctx",
".",
"_ctx",
",",
"key",
",",
"val",
")",
"\n",
"}"
] |
// WithValue returns a copy of the ctx in which the value associated with key is val.
|
[
"WithValue",
"returns",
"a",
"copy",
"of",
"the",
"ctx",
"in",
"which",
"the",
"value",
"associated",
"with",
"key",
"is",
"val",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L152-L154
|
18,329 |
teambition/gear
|
context.go
|
Timing
|
func (ctx *Context) Timing(dt time.Duration, fn func(context.Context)) (err error) {
ct, cancel := ctx.WithTimeout(dt)
defer cancel()
ch := make(chan error, 1) // not block tryRunTiming
go tryRunTiming(ct, fn, ch)
select {
case <-ct.Done():
err = ct.Err()
case err = <-ch:
}
return
}
|
go
|
func (ctx *Context) Timing(dt time.Duration, fn func(context.Context)) (err error) {
ct, cancel := ctx.WithTimeout(dt)
defer cancel()
ch := make(chan error, 1) // not block tryRunTiming
go tryRunTiming(ct, fn, ch)
select {
case <-ct.Done():
err = ct.Err()
case err = <-ch:
}
return
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Timing",
"(",
"dt",
"time",
".",
"Duration",
",",
"fn",
"func",
"(",
"context",
".",
"Context",
")",
")",
"(",
"err",
"error",
")",
"{",
"ct",
",",
"cancel",
":=",
"ctx",
".",
"WithTimeout",
"(",
"dt",
")",
"\n",
"defer",
"cancel",
"(",
")",
"\n\n",
"ch",
":=",
"make",
"(",
"chan",
"error",
",",
"1",
")",
"// not block tryRunTiming",
"\n",
"go",
"tryRunTiming",
"(",
"ct",
",",
"fn",
",",
"ch",
")",
"\n\n",
"select",
"{",
"case",
"<-",
"ct",
".",
"Done",
"(",
")",
":",
"err",
"=",
"ct",
".",
"Err",
"(",
")",
"\n",
"case",
"err",
"=",
"<-",
"ch",
":",
"}",
"\n",
"return",
"\n",
"}"
] |
// Timing runs fn with the given time limit. If a call runs for longer than its time limit or panic,
// it will return context.DeadlineExceeded error or panic error.
|
[
"Timing",
"runs",
"fn",
"with",
"the",
"given",
"time",
"limit",
".",
"If",
"a",
"call",
"runs",
"for",
"longer",
"than",
"its",
"time",
"limit",
"or",
"panic",
"it",
"will",
"return",
"context",
".",
"DeadlineExceeded",
"error",
"or",
"panic",
"error",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L195-L208
|
18,330 |
teambition/gear
|
context.go
|
MustAny
|
func (ctx *Context) MustAny(any interface{}) interface{} {
val, err := ctx.Any(any)
if err != nil {
panic(err)
}
return val
}
|
go
|
func (ctx *Context) MustAny(any interface{}) interface{} {
val, err := ctx.Any(any)
if err != nil {
panic(err)
}
return val
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"MustAny",
"(",
"any",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"val",
",",
"err",
":=",
"ctx",
".",
"Any",
"(",
"any",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"val",
"\n",
"}"
] |
// MustAny returns the value on this ctx by key. It is a sugar for ctx.Any,
// If some error occurred, it will panic.
|
[
"MustAny",
"returns",
"the",
"value",
"on",
"this",
"ctx",
"by",
"key",
".",
"It",
"is",
"a",
"sugar",
"for",
"ctx",
".",
"Any",
"If",
"some",
"error",
"occurred",
"it",
"will",
"panic",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L248-L254
|
18,331 |
teambition/gear
|
context.go
|
IP
|
func (ctx *Context) IP(trustedProxy ...bool) net.IP {
trusted := ctx.Setting(SetTrustedProxy).(bool)
if len(trustedProxy) > 0 {
trusted = trustedProxy[0]
}
if trusted {
ip := ctx.Req.Header.Get(HeaderXForwardedFor)
if ip == "" {
ip = ctx.Req.Header.Get(HeaderXRealIP)
} else if i := strings.IndexByte(ip, ','); i >= 0 {
ip = ip[0:i]
}
if realIP := net.ParseIP(ip); realIP != nil {
return realIP
}
}
ra := ctx.Req.RemoteAddr
ra, _, _ = net.SplitHostPort(ra)
return net.ParseIP(ra)
}
|
go
|
func (ctx *Context) IP(trustedProxy ...bool) net.IP {
trusted := ctx.Setting(SetTrustedProxy).(bool)
if len(trustedProxy) > 0 {
trusted = trustedProxy[0]
}
if trusted {
ip := ctx.Req.Header.Get(HeaderXForwardedFor)
if ip == "" {
ip = ctx.Req.Header.Get(HeaderXRealIP)
} else if i := strings.IndexByte(ip, ','); i >= 0 {
ip = ip[0:i]
}
if realIP := net.ParseIP(ip); realIP != nil {
return realIP
}
}
ra := ctx.Req.RemoteAddr
ra, _, _ = net.SplitHostPort(ra)
return net.ParseIP(ra)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"IP",
"(",
"trustedProxy",
"...",
"bool",
")",
"net",
".",
"IP",
"{",
"trusted",
":=",
"ctx",
".",
"Setting",
"(",
"SetTrustedProxy",
")",
".",
"(",
"bool",
")",
"\n",
"if",
"len",
"(",
"trustedProxy",
")",
">",
"0",
"{",
"trusted",
"=",
"trustedProxy",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"if",
"trusted",
"{",
"ip",
":=",
"ctx",
".",
"Req",
".",
"Header",
".",
"Get",
"(",
"HeaderXForwardedFor",
")",
"\n",
"if",
"ip",
"==",
"\"",
"\"",
"{",
"ip",
"=",
"ctx",
".",
"Req",
".",
"Header",
".",
"Get",
"(",
"HeaderXRealIP",
")",
"\n",
"}",
"else",
"if",
"i",
":=",
"strings",
".",
"IndexByte",
"(",
"ip",
",",
"','",
")",
";",
"i",
">=",
"0",
"{",
"ip",
"=",
"ip",
"[",
"0",
":",
"i",
"]",
"\n",
"}",
"\n\n",
"if",
"realIP",
":=",
"net",
".",
"ParseIP",
"(",
"ip",
")",
";",
"realIP",
"!=",
"nil",
"{",
"return",
"realIP",
"\n",
"}",
"\n",
"}",
"\n\n",
"ra",
":=",
"ctx",
".",
"Req",
".",
"RemoteAddr",
"\n",
"ra",
",",
"_",
",",
"_",
"=",
"net",
".",
"SplitHostPort",
"(",
"ra",
")",
"\n",
"return",
"net",
".",
"ParseIP",
"(",
"ra",
")",
"\n",
"}"
] |
// IP returns the client's network address based on `X-Forwarded-For`
// or `X-Real-IP` request header.
|
[
"IP",
"returns",
"the",
"client",
"s",
"network",
"address",
"based",
"on",
"X",
"-",
"Forwarded",
"-",
"For",
"or",
"X",
"-",
"Real",
"-",
"IP",
"request",
"header",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L277-L299
|
18,332 |
teambition/gear
|
context.go
|
AcceptType
|
func (ctx *Context) AcceptType(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Type(preferred...)
}
|
go
|
func (ctx *Context) AcceptType(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Type(preferred...)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"AcceptType",
"(",
"preferred",
"...",
"string",
")",
"string",
"{",
"return",
"negotiator",
".",
"New",
"(",
"ctx",
".",
"Req",
".",
"Header",
")",
".",
"Type",
"(",
"preferred",
"...",
")",
"\n",
"}"
] |
// AcceptType returns the most preferred content type from the HTTP Accept header.
// If nothing accepted, then empty string is returned.
|
[
"AcceptType",
"returns",
"the",
"most",
"preferred",
"content",
"type",
"from",
"the",
"HTTP",
"Accept",
"header",
".",
"If",
"nothing",
"accepted",
"then",
"empty",
"string",
"is",
"returned",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L317-L319
|
18,333 |
teambition/gear
|
context.go
|
AcceptLanguage
|
func (ctx *Context) AcceptLanguage(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Language(preferred...)
}
|
go
|
func (ctx *Context) AcceptLanguage(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Language(preferred...)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"AcceptLanguage",
"(",
"preferred",
"...",
"string",
")",
"string",
"{",
"return",
"negotiator",
".",
"New",
"(",
"ctx",
".",
"Req",
".",
"Header",
")",
".",
"Language",
"(",
"preferred",
"...",
")",
"\n",
"}"
] |
// AcceptLanguage returns the most preferred language from the HTTP Accept-Language header.
// If nothing accepted, then empty string is returned.
|
[
"AcceptLanguage",
"returns",
"the",
"most",
"preferred",
"language",
"from",
"the",
"HTTP",
"Accept",
"-",
"Language",
"header",
".",
"If",
"nothing",
"accepted",
"then",
"empty",
"string",
"is",
"returned",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L323-L325
|
18,334 |
teambition/gear
|
context.go
|
AcceptEncoding
|
func (ctx *Context) AcceptEncoding(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Encoding(preferred...)
}
|
go
|
func (ctx *Context) AcceptEncoding(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Encoding(preferred...)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"AcceptEncoding",
"(",
"preferred",
"...",
"string",
")",
"string",
"{",
"return",
"negotiator",
".",
"New",
"(",
"ctx",
".",
"Req",
".",
"Header",
")",
".",
"Encoding",
"(",
"preferred",
"...",
")",
"\n",
"}"
] |
// AcceptEncoding returns the most preferred encoding from the HTTP Accept-Encoding header.
// If nothing accepted, then empty string is returned.
|
[
"AcceptEncoding",
"returns",
"the",
"most",
"preferred",
"encoding",
"from",
"the",
"HTTP",
"Accept",
"-",
"Encoding",
"header",
".",
"If",
"nothing",
"accepted",
"then",
"empty",
"string",
"is",
"returned",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L329-L331
|
18,335 |
teambition/gear
|
context.go
|
AcceptCharset
|
func (ctx *Context) AcceptCharset(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Charset(preferred...)
}
|
go
|
func (ctx *Context) AcceptCharset(preferred ...string) string {
return negotiator.New(ctx.Req.Header).Charset(preferred...)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"AcceptCharset",
"(",
"preferred",
"...",
"string",
")",
"string",
"{",
"return",
"negotiator",
".",
"New",
"(",
"ctx",
".",
"Req",
".",
"Header",
")",
".",
"Charset",
"(",
"preferred",
"...",
")",
"\n",
"}"
] |
// AcceptCharset returns the most preferred charset from the HTTP Accept-Charset header.
// If nothing accepted, then empty string is returned.
|
[
"AcceptCharset",
"returns",
"the",
"most",
"preferred",
"charset",
"from",
"the",
"HTTP",
"Accept",
"-",
"Charset",
"header",
".",
"If",
"nothing",
"accepted",
"then",
"empty",
"string",
"is",
"returned",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L335-L337
|
18,336 |
teambition/gear
|
context.go
|
Query
|
func (ctx *Context) Query(name string) string {
if ctx.query == nil {
ctx.query = ctx.Req.URL.Query()
}
return ctx.query.Get(name)
}
|
go
|
func (ctx *Context) Query(name string) string {
if ctx.query == nil {
ctx.query = ctx.Req.URL.Query()
}
return ctx.query.Get(name)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Query",
"(",
"name",
"string",
")",
"string",
"{",
"if",
"ctx",
".",
"query",
"==",
"nil",
"{",
"ctx",
".",
"query",
"=",
"ctx",
".",
"Req",
".",
"URL",
".",
"Query",
"(",
")",
"\n",
"}",
"\n",
"return",
"ctx",
".",
"query",
".",
"Get",
"(",
"name",
")",
"\n",
"}"
] |
// Query returns the query param for the provided name.
|
[
"Query",
"returns",
"the",
"query",
"param",
"for",
"the",
"provided",
"name",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L348-L353
|
18,337 |
teambition/gear
|
context.go
|
Set
|
func (ctx *Context) Set(key, value string) {
ctx.SetHeader(key, value)
}
|
go
|
func (ctx *Context) Set(key, value string) {
ctx.SetHeader(key, value)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Set",
"(",
"key",
",",
"value",
"string",
")",
"{",
"ctx",
".",
"SetHeader",
"(",
"key",
",",
"value",
")",
"\n",
"}"
] |
// Set - Please use ctx.SetHeader instead. This method will be changed in v2.
|
[
"Set",
"-",
"Please",
"use",
"ctx",
".",
"SetHeader",
"instead",
".",
"This",
"method",
"will",
"be",
"changed",
"in",
"v2",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L484-L486
|
18,338 |
teambition/gear
|
context.go
|
GetHeader
|
func (ctx *Context) GetHeader(key string) string {
switch key {
case "Referer", "referer", "Referrer", "referrer":
if val := ctx.Req.Header.Get("Referer"); val != "" {
return val
}
return ctx.Req.Header.Get("Referrer")
default:
return ctx.Req.Header.Get(key)
}
}
|
go
|
func (ctx *Context) GetHeader(key string) string {
switch key {
case "Referer", "referer", "Referrer", "referrer":
if val := ctx.Req.Header.Get("Referer"); val != "" {
return val
}
return ctx.Req.Header.Get("Referrer")
default:
return ctx.Req.Header.Get(key)
}
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"GetHeader",
"(",
"key",
"string",
")",
"string",
"{",
"switch",
"key",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
":",
"if",
"val",
":=",
"ctx",
".",
"Req",
".",
"Header",
".",
"Get",
"(",
"\"",
"\"",
")",
";",
"val",
"!=",
"\"",
"\"",
"{",
"return",
"val",
"\n",
"}",
"\n",
"return",
"ctx",
".",
"Req",
".",
"Header",
".",
"Get",
"(",
"\"",
"\"",
")",
"\n",
"default",
":",
"return",
"ctx",
".",
"Req",
".",
"Header",
".",
"Get",
"(",
"key",
")",
"\n",
"}",
"\n",
"}"
] |
// GetHeader retrieves data from the request Header.
|
[
"GetHeader",
"retrieves",
"data",
"from",
"the",
"request",
"Header",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L489-L499
|
18,339 |
teambition/gear
|
context.go
|
HTML
|
func (ctx *Context) HTML(code int, str string) error {
ctx.Type(MIMETextHTMLCharsetUTF8)
return ctx.End(code, []byte(str))
}
|
go
|
func (ctx *Context) HTML(code int, str string) error {
ctx.Type(MIMETextHTMLCharsetUTF8)
return ctx.End(code, []byte(str))
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"HTML",
"(",
"code",
"int",
",",
"str",
"string",
")",
"error",
"{",
"ctx",
".",
"Type",
"(",
"MIMETextHTMLCharsetUTF8",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"code",
",",
"[",
"]",
"byte",
"(",
"str",
")",
")",
"\n",
"}"
] |
// HTML set an Html body with status code to response.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"HTML",
"set",
"an",
"Html",
"body",
"with",
"status",
"code",
"to",
"response",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L519-L522
|
18,340 |
teambition/gear
|
context.go
|
JSONBlob
|
func (ctx *Context) JSONBlob(code int, buf []byte) error {
ctx.Type(MIMEApplicationJSONCharsetUTF8)
return ctx.End(code, buf)
}
|
go
|
func (ctx *Context) JSONBlob(code int, buf []byte) error {
ctx.Type(MIMEApplicationJSONCharsetUTF8)
return ctx.End(code, buf)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"JSONBlob",
"(",
"code",
"int",
",",
"buf",
"[",
"]",
"byte",
")",
"error",
"{",
"ctx",
".",
"Type",
"(",
"MIMEApplicationJSONCharsetUTF8",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"code",
",",
"buf",
")",
"\n",
"}"
] |
// JSONBlob set a JSON blob body with status code to response.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"JSONBlob",
"set",
"a",
"JSON",
"blob",
"body",
"with",
"status",
"code",
"to",
"response",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L538-L541
|
18,341 |
teambition/gear
|
context.go
|
JSONPBlob
|
func (ctx *Context) JSONPBlob(code int, callback string, buf []byte) error {
ctx.Type(MIMEApplicationJavaScriptCharsetUTF8)
ctx.SetHeader(HeaderXContentTypeOptions, "nosniff")
// the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
// @see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
// the typeof check is just to reduce client error noise
b := []byte(fmt.Sprintf(`/**/ typeof %s === "function" && %s(`, callback, callback))
b = append(b, buf...)
return ctx.End(code, append(b, ')', ';'))
}
|
go
|
func (ctx *Context) JSONPBlob(code int, callback string, buf []byte) error {
ctx.Type(MIMEApplicationJavaScriptCharsetUTF8)
ctx.SetHeader(HeaderXContentTypeOptions, "nosniff")
// the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
// @see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
// the typeof check is just to reduce client error noise
b := []byte(fmt.Sprintf(`/**/ typeof %s === "function" && %s(`, callback, callback))
b = append(b, buf...)
return ctx.End(code, append(b, ')', ';'))
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"JSONPBlob",
"(",
"code",
"int",
",",
"callback",
"string",
",",
"buf",
"[",
"]",
"byte",
")",
"error",
"{",
"ctx",
".",
"Type",
"(",
"MIMEApplicationJavaScriptCharsetUTF8",
")",
"\n",
"ctx",
".",
"SetHeader",
"(",
"HeaderXContentTypeOptions",
",",
"\"",
"\"",
")",
"\n",
"// the /**/ is a specific security mitigation for \"Rosetta Flash JSONP abuse\"",
"// @see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/",
"// the typeof check is just to reduce client error noise",
"b",
":=",
"[",
"]",
"byte",
"(",
"fmt",
".",
"Sprintf",
"(",
"`/**/ typeof %s === \"function\" && %s(`",
",",
"callback",
",",
"callback",
")",
")",
"\n",
"b",
"=",
"append",
"(",
"b",
",",
"buf",
"...",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"code",
",",
"append",
"(",
"b",
",",
"')'",
",",
"';'",
")",
")",
"\n",
"}"
] |
// JSONPBlob sends a JSONP blob response with status code. It uses `callback`
// to construct the JSONP payload.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"JSONPBlob",
"sends",
"a",
"JSONP",
"blob",
"response",
"with",
"status",
"code",
".",
"It",
"uses",
"callback",
"to",
"construct",
"the",
"JSONP",
"payload",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L558-L567
|
18,342 |
teambition/gear
|
context.go
|
XMLBlob
|
func (ctx *Context) XMLBlob(code int, buf []byte) error {
ctx.Type(MIMEApplicationXMLCharsetUTF8)
return ctx.End(code, buf)
}
|
go
|
func (ctx *Context) XMLBlob(code int, buf []byte) error {
ctx.Type(MIMEApplicationXMLCharsetUTF8)
return ctx.End(code, buf)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"XMLBlob",
"(",
"code",
"int",
",",
"buf",
"[",
"]",
"byte",
")",
"error",
"{",
"ctx",
".",
"Type",
"(",
"MIMEApplicationXMLCharsetUTF8",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"code",
",",
"buf",
")",
"\n",
"}"
] |
// XMLBlob set a XML blob body with status code to response.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"XMLBlob",
"set",
"a",
"XML",
"blob",
"body",
"with",
"status",
"code",
"to",
"response",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L583-L586
|
18,343 |
teambition/gear
|
context.go
|
Stream
|
func (ctx *Context) Stream(code int, contentType string, r io.Reader) (err error) {
if ctx.Res.ended.swapTrue() {
ctx.Status(code)
ctx.Type(contentType)
_, err = io.Copy(ctx.Res, r)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Stream")
}
return
}
|
go
|
func (ctx *Context) Stream(code int, contentType string, r io.Reader) (err error) {
if ctx.Res.ended.swapTrue() {
ctx.Status(code)
ctx.Type(contentType)
_, err = io.Copy(ctx.Res, r)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Stream")
}
return
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Stream",
"(",
"code",
"int",
",",
"contentType",
"string",
",",
"r",
"io",
".",
"Reader",
")",
"(",
"err",
"error",
")",
"{",
"if",
"ctx",
".",
"Res",
".",
"ended",
".",
"swapTrue",
"(",
")",
"{",
"ctx",
".",
"Status",
"(",
"code",
")",
"\n",
"ctx",
".",
"Type",
"(",
"contentType",
")",
"\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"ctx",
".",
"Res",
",",
"r",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Stream sends a streaming response with status code and content type.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"Stream",
"sends",
"a",
"streaming",
"response",
"with",
"status",
"code",
"and",
"content",
"type",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L649-L658
|
18,344 |
teambition/gear
|
context.go
|
Attachment
|
func (ctx *Context) Attachment(name string, modtime time.Time, content io.ReadSeeker, inline ...bool) (err error) {
if ctx.Res.ended.swapTrue() {
dispositionType := "attachment"
if len(inline) > 0 && inline[0] {
dispositionType = "inline"
}
ctx.SetHeader(HeaderContentDisposition, ContentDisposition(name, dispositionType))
http.ServeContent(ctx.Res, ctx.Req, name, modtime, content)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Attachment")
}
return
}
|
go
|
func (ctx *Context) Attachment(name string, modtime time.Time, content io.ReadSeeker, inline ...bool) (err error) {
if ctx.Res.ended.swapTrue() {
dispositionType := "attachment"
if len(inline) > 0 && inline[0] {
dispositionType = "inline"
}
ctx.SetHeader(HeaderContentDisposition, ContentDisposition(name, dispositionType))
http.ServeContent(ctx.Res, ctx.Req, name, modtime, content)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Attachment")
}
return
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Attachment",
"(",
"name",
"string",
",",
"modtime",
"time",
".",
"Time",
",",
"content",
"io",
".",
"ReadSeeker",
",",
"inline",
"...",
"bool",
")",
"(",
"err",
"error",
")",
"{",
"if",
"ctx",
".",
"Res",
".",
"ended",
".",
"swapTrue",
"(",
")",
"{",
"dispositionType",
":=",
"\"",
"\"",
"\n",
"if",
"len",
"(",
"inline",
")",
">",
"0",
"&&",
"inline",
"[",
"0",
"]",
"{",
"dispositionType",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"ctx",
".",
"SetHeader",
"(",
"HeaderContentDisposition",
",",
"ContentDisposition",
"(",
"name",
",",
"dispositionType",
")",
")",
"\n",
"http",
".",
"ServeContent",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
",",
"name",
",",
"modtime",
",",
"content",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Attachment sends a response from `io.ReaderSeeker` as attachment, prompting
// client to save the file. If inline is true, the attachment will sends as inline,
// opening the file in the browser.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"Attachment",
"sends",
"a",
"response",
"from",
"io",
".",
"ReaderSeeker",
"as",
"attachment",
"prompting",
"client",
"to",
"save",
"the",
"file",
".",
"If",
"inline",
"is",
"true",
"the",
"attachment",
"will",
"sends",
"as",
"inline",
"opening",
"the",
"file",
"in",
"the",
"browser",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L665-L677
|
18,345 |
teambition/gear
|
context.go
|
Redirect
|
func (ctx *Context) Redirect(url string) (err error) {
if ctx.Res.ended.swapTrue() {
if !isRedirectStatus(ctx.Res.status) {
ctx.Res.status = http.StatusFound
}
http.Redirect(ctx.Res, ctx.Req, url, ctx.Res.status)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Redirect")
}
return
}
|
go
|
func (ctx *Context) Redirect(url string) (err error) {
if ctx.Res.ended.swapTrue() {
if !isRedirectStatus(ctx.Res.status) {
ctx.Res.status = http.StatusFound
}
http.Redirect(ctx.Res, ctx.Req, url, ctx.Res.status)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.Redirect")
}
return
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"Redirect",
"(",
"url",
"string",
")",
"(",
"err",
"error",
")",
"{",
"if",
"ctx",
".",
"Res",
".",
"ended",
".",
"swapTrue",
"(",
")",
"{",
"if",
"!",
"isRedirectStatus",
"(",
"ctx",
".",
"Res",
".",
"status",
")",
"{",
"ctx",
".",
"Res",
".",
"status",
"=",
"http",
".",
"StatusFound",
"\n",
"}",
"\n",
"http",
".",
"Redirect",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
",",
"url",
",",
"ctx",
".",
"Res",
".",
"status",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Redirect redirects the request with status code 302.
// You can use other status code with ctx.Status method, It is a wrap of http.Redirect.
// It will end the ctx. The middlewares after current middleware will not run.
// "after hooks" and "end hooks" will run normally.
|
[
"Redirect",
"redirects",
"the",
"request",
"with",
"status",
"code",
"302",
".",
"You",
"can",
"use",
"other",
"status",
"code",
"with",
"ctx",
".",
"Status",
"method",
"It",
"is",
"a",
"wrap",
"of",
"http",
".",
"Redirect",
".",
"It",
"will",
"end",
"the",
"ctx",
".",
"The",
"middlewares",
"after",
"current",
"middleware",
"will",
"not",
"run",
".",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L683-L693
|
18,346 |
teambition/gear
|
context.go
|
OkHTML
|
func (ctx *Context) OkHTML(str string) error {
return ctx.HTML(http.StatusOK, str)
}
|
go
|
func (ctx *Context) OkHTML(str string) error {
return ctx.HTML(http.StatusOK, str)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"OkHTML",
"(",
"str",
"string",
")",
"error",
"{",
"return",
"ctx",
".",
"HTML",
"(",
"http",
".",
"StatusOK",
",",
"str",
")",
"\n",
"}"
] |
// OkHTML is a wrap of ctx.HTML with http.StatusOK
|
[
"OkHTML",
"is",
"a",
"wrap",
"of",
"ctx",
".",
"HTML",
"with",
"http",
".",
"StatusOK"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L696-L698
|
18,347 |
teambition/gear
|
context.go
|
OkXML
|
func (ctx *Context) OkXML(val interface{}) error {
return ctx.XML(http.StatusOK, val)
}
|
go
|
func (ctx *Context) OkXML(val interface{}) error {
return ctx.XML(http.StatusOK, val)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"OkXML",
"(",
"val",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"ctx",
".",
"XML",
"(",
"http",
".",
"StatusOK",
",",
"val",
")",
"\n",
"}"
] |
// OkXML is a wrap of ctx.XML with http.StatusOK
|
[
"OkXML",
"is",
"a",
"wrap",
"of",
"ctx",
".",
"XML",
"with",
"http",
".",
"StatusOK"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L708-L710
|
18,348 |
teambition/gear
|
context.go
|
OkSend
|
func (ctx *Context) OkSend(val interface{}) error {
return ctx.Send(http.StatusOK, val)
}
|
go
|
func (ctx *Context) OkSend(val interface{}) error {
return ctx.Send(http.StatusOK, val)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"OkSend",
"(",
"val",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"ctx",
".",
"Send",
"(",
"http",
".",
"StatusOK",
",",
"val",
")",
"\n",
"}"
] |
// OkSend is a wrap of ctx.Send with http.StatusOK
|
[
"OkSend",
"is",
"a",
"wrap",
"of",
"ctx",
".",
"Send",
"with",
"http",
".",
"StatusOK"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L713-L715
|
18,349 |
teambition/gear
|
context.go
|
OkRender
|
func (ctx *Context) OkRender(name string, val interface{}) error {
return ctx.Render(http.StatusOK, name, val)
}
|
go
|
func (ctx *Context) OkRender(name string, val interface{}) error {
return ctx.Render(http.StatusOK, name, val)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"OkRender",
"(",
"name",
"string",
",",
"val",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"ctx",
".",
"Render",
"(",
"http",
".",
"StatusOK",
",",
"name",
",",
"val",
")",
"\n",
"}"
] |
// OkRender is a wrap of ctx.Render with http.StatusOK
|
[
"OkRender",
"is",
"a",
"wrap",
"of",
"ctx",
".",
"Render",
"with",
"http",
".",
"StatusOK"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L718-L720
|
18,350 |
teambition/gear
|
context.go
|
OkStream
|
func (ctx *Context) OkStream(contentType string, r io.Reader) error {
return ctx.Stream(http.StatusOK, contentType, r)
}
|
go
|
func (ctx *Context) OkStream(contentType string, r io.Reader) error {
return ctx.Stream(http.StatusOK, contentType, r)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"OkStream",
"(",
"contentType",
"string",
",",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"return",
"ctx",
".",
"Stream",
"(",
"http",
".",
"StatusOK",
",",
"contentType",
",",
"r",
")",
"\n",
"}"
] |
// OkStream is a wrap of ctx.Stream with http.StatusOK
|
[
"OkStream",
"is",
"a",
"wrap",
"of",
"ctx",
".",
"Stream",
"with",
"http",
".",
"StatusOK"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L723-L725
|
18,351 |
teambition/gear
|
context.go
|
ErrorStatus
|
func (ctx *Context) ErrorStatus(status int) error {
if status >= 400 && IsStatusCode(status) {
return ctx.Error(ErrByStatus(status))
}
return ErrInternalServerError.WithMsg("invalid error status")
}
|
go
|
func (ctx *Context) ErrorStatus(status int) error {
if status >= 400 && IsStatusCode(status) {
return ctx.Error(ErrByStatus(status))
}
return ErrInternalServerError.WithMsg("invalid error status")
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"ErrorStatus",
"(",
"status",
"int",
")",
"error",
"{",
"if",
"status",
">=",
"400",
"&&",
"IsStatusCode",
"(",
"status",
")",
"{",
"return",
"ctx",
".",
"Error",
"(",
"ErrByStatus",
"(",
"status",
")",
")",
"\n",
"}",
"\n",
"return",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}"
] |
// ErrorStatus send a error by status code to response.
// It is sugar of ctx.Error
|
[
"ErrorStatus",
"send",
"a",
"error",
"by",
"status",
"code",
"to",
"response",
".",
"It",
"is",
"sugar",
"of",
"ctx",
".",
"Error"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L744-L749
|
18,352 |
teambition/gear
|
context.go
|
End
|
func (ctx *Context) End(code int, buf ...[]byte) (err error) {
if ctx.Res.ended.swapTrue() {
var body []byte
if len(buf) > 0 {
body = buf[0]
}
err = ctx.Res.respond(code, body)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.End")
}
return
}
|
go
|
func (ctx *Context) End(code int, buf ...[]byte) (err error) {
if ctx.Res.ended.swapTrue() {
var body []byte
if len(buf) > 0 {
body = buf[0]
}
err = ctx.Res.respond(code, body)
} else {
err = ErrInternalServerError.WithMsg("request ended before ctx.End")
}
return
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"End",
"(",
"code",
"int",
",",
"buf",
"...",
"[",
"]",
"byte",
")",
"(",
"err",
"error",
")",
"{",
"if",
"ctx",
".",
"Res",
".",
"ended",
".",
"swapTrue",
"(",
")",
"{",
"var",
"body",
"[",
"]",
"byte",
"\n",
"if",
"len",
"(",
"buf",
")",
">",
"0",
"{",
"body",
"=",
"buf",
"[",
"0",
"]",
"\n",
"}",
"\n",
"err",
"=",
"ctx",
".",
"Res",
".",
"respond",
"(",
"code",
",",
"body",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// End end the ctx with bytes and status code optionally.
// After it's called, the rest of middleware handles will not run.
// But "after hooks" and "end hooks" will run normally.
|
[
"End",
"end",
"the",
"ctx",
"with",
"bytes",
"and",
"status",
"code",
"optionally",
".",
"After",
"it",
"s",
"called",
"the",
"rest",
"of",
"middleware",
"handles",
"will",
"not",
"run",
".",
"But",
"after",
"hooks",
"and",
"end",
"hooks",
"will",
"run",
"normally",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L754-L765
|
18,353 |
teambition/gear
|
context.go
|
After
|
func (ctx *Context) After(hook func()) {
if ctx.Res.ended.isTrue() { // should not add afterHooks if ctx.Res.ended
panic(Err.WithMsg(`can't add "after hook" after middleware process ended`))
}
ctx.Res.afterHooks = append(ctx.Res.afterHooks, hook)
}
|
go
|
func (ctx *Context) After(hook func()) {
if ctx.Res.ended.isTrue() { // should not add afterHooks if ctx.Res.ended
panic(Err.WithMsg(`can't add "after hook" after middleware process ended`))
}
ctx.Res.afterHooks = append(ctx.Res.afterHooks, hook)
}
|
[
"func",
"(",
"ctx",
"*",
"Context",
")",
"After",
"(",
"hook",
"func",
"(",
")",
")",
"{",
"if",
"ctx",
".",
"Res",
".",
"ended",
".",
"isTrue",
"(",
")",
"{",
"// should not add afterHooks if ctx.Res.ended",
"panic",
"(",
"Err",
".",
"WithMsg",
"(",
"`can't add \"after hook\" after middleware process ended`",
")",
")",
"\n",
"}",
"\n",
"ctx",
".",
"Res",
".",
"afterHooks",
"=",
"append",
"(",
"ctx",
".",
"Res",
".",
"afterHooks",
",",
"hook",
")",
"\n",
"}"
] |
// After add a "after hook" to the ctx that will run after middleware process,
// but before Response.WriteHeader. So it will block response writing.
|
[
"After",
"add",
"a",
"after",
"hook",
"to",
"the",
"ctx",
"that",
"will",
"run",
"after",
"middleware",
"process",
"but",
"before",
"Response",
".",
"WriteHeader",
".",
"So",
"it",
"will",
"block",
"response",
"writing",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/context.go#L769-L774
|
18,354 |
teambition/gear
|
middleware/grpc/grpc.go
|
New
|
func New(srv http.Handler) gear.Middleware {
return func(ctx *gear.Context) error {
// "application/grpc", "application/grpc+proto"
if strings.HasPrefix(ctx.GetHeader(gear.HeaderContentType), "application/grpc") {
srv.ServeHTTP(ctx.Res, ctx.Req)
ctx.End(204) // Must end with 204 to handle rpc error
}
return nil
}
}
|
go
|
func New(srv http.Handler) gear.Middleware {
return func(ctx *gear.Context) error {
// "application/grpc", "application/grpc+proto"
if strings.HasPrefix(ctx.GetHeader(gear.HeaderContentType), "application/grpc") {
srv.ServeHTTP(ctx.Res, ctx.Req)
ctx.End(204) // Must end with 204 to handle rpc error
}
return nil
}
}
|
[
"func",
"New",
"(",
"srv",
"http",
".",
"Handler",
")",
"gear",
".",
"Middleware",
"{",
"return",
"func",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"error",
"{",
"// \"application/grpc\", \"application/grpc+proto\"",
"if",
"strings",
".",
"HasPrefix",
"(",
"ctx",
".",
"GetHeader",
"(",
"gear",
".",
"HeaderContentType",
")",
",",
"\"",
"\"",
")",
"{",
"srv",
".",
"ServeHTTP",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
")",
"\n",
"ctx",
".",
"End",
"(",
"204",
")",
"// Must end with 204 to handle rpc error",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// New creates a middleware with gRPC server to Handle gRPC requests.
|
[
"New",
"creates",
"a",
"middleware",
"with",
"gRPC",
"server",
"to",
"Handle",
"gRPC",
"requests",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/middleware/grpc/grpc.go#L11-L20
|
18,355 |
teambition/gear
|
middleware/secure/secure.go
|
HidePoweredBy
|
func HidePoweredBy() gear.Middleware {
return func(ctx *gear.Context) error {
ctx.After(func() {
ctx.Res.Header().Del(gear.HeaderXPoweredBy)
})
return nil
}
}
|
go
|
func HidePoweredBy() gear.Middleware {
return func(ctx *gear.Context) error {
ctx.After(func() {
ctx.Res.Header().Del(gear.HeaderXPoweredBy)
})
return nil
}
}
|
[
"func",
"HidePoweredBy",
"(",
")",
"gear",
".",
"Middleware",
"{",
"return",
"func",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"error",
"{",
"ctx",
".",
"After",
"(",
"func",
"(",
")",
"{",
"ctx",
".",
"Res",
".",
"Header",
"(",
")",
".",
"Del",
"(",
"gear",
".",
"HeaderXPoweredBy",
")",
"\n",
"}",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// HidePoweredBy removes the X-Powered-By header to make it slightly harder for
// attackers to see what potentially-vulnerable technology powers your site.
|
[
"HidePoweredBy",
"removes",
"the",
"X",
"-",
"Powered",
"-",
"By",
"header",
"to",
"make",
"it",
"slightly",
"harder",
"for",
"attackers",
"to",
"see",
"what",
"potentially",
"-",
"vulnerable",
"technology",
"powers",
"your",
"site",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/middleware/secure/secure.go#L119-L126
|
18,356 |
teambition/gear
|
app.go
|
Parse
|
func (d DefaultURLParser) Parse(val map[string][]string, body interface{}, tag string) error {
return ValuesToStruct(val, body, tag)
}
|
go
|
func (d DefaultURLParser) Parse(val map[string][]string, body interface{}, tag string) error {
return ValuesToStruct(val, body, tag)
}
|
[
"func",
"(",
"d",
"DefaultURLParser",
")",
"Parse",
"(",
"val",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
",",
"body",
"interface",
"{",
"}",
",",
"tag",
"string",
")",
"error",
"{",
"return",
"ValuesToStruct",
"(",
"val",
",",
"body",
",",
"tag",
")",
"\n",
"}"
] |
// Parse implemented URLParser interface.
|
[
"Parse",
"implemented",
"URLParser",
"interface",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L46-L48
|
18,357 |
teambition/gear
|
app.go
|
Parse
|
func (d DefaultBodyParser) Parse(buf []byte, body interface{}, mediaType, charset string) error {
if len(buf) == 0 {
return ErrBadRequest.WithMsg("request entity empty")
}
switch true {
case mediaType == MIMEApplicationJSON, isLikeMediaType(mediaType, "json"):
err := json.Unmarshal(buf, body)
if err == nil {
return nil
}
if ute, ok := err.(*json.UnmarshalTypeError); ok {
if ute.Field == "" { // go1.11
return fmt.Errorf("Unmarshal type error: expected=%v, got=%v, offset=%v",
ute.Type, ute.Value, ute.Offset)
}
return fmt.Errorf("Unmarshal type error: field=%v, expected=%v, got=%v, offset=%v",
ute.Field, ute.Type, ute.Value, ute.Offset)
} else if se, ok := err.(*json.SyntaxError); ok {
return fmt.Errorf("Syntax error: offset=%v, error=%v", se.Offset, se.Error())
} else {
return err
}
case mediaType == MIMEApplicationXML, isLikeMediaType(mediaType, "xml"):
return xml.Unmarshal(buf, body)
case mediaType == MIMEApplicationForm:
val, err := url.ParseQuery(string(buf))
if err == nil {
err = ValuesToStruct(val, body, "form")
}
return err
}
return ErrUnsupportedMediaType.WithMsg("unsupported media type")
}
|
go
|
func (d DefaultBodyParser) Parse(buf []byte, body interface{}, mediaType, charset string) error {
if len(buf) == 0 {
return ErrBadRequest.WithMsg("request entity empty")
}
switch true {
case mediaType == MIMEApplicationJSON, isLikeMediaType(mediaType, "json"):
err := json.Unmarshal(buf, body)
if err == nil {
return nil
}
if ute, ok := err.(*json.UnmarshalTypeError); ok {
if ute.Field == "" { // go1.11
return fmt.Errorf("Unmarshal type error: expected=%v, got=%v, offset=%v",
ute.Type, ute.Value, ute.Offset)
}
return fmt.Errorf("Unmarshal type error: field=%v, expected=%v, got=%v, offset=%v",
ute.Field, ute.Type, ute.Value, ute.Offset)
} else if se, ok := err.(*json.SyntaxError); ok {
return fmt.Errorf("Syntax error: offset=%v, error=%v", se.Offset, se.Error())
} else {
return err
}
case mediaType == MIMEApplicationXML, isLikeMediaType(mediaType, "xml"):
return xml.Unmarshal(buf, body)
case mediaType == MIMEApplicationForm:
val, err := url.ParseQuery(string(buf))
if err == nil {
err = ValuesToStruct(val, body, "form")
}
return err
}
return ErrUnsupportedMediaType.WithMsg("unsupported media type")
}
|
[
"func",
"(",
"d",
"DefaultBodyParser",
")",
"Parse",
"(",
"buf",
"[",
"]",
"byte",
",",
"body",
"interface",
"{",
"}",
",",
"mediaType",
",",
"charset",
"string",
")",
"error",
"{",
"if",
"len",
"(",
"buf",
")",
"==",
"0",
"{",
"return",
"ErrBadRequest",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"switch",
"true",
"{",
"case",
"mediaType",
"==",
"MIMEApplicationJSON",
",",
"isLikeMediaType",
"(",
"mediaType",
",",
"\"",
"\"",
")",
":",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"buf",
",",
"body",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"ute",
",",
"ok",
":=",
"err",
".",
"(",
"*",
"json",
".",
"UnmarshalTypeError",
")",
";",
"ok",
"{",
"if",
"ute",
".",
"Field",
"==",
"\"",
"\"",
"{",
"// go1.11",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"ute",
".",
"Type",
",",
"ute",
".",
"Value",
",",
"ute",
".",
"Offset",
")",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"ute",
".",
"Field",
",",
"ute",
".",
"Type",
",",
"ute",
".",
"Value",
",",
"ute",
".",
"Offset",
")",
"\n",
"}",
"else",
"if",
"se",
",",
"ok",
":=",
"err",
".",
"(",
"*",
"json",
".",
"SyntaxError",
")",
";",
"ok",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"se",
".",
"Offset",
",",
"se",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"else",
"{",
"return",
"err",
"\n",
"}",
"\n",
"case",
"mediaType",
"==",
"MIMEApplicationXML",
",",
"isLikeMediaType",
"(",
"mediaType",
",",
"\"",
"\"",
")",
":",
"return",
"xml",
".",
"Unmarshal",
"(",
"buf",
",",
"body",
")",
"\n",
"case",
"mediaType",
"==",
"MIMEApplicationForm",
":",
"val",
",",
"err",
":=",
"url",
".",
"ParseQuery",
"(",
"string",
"(",
"buf",
")",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"err",
"=",
"ValuesToStruct",
"(",
"val",
",",
"body",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"ErrUnsupportedMediaType",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
"\n",
"}"
] |
// Parse implemented BodyParser interface.
|
[
"Parse",
"implemented",
"BodyParser",
"interface",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L72-L106
|
18,358 |
teambition/gear
|
app.go
|
New
|
func New() *App {
app := new(App)
app.Server = new(http.Server)
app.mds = make(middlewares, 0)
app.settings = make(map[interface{}]interface{})
env := os.Getenv("APP_ENV")
if env == "" {
env = "development"
}
app.Set(SetEnv, env)
app.Set(SetServerName, "Gear/"+Version)
app.Set(SetTrustedProxy, false)
app.Set(SetBodyParser, DefaultBodyParser(2<<20)) // 2MB
app.Set(SetURLParser, DefaultURLParser{})
app.Set(SetLogger, log.New(os.Stderr, "", 0))
app.Set(SetParseError, func(err error) HTTPError {
return ParseError(err)
})
app.Set(SetOnError, func(ctx *Context, err HTTPError) {
ctx.Error(err)
})
return app
}
|
go
|
func New() *App {
app := new(App)
app.Server = new(http.Server)
app.mds = make(middlewares, 0)
app.settings = make(map[interface{}]interface{})
env := os.Getenv("APP_ENV")
if env == "" {
env = "development"
}
app.Set(SetEnv, env)
app.Set(SetServerName, "Gear/"+Version)
app.Set(SetTrustedProxy, false)
app.Set(SetBodyParser, DefaultBodyParser(2<<20)) // 2MB
app.Set(SetURLParser, DefaultURLParser{})
app.Set(SetLogger, log.New(os.Stderr, "", 0))
app.Set(SetParseError, func(err error) HTTPError {
return ParseError(err)
})
app.Set(SetOnError, func(ctx *Context, err HTTPError) {
ctx.Error(err)
})
return app
}
|
[
"func",
"New",
"(",
")",
"*",
"App",
"{",
"app",
":=",
"new",
"(",
"App",
")",
"\n",
"app",
".",
"Server",
"=",
"new",
"(",
"http",
".",
"Server",
")",
"\n",
"app",
".",
"mds",
"=",
"make",
"(",
"middlewares",
",",
"0",
")",
"\n",
"app",
".",
"settings",
"=",
"make",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n\n",
"env",
":=",
"os",
".",
"Getenv",
"(",
"\"",
"\"",
")",
"\n",
"if",
"env",
"==",
"\"",
"\"",
"{",
"env",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"app",
".",
"Set",
"(",
"SetEnv",
",",
"env",
")",
"\n",
"app",
".",
"Set",
"(",
"SetServerName",
",",
"\"",
"\"",
"+",
"Version",
")",
"\n",
"app",
".",
"Set",
"(",
"SetTrustedProxy",
",",
"false",
")",
"\n",
"app",
".",
"Set",
"(",
"SetBodyParser",
",",
"DefaultBodyParser",
"(",
"2",
"<<",
"20",
")",
")",
"// 2MB",
"\n",
"app",
".",
"Set",
"(",
"SetURLParser",
",",
"DefaultURLParser",
"{",
"}",
")",
"\n",
"app",
".",
"Set",
"(",
"SetLogger",
",",
"log",
".",
"New",
"(",
"os",
".",
"Stderr",
",",
"\"",
"\"",
",",
"0",
")",
")",
"\n",
"app",
".",
"Set",
"(",
"SetParseError",
",",
"func",
"(",
"err",
"error",
")",
"HTTPError",
"{",
"return",
"ParseError",
"(",
"err",
")",
"\n",
"}",
")",
"\n",
"app",
".",
"Set",
"(",
"SetOnError",
",",
"func",
"(",
"ctx",
"*",
"Context",
",",
"err",
"HTTPError",
")",
"{",
"ctx",
".",
"Error",
"(",
"err",
")",
"\n",
"}",
")",
"\n",
"return",
"app",
"\n",
"}"
] |
// New creates an instance of App.
|
[
"New",
"creates",
"an",
"instance",
"of",
"App",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L152-L175
|
18,359 |
teambition/gear
|
app.go
|
Use
|
func (app *App) Use(handle Middleware) *App {
app.mds = append(app.mds, handle)
return app
}
|
go
|
func (app *App) Use(handle Middleware) *App {
app.mds = append(app.mds, handle)
return app
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"Use",
"(",
"handle",
"Middleware",
")",
"*",
"App",
"{",
"app",
".",
"mds",
"=",
"append",
"(",
"app",
".",
"mds",
",",
"handle",
")",
"\n",
"return",
"app",
"\n",
"}"
] |
// Use uses the given middleware `handle`.
|
[
"Use",
"uses",
"the",
"given",
"middleware",
"handle",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L178-L181
|
18,360 |
teambition/gear
|
app.go
|
UseHandler
|
func (app *App) UseHandler(h Handler) *App {
app.mds = append(app.mds, h.Serve)
return app
}
|
go
|
func (app *App) UseHandler(h Handler) *App {
app.mds = append(app.mds, h.Serve)
return app
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"UseHandler",
"(",
"h",
"Handler",
")",
"*",
"App",
"{",
"app",
".",
"mds",
"=",
"append",
"(",
"app",
".",
"mds",
",",
"h",
".",
"Serve",
")",
"\n",
"return",
"app",
"\n",
"}"
] |
// UseHandler uses a instance that implemented Handler interface.
|
[
"UseHandler",
"uses",
"a",
"instance",
"that",
"implemented",
"Handler",
"interface",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L184-L187
|
18,361 |
teambition/gear
|
app.go
|
Listen
|
func (app *App) Listen(addr string) error {
app.Server.Addr = addr
app.Server.ErrorLog = app.logger
app.Server.Handler = app
return app.Server.ListenAndServe()
}
|
go
|
func (app *App) Listen(addr string) error {
app.Server.Addr = addr
app.Server.ErrorLog = app.logger
app.Server.Handler = app
return app.Server.ListenAndServe()
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"Listen",
"(",
"addr",
"string",
")",
"error",
"{",
"app",
".",
"Server",
".",
"Addr",
"=",
"addr",
"\n",
"app",
".",
"Server",
".",
"ErrorLog",
"=",
"app",
".",
"logger",
"\n",
"app",
".",
"Server",
".",
"Handler",
"=",
"app",
"\n",
"return",
"app",
".",
"Server",
".",
"ListenAndServe",
"(",
")",
"\n",
"}"
] |
// Listen starts the HTTP server.
|
[
"Listen",
"starts",
"the",
"HTTP",
"server",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L363-L368
|
18,362 |
teambition/gear
|
app.go
|
ListenTLS
|
func (app *App) ListenTLS(addr, certFile, keyFile string) error {
app.Server.Addr = addr
app.Server.ErrorLog = app.logger
app.Server.Handler = app
return app.Server.ListenAndServeTLS(certFile, keyFile)
}
|
go
|
func (app *App) ListenTLS(addr, certFile, keyFile string) error {
app.Server.Addr = addr
app.Server.ErrorLog = app.logger
app.Server.Handler = app
return app.Server.ListenAndServeTLS(certFile, keyFile)
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"ListenTLS",
"(",
"addr",
",",
"certFile",
",",
"keyFile",
"string",
")",
"error",
"{",
"app",
".",
"Server",
".",
"Addr",
"=",
"addr",
"\n",
"app",
".",
"Server",
".",
"ErrorLog",
"=",
"app",
".",
"logger",
"\n",
"app",
".",
"Server",
".",
"Handler",
"=",
"app",
"\n",
"return",
"app",
".",
"Server",
".",
"ListenAndServeTLS",
"(",
"certFile",
",",
"keyFile",
")",
"\n",
"}"
] |
// ListenTLS starts the HTTPS server.
|
[
"ListenTLS",
"starts",
"the",
"HTTPS",
"server",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L371-L376
|
18,363 |
teambition/gear
|
app.go
|
Error
|
func (app *App) Error(err interface{}) {
if err := ErrorWithStack(err, 4); err != nil {
str, e := err.Format()
f := app.logger.Flags() == 0
switch {
case f && e == nil:
app.logger.Printf("[%s] ERR %s\n", time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), str)
case f && e != nil:
app.logger.Printf("[%s] CRIT %s\n", time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), err.String())
case !f && e == nil:
app.logger.Printf("ERR %s\n", str)
default:
app.logger.Printf("CRIT %s\n", err.String())
}
}
}
|
go
|
func (app *App) Error(err interface{}) {
if err := ErrorWithStack(err, 4); err != nil {
str, e := err.Format()
f := app.logger.Flags() == 0
switch {
case f && e == nil:
app.logger.Printf("[%s] ERR %s\n", time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), str)
case f && e != nil:
app.logger.Printf("[%s] CRIT %s\n", time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), err.String())
case !f && e == nil:
app.logger.Printf("ERR %s\n", str)
default:
app.logger.Printf("CRIT %s\n", err.String())
}
}
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"Error",
"(",
"err",
"interface",
"{",
"}",
")",
"{",
"if",
"err",
":=",
"ErrorWithStack",
"(",
"err",
",",
"4",
")",
";",
"err",
"!=",
"nil",
"{",
"str",
",",
"e",
":=",
"err",
".",
"Format",
"(",
")",
"\n",
"f",
":=",
"app",
".",
"logger",
".",
"Flags",
"(",
")",
"==",
"0",
"\n",
"switch",
"{",
"case",
"f",
"&&",
"e",
"==",
"nil",
":",
"app",
".",
"logger",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"time",
".",
"Now",
"(",
")",
".",
"UTC",
"(",
")",
".",
"Format",
"(",
"\"",
"\"",
")",
",",
"str",
")",
"\n",
"case",
"f",
"&&",
"e",
"!=",
"nil",
":",
"app",
".",
"logger",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"time",
".",
"Now",
"(",
")",
".",
"UTC",
"(",
")",
".",
"Format",
"(",
"\"",
"\"",
")",
",",
"err",
".",
"String",
"(",
")",
")",
"\n",
"case",
"!",
"f",
"&&",
"e",
"==",
"nil",
":",
"app",
".",
"logger",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"str",
")",
"\n",
"default",
":",
"app",
".",
"logger",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"err",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Error writes error to underlayer logging system.
|
[
"Error",
"writes",
"error",
"to",
"underlayer",
"logging",
"system",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L402-L417
|
18,364 |
teambition/gear
|
app.go
|
Close
|
func (app *App) Close(ctx ...context.Context) error {
if len(ctx) > 0 {
return app.Server.Shutdown(ctx[0])
}
return app.Server.Close()
}
|
go
|
func (app *App) Close(ctx ...context.Context) error {
if len(ctx) > 0 {
return app.Server.Shutdown(ctx[0])
}
return app.Server.Close()
}
|
[
"func",
"(",
"app",
"*",
"App",
")",
"Close",
"(",
"ctx",
"...",
"context",
".",
"Context",
")",
"error",
"{",
"if",
"len",
"(",
"ctx",
")",
">",
"0",
"{",
"return",
"app",
".",
"Server",
".",
"Shutdown",
"(",
"ctx",
"[",
"0",
"]",
")",
"\n",
"}",
"\n",
"return",
"app",
".",
"Server",
".",
"Close",
"(",
")",
"\n",
"}"
] |
// Close closes the underlying server.
// If context omit, Server.Close will be used to close immediately.
// Otherwise Server.Shutdown will be used to close gracefully.
|
[
"Close",
"closes",
"the",
"underlying",
"server",
".",
"If",
"context",
"omit",
"Server",
".",
"Close",
"will",
"be",
"used",
"to",
"close",
"immediately",
".",
"Otherwise",
"Server",
".",
"Shutdown",
"will",
"be",
"used",
"to",
"close",
"gracefully",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/app.go#L468-L473
|
18,365 |
teambition/gear
|
util.go
|
Compose
|
func Compose(mds ...Middleware) Middleware {
switch len(mds) {
case 0:
return noOp
case 1:
return mds[0]
default:
return middlewares(mds).run
}
}
|
go
|
func Compose(mds ...Middleware) Middleware {
switch len(mds) {
case 0:
return noOp
case 1:
return mds[0]
default:
return middlewares(mds).run
}
}
|
[
"func",
"Compose",
"(",
"mds",
"...",
"Middleware",
")",
"Middleware",
"{",
"switch",
"len",
"(",
"mds",
")",
"{",
"case",
"0",
":",
"return",
"noOp",
"\n",
"case",
"1",
":",
"return",
"mds",
"[",
"0",
"]",
"\n",
"default",
":",
"return",
"middlewares",
"(",
"mds",
")",
".",
"run",
"\n",
"}",
"\n",
"}"
] |
// Compose composes a slice of middlewares to one middleware
|
[
"Compose",
"composes",
"a",
"slice",
"of",
"middlewares",
"to",
"one",
"middleware"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L35-L44
|
18,366 |
teambition/gear
|
util.go
|
WrapHandler
|
func WrapHandler(handler http.Handler) Middleware {
return func(ctx *Context) error {
handler.ServeHTTP(ctx.Res, ctx.Req)
return nil
}
}
|
go
|
func WrapHandler(handler http.Handler) Middleware {
return func(ctx *Context) error {
handler.ServeHTTP(ctx.Res, ctx.Req)
return nil
}
}
|
[
"func",
"WrapHandler",
"(",
"handler",
"http",
".",
"Handler",
")",
"Middleware",
"{",
"return",
"func",
"(",
"ctx",
"*",
"Context",
")",
"error",
"{",
"handler",
".",
"ServeHTTP",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// WrapHandler wrap a http.Handler to Gear Middleware
|
[
"WrapHandler",
"wrap",
"a",
"http",
".",
"Handler",
"to",
"Gear",
"Middleware"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L49-L54
|
18,367 |
teambition/gear
|
util.go
|
WrapHandlerFunc
|
func WrapHandlerFunc(fn http.HandlerFunc) Middleware {
return func(ctx *Context) error {
fn(ctx.Res, ctx.Req)
return nil
}
}
|
go
|
func WrapHandlerFunc(fn http.HandlerFunc) Middleware {
return func(ctx *Context) error {
fn(ctx.Res, ctx.Req)
return nil
}
}
|
[
"func",
"WrapHandlerFunc",
"(",
"fn",
"http",
".",
"HandlerFunc",
")",
"Middleware",
"{",
"return",
"func",
"(",
"ctx",
"*",
"Context",
")",
"error",
"{",
"fn",
"(",
"ctx",
".",
"Res",
",",
"ctx",
".",
"Req",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// WrapHandlerFunc wrap a http.HandlerFunc to Gear Middleware
|
[
"WrapHandlerFunc",
"wrap",
"a",
"http",
".",
"HandlerFunc",
"to",
"Gear",
"Middleware"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L57-L62
|
18,368 |
teambition/gear
|
util.go
|
Error
|
func (err *Error) Error() string {
return fmt.Sprintf("%s: %s", err.Err, err.Msg)
}
|
go
|
func (err *Error) Error() string {
return fmt.Sprintf("%s: %s", err.Err, err.Msg)
}
|
[
"func",
"(",
"err",
"*",
"Error",
")",
"Error",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"err",
".",
"Err",
",",
"err",
".",
"Msg",
")",
"\n",
"}"
] |
// Error implemented HTTPError interface.
|
[
"Error",
"implemented",
"HTTPError",
"interface",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L103-L105
|
18,369 |
teambition/gear
|
util.go
|
Format
|
func (err Error) Format() (string, error) {
errlog := errorForLog{err.Code, err.Err, err.Msg, err.Data, err.Stack}
res, e := json.Marshal(errlog)
if e == nil {
return string(res), nil
}
return "", e
}
|
go
|
func (err Error) Format() (string, error) {
errlog := errorForLog{err.Code, err.Err, err.Msg, err.Data, err.Stack}
res, e := json.Marshal(errlog)
if e == nil {
return string(res), nil
}
return "", e
}
|
[
"func",
"(",
"err",
"Error",
")",
"Format",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"errlog",
":=",
"errorForLog",
"{",
"err",
".",
"Code",
",",
"err",
".",
"Err",
",",
"err",
".",
"Msg",
",",
"err",
".",
"Data",
",",
"err",
".",
"Stack",
"}",
"\n",
"res",
",",
"e",
":=",
"json",
".",
"Marshal",
"(",
"errlog",
")",
"\n",
"if",
"e",
"==",
"nil",
"{",
"return",
"string",
"(",
"res",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
",",
"e",
"\n",
"}"
] |
// Format implemented logging.Messager interface.
|
[
"Format",
"implemented",
"logging",
".",
"Messager",
"interface",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L122-L129
|
18,370 |
teambition/gear
|
util.go
|
ParseError
|
func ParseError(e error, code ...int) HTTPError {
if IsNil(e) {
return nil
}
switch v := e.(type) {
case HTTPError:
return v
case *textproto.Error:
err := Err.WithCode(v.Code)
err.Msg = v.Msg
return err
default:
err := ErrInternalServerError.WithMsg(e.Error())
if len(code) > 0 && code[0] > 0 {
err = err.WithCode(code[0])
}
return err
}
}
|
go
|
func ParseError(e error, code ...int) HTTPError {
if IsNil(e) {
return nil
}
switch v := e.(type) {
case HTTPError:
return v
case *textproto.Error:
err := Err.WithCode(v.Code)
err.Msg = v.Msg
return err
default:
err := ErrInternalServerError.WithMsg(e.Error())
if len(code) > 0 && code[0] > 0 {
err = err.WithCode(code[0])
}
return err
}
}
|
[
"func",
"ParseError",
"(",
"e",
"error",
",",
"code",
"...",
"int",
")",
"HTTPError",
"{",
"if",
"IsNil",
"(",
"e",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"switch",
"v",
":=",
"e",
".",
"(",
"type",
")",
"{",
"case",
"HTTPError",
":",
"return",
"v",
"\n",
"case",
"*",
"textproto",
".",
"Error",
":",
"err",
":=",
"Err",
".",
"WithCode",
"(",
"v",
".",
"Code",
")",
"\n",
"err",
".",
"Msg",
"=",
"v",
".",
"Msg",
"\n",
"return",
"err",
"\n",
"default",
":",
"err",
":=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"e",
".",
"Error",
"(",
")",
")",
"\n",
"if",
"len",
"(",
"code",
")",
">",
"0",
"&&",
"code",
"[",
"0",
"]",
">",
"0",
"{",
"err",
"=",
"err",
".",
"WithCode",
"(",
"code",
"[",
"0",
"]",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}"
] |
// ParseError parse a error, textproto.Error or HTTPError to HTTPError
|
[
"ParseError",
"parse",
"a",
"error",
"textproto",
".",
"Error",
"or",
"HTTPError",
"to",
"HTTPError"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L199-L218
|
18,371 |
teambition/gear
|
util.go
|
ErrorWithStack
|
func ErrorWithStack(val interface{}, skip ...int) *Error {
if IsNil(val) {
return nil
}
var err *Error
switch v := val.(type) {
case *Error:
err = v.WithMsg() // must clone, should not change the origin *Error instance
case error:
err = ErrInternalServerError.From(v)
case string:
err = ErrInternalServerError.WithMsg(v)
default:
err = ErrInternalServerError.WithMsgf("%#v", v)
}
if err.Stack == "" {
buf := make([]byte, 2048)
buf = buf[:runtime.Stack(buf, false)]
s := 1
if len(skip) != 0 {
s = skip[0]
}
err.Stack = pruneStack(buf, s)
}
return err
}
|
go
|
func ErrorWithStack(val interface{}, skip ...int) *Error {
if IsNil(val) {
return nil
}
var err *Error
switch v := val.(type) {
case *Error:
err = v.WithMsg() // must clone, should not change the origin *Error instance
case error:
err = ErrInternalServerError.From(v)
case string:
err = ErrInternalServerError.WithMsg(v)
default:
err = ErrInternalServerError.WithMsgf("%#v", v)
}
if err.Stack == "" {
buf := make([]byte, 2048)
buf = buf[:runtime.Stack(buf, false)]
s := 1
if len(skip) != 0 {
s = skip[0]
}
err.Stack = pruneStack(buf, s)
}
return err
}
|
[
"func",
"ErrorWithStack",
"(",
"val",
"interface",
"{",
"}",
",",
"skip",
"...",
"int",
")",
"*",
"Error",
"{",
"if",
"IsNil",
"(",
"val",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"var",
"err",
"*",
"Error",
"\n",
"switch",
"v",
":=",
"val",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Error",
":",
"err",
"=",
"v",
".",
"WithMsg",
"(",
")",
"// must clone, should not change the origin *Error instance",
"\n",
"case",
"error",
":",
"err",
"=",
"ErrInternalServerError",
".",
"From",
"(",
"v",
")",
"\n",
"case",
"string",
":",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsg",
"(",
"v",
")",
"\n",
"default",
":",
"err",
"=",
"ErrInternalServerError",
".",
"WithMsgf",
"(",
"\"",
"\"",
",",
"v",
")",
"\n",
"}",
"\n\n",
"if",
"err",
".",
"Stack",
"==",
"\"",
"\"",
"{",
"buf",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"2048",
")",
"\n",
"buf",
"=",
"buf",
"[",
":",
"runtime",
".",
"Stack",
"(",
"buf",
",",
"false",
")",
"]",
"\n",
"s",
":=",
"1",
"\n",
"if",
"len",
"(",
"skip",
")",
"!=",
"0",
"{",
"s",
"=",
"skip",
"[",
"0",
"]",
"\n",
"}",
"\n",
"err",
".",
"Stack",
"=",
"pruneStack",
"(",
"buf",
",",
"s",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}"
] |
// ErrorWithStack create a error with stacktrace
|
[
"ErrorWithStack",
"create",
"a",
"error",
"with",
"stacktrace"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L221-L248
|
18,372 |
teambition/gear
|
util.go
|
Add
|
func (s *LoggerFilterWriter) Add(err string) {
if s.out == nil {
panic(Err.WithMsg("output io.Writer should be set with SetOutput method"))
}
s.phrases = append(s.phrases, []byte(err))
}
|
go
|
func (s *LoggerFilterWriter) Add(err string) {
if s.out == nil {
panic(Err.WithMsg("output io.Writer should be set with SetOutput method"))
}
s.phrases = append(s.phrases, []byte(err))
}
|
[
"func",
"(",
"s",
"*",
"LoggerFilterWriter",
")",
"Add",
"(",
"err",
"string",
")",
"{",
"if",
"s",
".",
"out",
"==",
"nil",
"{",
"panic",
"(",
"Err",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"s",
".",
"phrases",
"=",
"append",
"(",
"s",
".",
"phrases",
",",
"[",
"]",
"byte",
"(",
"err",
")",
")",
"\n",
"}"
] |
// Add add a phrase string to filter
|
[
"Add",
"add",
"a",
"phrase",
"string",
"to",
"filter"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L582-L587
|
18,373 |
teambition/gear
|
util.go
|
Decompress
|
func Decompress(encoding string, r io.Reader) (io.ReadCloser, error) {
switch encoding {
case "gzip":
return gzip.NewReader(r)
case "deflate", "zlib":
// compatible for RFC 1950 zlib, RFC 1951 deflate, http://www.open-open.com/lib/view/open1460866410410.html
return zlib.NewReader(r)
default:
return nil, ErrUnsupportedMediaType.WithMsgf("Unsupported Content-Encoding: %s", encoding)
}
}
|
go
|
func Decompress(encoding string, r io.Reader) (io.ReadCloser, error) {
switch encoding {
case "gzip":
return gzip.NewReader(r)
case "deflate", "zlib":
// compatible for RFC 1950 zlib, RFC 1951 deflate, http://www.open-open.com/lib/view/open1460866410410.html
return zlib.NewReader(r)
default:
return nil, ErrUnsupportedMediaType.WithMsgf("Unsupported Content-Encoding: %s", encoding)
}
}
|
[
"func",
"Decompress",
"(",
"encoding",
"string",
",",
"r",
"io",
".",
"Reader",
")",
"(",
"io",
".",
"ReadCloser",
",",
"error",
")",
"{",
"switch",
"encoding",
"{",
"case",
"\"",
"\"",
":",
"return",
"gzip",
".",
"NewReader",
"(",
"r",
")",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// compatible for RFC 1950 zlib, RFC 1951 deflate, http://www.open-open.com/lib/view/open1460866410410.html",
"return",
"zlib",
".",
"NewReader",
"(",
"r",
")",
"\n",
"default",
":",
"return",
"nil",
",",
"ErrUnsupportedMediaType",
".",
"WithMsgf",
"(",
"\"",
"\"",
",",
"encoding",
")",
"\n",
"}",
"\n",
"}"
] |
// Decompress wrap the reader for decompressing, It support gzip and zlib, and compatible for deflate.
|
[
"Decompress",
"wrap",
"the",
"reader",
"for",
"decompressing",
"It",
"support",
"gzip",
"and",
"zlib",
"and",
"compatible",
"for",
"deflate",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/util.go#L600-L610
|
18,374 |
teambition/gear
|
middleware/cors/cors.go
|
New
|
func New(options ...Options) gear.Middleware {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}
if opts.AllowOrigins == nil {
opts.AllowOrigins = defaultAllowOrigins
}
if opts.AllowMethods == nil {
opts.AllowMethods = defaultAllowMethods
}
if opts.AllowOriginsValidator == nil {
opts.AllowOriginsValidator = func(origin string, _ *gear.Context) (allowOrigin string) {
for _, o := range opts.AllowOrigins {
if o == origin || o == "*" {
allowOrigin = origin
break
}
}
return
}
}
return func(ctx *gear.Context) (err error) {
// Always set Vary, see https://github.com/rs/cors/issues/10
ctx.Res.Vary(gear.HeaderOrigin)
origin := ctx.GetHeader(gear.HeaderOrigin)
// not a CORS request
if origin == "" {
return
}
allowOrigin := opts.AllowOriginsValidator(origin, ctx)
if allowOrigin == "" {
// If the request Origin header is not allowed. Just terminate the following steps.
if ctx.Method == http.MethodOptions {
return ctx.End(http.StatusOK)
}
return
}
ctx.SetHeader(gear.HeaderAccessControlAllowOrigin, allowOrigin)
if opts.Credentials {
// when responding to a credentialed request, server must specify a
// domain, and cannot use wild carding.
// See *important note* in https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials .
ctx.SetHeader(gear.HeaderAccessControlAllowCredentials, "true")
}
// Handle preflighted requests (https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests) .
// https://stackoverflow.com/questions/46026409/what-are-proper-status-codes-for-cors-preflight-requests
if ctx.Method == http.MethodOptions {
ctx.Res.Vary(gear.HeaderAccessControlRequestMethod)
ctx.Res.Vary(gear.HeaderAccessControlRequestHeaders)
requestMethod := ctx.GetHeader(gear.HeaderAccessControlRequestMethod)
// If there is no "Access-Control-Request-Method" request header. We just
// treat this request as an invalid preflighted request, so terminate the
// following steps.
if requestMethod == "" {
ctx.Res.Del(gear.HeaderAccessControlAllowOrigin)
ctx.Res.Del(gear.HeaderAccessControlAllowCredentials)
return ctx.End(http.StatusOK)
}
if len(opts.AllowMethods) > 0 {
ctx.SetHeader(gear.HeaderAccessControlAllowMethods, strings.Join(opts.AllowMethods, ", "))
}
var allowHeaders string
if len(opts.AllowHeaders) > 0 {
allowHeaders = strings.Join(opts.AllowHeaders, ", ")
} else {
allowHeaders = ctx.GetHeader(gear.HeaderAccessControlRequestHeaders)
}
if allowHeaders != "" {
ctx.SetHeader(gear.HeaderAccessControlAllowHeaders, allowHeaders)
}
if opts.MaxAge > 0 {
ctx.SetHeader(gear.HeaderAccessControlMaxAge, strconv.Itoa(int(opts.MaxAge.Seconds())))
}
return ctx.End(http.StatusOK)
}
if len(opts.ExposeHeaders) > 0 {
ctx.SetHeader(gear.HeaderAccessControlExposeHeaders, strings.Join(opts.ExposeHeaders, ", "))
}
return
}
}
|
go
|
func New(options ...Options) gear.Middleware {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}
if opts.AllowOrigins == nil {
opts.AllowOrigins = defaultAllowOrigins
}
if opts.AllowMethods == nil {
opts.AllowMethods = defaultAllowMethods
}
if opts.AllowOriginsValidator == nil {
opts.AllowOriginsValidator = func(origin string, _ *gear.Context) (allowOrigin string) {
for _, o := range opts.AllowOrigins {
if o == origin || o == "*" {
allowOrigin = origin
break
}
}
return
}
}
return func(ctx *gear.Context) (err error) {
// Always set Vary, see https://github.com/rs/cors/issues/10
ctx.Res.Vary(gear.HeaderOrigin)
origin := ctx.GetHeader(gear.HeaderOrigin)
// not a CORS request
if origin == "" {
return
}
allowOrigin := opts.AllowOriginsValidator(origin, ctx)
if allowOrigin == "" {
// If the request Origin header is not allowed. Just terminate the following steps.
if ctx.Method == http.MethodOptions {
return ctx.End(http.StatusOK)
}
return
}
ctx.SetHeader(gear.HeaderAccessControlAllowOrigin, allowOrigin)
if opts.Credentials {
// when responding to a credentialed request, server must specify a
// domain, and cannot use wild carding.
// See *important note* in https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials .
ctx.SetHeader(gear.HeaderAccessControlAllowCredentials, "true")
}
// Handle preflighted requests (https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests) .
// https://stackoverflow.com/questions/46026409/what-are-proper-status-codes-for-cors-preflight-requests
if ctx.Method == http.MethodOptions {
ctx.Res.Vary(gear.HeaderAccessControlRequestMethod)
ctx.Res.Vary(gear.HeaderAccessControlRequestHeaders)
requestMethod := ctx.GetHeader(gear.HeaderAccessControlRequestMethod)
// If there is no "Access-Control-Request-Method" request header. We just
// treat this request as an invalid preflighted request, so terminate the
// following steps.
if requestMethod == "" {
ctx.Res.Del(gear.HeaderAccessControlAllowOrigin)
ctx.Res.Del(gear.HeaderAccessControlAllowCredentials)
return ctx.End(http.StatusOK)
}
if len(opts.AllowMethods) > 0 {
ctx.SetHeader(gear.HeaderAccessControlAllowMethods, strings.Join(opts.AllowMethods, ", "))
}
var allowHeaders string
if len(opts.AllowHeaders) > 0 {
allowHeaders = strings.Join(opts.AllowHeaders, ", ")
} else {
allowHeaders = ctx.GetHeader(gear.HeaderAccessControlRequestHeaders)
}
if allowHeaders != "" {
ctx.SetHeader(gear.HeaderAccessControlAllowHeaders, allowHeaders)
}
if opts.MaxAge > 0 {
ctx.SetHeader(gear.HeaderAccessControlMaxAge, strconv.Itoa(int(opts.MaxAge.Seconds())))
}
return ctx.End(http.StatusOK)
}
if len(opts.ExposeHeaders) > 0 {
ctx.SetHeader(gear.HeaderAccessControlExposeHeaders, strings.Join(opts.ExposeHeaders, ", "))
}
return
}
}
|
[
"func",
"New",
"(",
"options",
"...",
"Options",
")",
"gear",
".",
"Middleware",
"{",
"opts",
":=",
"Options",
"{",
"}",
"\n",
"if",
"len",
"(",
"options",
")",
">",
"0",
"{",
"opts",
"=",
"options",
"[",
"0",
"]",
"\n",
"}",
"\n",
"if",
"opts",
".",
"AllowOrigins",
"==",
"nil",
"{",
"opts",
".",
"AllowOrigins",
"=",
"defaultAllowOrigins",
"\n",
"}",
"\n",
"if",
"opts",
".",
"AllowMethods",
"==",
"nil",
"{",
"opts",
".",
"AllowMethods",
"=",
"defaultAllowMethods",
"\n",
"}",
"\n",
"if",
"opts",
".",
"AllowOriginsValidator",
"==",
"nil",
"{",
"opts",
".",
"AllowOriginsValidator",
"=",
"func",
"(",
"origin",
"string",
",",
"_",
"*",
"gear",
".",
"Context",
")",
"(",
"allowOrigin",
"string",
")",
"{",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
".",
"AllowOrigins",
"{",
"if",
"o",
"==",
"origin",
"||",
"o",
"==",
"\"",
"\"",
"{",
"allowOrigin",
"=",
"origin",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"func",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"(",
"err",
"error",
")",
"{",
"// Always set Vary, see https://github.com/rs/cors/issues/10",
"ctx",
".",
"Res",
".",
"Vary",
"(",
"gear",
".",
"HeaderOrigin",
")",
"\n\n",
"origin",
":=",
"ctx",
".",
"GetHeader",
"(",
"gear",
".",
"HeaderOrigin",
")",
"\n",
"// not a CORS request",
"if",
"origin",
"==",
"\"",
"\"",
"{",
"return",
"\n",
"}",
"\n\n",
"allowOrigin",
":=",
"opts",
".",
"AllowOriginsValidator",
"(",
"origin",
",",
"ctx",
")",
"\n",
"if",
"allowOrigin",
"==",
"\"",
"\"",
"{",
"// If the request Origin header is not allowed. Just terminate the following steps.",
"if",
"ctx",
".",
"Method",
"==",
"http",
".",
"MethodOptions",
"{",
"return",
"ctx",
".",
"End",
"(",
"http",
".",
"StatusOK",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}",
"\n\n",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlAllowOrigin",
",",
"allowOrigin",
")",
"\n",
"if",
"opts",
".",
"Credentials",
"{",
"// when responding to a credentialed request, server must specify a",
"// domain, and cannot use wild carding.",
"// See *important note* in https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials .",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlAllowCredentials",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// Handle preflighted requests (https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests) .",
"// https://stackoverflow.com/questions/46026409/what-are-proper-status-codes-for-cors-preflight-requests",
"if",
"ctx",
".",
"Method",
"==",
"http",
".",
"MethodOptions",
"{",
"ctx",
".",
"Res",
".",
"Vary",
"(",
"gear",
".",
"HeaderAccessControlRequestMethod",
")",
"\n",
"ctx",
".",
"Res",
".",
"Vary",
"(",
"gear",
".",
"HeaderAccessControlRequestHeaders",
")",
"\n\n",
"requestMethod",
":=",
"ctx",
".",
"GetHeader",
"(",
"gear",
".",
"HeaderAccessControlRequestMethod",
")",
"\n",
"// If there is no \"Access-Control-Request-Method\" request header. We just",
"// treat this request as an invalid preflighted request, so terminate the",
"// following steps.",
"if",
"requestMethod",
"==",
"\"",
"\"",
"{",
"ctx",
".",
"Res",
".",
"Del",
"(",
"gear",
".",
"HeaderAccessControlAllowOrigin",
")",
"\n",
"ctx",
".",
"Res",
".",
"Del",
"(",
"gear",
".",
"HeaderAccessControlAllowCredentials",
")",
"\n",
"return",
"ctx",
".",
"End",
"(",
"http",
".",
"StatusOK",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"opts",
".",
"AllowMethods",
")",
">",
"0",
"{",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlAllowMethods",
",",
"strings",
".",
"Join",
"(",
"opts",
".",
"AllowMethods",
",",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n\n",
"var",
"allowHeaders",
"string",
"\n",
"if",
"len",
"(",
"opts",
".",
"AllowHeaders",
")",
">",
"0",
"{",
"allowHeaders",
"=",
"strings",
".",
"Join",
"(",
"opts",
".",
"AllowHeaders",
",",
"\"",
"\"",
")",
"\n",
"}",
"else",
"{",
"allowHeaders",
"=",
"ctx",
".",
"GetHeader",
"(",
"gear",
".",
"HeaderAccessControlRequestHeaders",
")",
"\n",
"}",
"\n",
"if",
"allowHeaders",
"!=",
"\"",
"\"",
"{",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlAllowHeaders",
",",
"allowHeaders",
")",
"\n",
"}",
"\n\n",
"if",
"opts",
".",
"MaxAge",
">",
"0",
"{",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlMaxAge",
",",
"strconv",
".",
"Itoa",
"(",
"int",
"(",
"opts",
".",
"MaxAge",
".",
"Seconds",
"(",
")",
")",
")",
")",
"\n",
"}",
"\n",
"return",
"ctx",
".",
"End",
"(",
"http",
".",
"StatusOK",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"opts",
".",
"ExposeHeaders",
")",
">",
"0",
"{",
"ctx",
".",
"SetHeader",
"(",
"gear",
".",
"HeaderAccessControlExposeHeaders",
",",
"strings",
".",
"Join",
"(",
"opts",
".",
"ExposeHeaders",
",",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}",
"\n",
"}"
] |
// New creates a middleware to provide CORS support for gear.
|
[
"New",
"creates",
"a",
"middleware",
"to",
"provide",
"CORS",
"support",
"for",
"gear",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/middleware/cors/cors.go#L52-L142
|
18,375 |
teambition/gear
|
logging/logger.go
|
Format
|
func (l Log) Format() (string, error) {
res, err := json.Marshal(l)
if err == nil {
return string(res), nil
}
return "", err
}
|
go
|
func (l Log) Format() (string, error) {
res, err := json.Marshal(l)
if err == nil {
return string(res), nil
}
return "", err
}
|
[
"func",
"(",
"l",
"Log",
")",
"Format",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"res",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"l",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"string",
"(",
"res",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}"
] |
// Format try to marshal the structured log with json.Marshal.
|
[
"Format",
"try",
"to",
"marshal",
"the",
"structured",
"log",
"with",
"json",
".",
"Marshal",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L30-L36
|
18,376 |
teambition/gear
|
logging/logger.go
|
ParseLevel
|
func ParseLevel(lvl string) (Level, error) {
switch strings.ToUpper(lvl) {
case "EMERGENCY", "EMERG":
return EmergLevel, nil
case "ALERT":
return AlertLevel, nil
case "CRITICAL", "CRIT", "CRITI":
return CritiLevel, nil
case "ERROR", "ERR":
return ErrLevel, nil
case "WARNING", "WARN":
return WarningLevel, nil
case "NOTICE":
return NoticeLevel, nil
case "INFO":
return InfoLevel, nil
case "DEBUG":
return DebugLevel, nil
}
var l Level
return l, fmt.Errorf("not a valid gear logging Level: %q", lvl)
}
|
go
|
func ParseLevel(lvl string) (Level, error) {
switch strings.ToUpper(lvl) {
case "EMERGENCY", "EMERG":
return EmergLevel, nil
case "ALERT":
return AlertLevel, nil
case "CRITICAL", "CRIT", "CRITI":
return CritiLevel, nil
case "ERROR", "ERR":
return ErrLevel, nil
case "WARNING", "WARN":
return WarningLevel, nil
case "NOTICE":
return NoticeLevel, nil
case "INFO":
return InfoLevel, nil
case "DEBUG":
return DebugLevel, nil
}
var l Level
return l, fmt.Errorf("not a valid gear logging Level: %q", lvl)
}
|
[
"func",
"ParseLevel",
"(",
"lvl",
"string",
")",
"(",
"Level",
",",
"error",
")",
"{",
"switch",
"strings",
".",
"ToUpper",
"(",
"lvl",
")",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"return",
"EmergLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
":",
"return",
"AlertLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
":",
"return",
"CritiLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"return",
"ErrLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"return",
"WarningLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
":",
"return",
"NoticeLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
":",
"return",
"InfoLevel",
",",
"nil",
"\n",
"case",
"\"",
"\"",
":",
"return",
"DebugLevel",
",",
"nil",
"\n",
"}",
"\n\n",
"var",
"l",
"Level",
"\n",
"return",
"l",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"lvl",
")",
"\n",
"}"
] |
// ParseLevel takes a string level and returns the gear logging level constant.
|
[
"ParseLevel",
"takes",
"a",
"string",
"level",
"and",
"returns",
"the",
"gear",
"logging",
"level",
"constant",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L151-L173
|
18,377 |
teambition/gear
|
logging/logger.go
|
Default
|
func Default(devMode ...bool) *Logger {
if len(devMode) > 0 && devMode[0] {
std.SetLogConsume(developmentConsume)
}
return std
}
|
go
|
func Default(devMode ...bool) *Logger {
if len(devMode) > 0 && devMode[0] {
std.SetLogConsume(developmentConsume)
}
return std
}
|
[
"func",
"Default",
"(",
"devMode",
"...",
"bool",
")",
"*",
"Logger",
"{",
"if",
"len",
"(",
"devMode",
")",
">",
"0",
"&&",
"devMode",
"[",
"0",
"]",
"{",
"std",
".",
"SetLogConsume",
"(",
"developmentConsume",
")",
"\n",
"}",
"\n",
"return",
"std",
"\n",
"}"
] |
// Default returns the default logger
// If devMode is true, logger will print a simple version of Common Log Format with terminal color
|
[
"Default",
"returns",
"the",
"default",
"logger",
"If",
"devMode",
"is",
"true",
"logger",
"will",
"print",
"a",
"simple",
"version",
"of",
"Common",
"Log",
"Format",
"with",
"terminal",
"color"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L179-L184
|
18,378 |
teambition/gear
|
logging/logger.go
|
Emerg
|
func (l *Logger) Emerg(v interface{}) {
l.Output(time.Now(), EmergLevel, formatError(v))
}
|
go
|
func (l *Logger) Emerg(v interface{}) {
l.Output(time.Now(), EmergLevel, formatError(v))
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Emerg",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"EmergLevel",
",",
"formatError",
"(",
"v",
")",
")",
"\n",
"}"
] |
// Emerg produce a "Emergency" log
|
[
"Emerg",
"produce",
"a",
"Emergency",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L313-L315
|
18,379 |
teambition/gear
|
logging/logger.go
|
Alert
|
func (l *Logger) Alert(v interface{}) {
if l.checkLogLevel(AlertLevel) {
l.Output(time.Now(), AlertLevel, formatError(v))
}
}
|
go
|
func (l *Logger) Alert(v interface{}) {
if l.checkLogLevel(AlertLevel) {
l.Output(time.Now(), AlertLevel, formatError(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Alert",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"AlertLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"AlertLevel",
",",
"formatError",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Alert produce a "Alert" log
|
[
"Alert",
"produce",
"a",
"Alert",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L318-L322
|
18,380 |
teambition/gear
|
logging/logger.go
|
Crit
|
func (l *Logger) Crit(v interface{}) {
if l.checkLogLevel(CritiLevel) {
l.Output(time.Now(), CritiLevel, formatError(v))
}
}
|
go
|
func (l *Logger) Crit(v interface{}) {
if l.checkLogLevel(CritiLevel) {
l.Output(time.Now(), CritiLevel, formatError(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Crit",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"CritiLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"CritiLevel",
",",
"formatError",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Crit produce a "Critical" log
|
[
"Crit",
"produce",
"a",
"Critical",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L325-L329
|
18,381 |
teambition/gear
|
logging/logger.go
|
Err
|
func (l *Logger) Err(v interface{}) {
if l.checkLogLevel(ErrLevel) {
l.Output(time.Now(), ErrLevel, formatError(v))
}
}
|
go
|
func (l *Logger) Err(v interface{}) {
if l.checkLogLevel(ErrLevel) {
l.Output(time.Now(), ErrLevel, formatError(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Err",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"ErrLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"ErrLevel",
",",
"formatError",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Err produce a "Error" log
|
[
"Err",
"produce",
"a",
"Error",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L332-L336
|
18,382 |
teambition/gear
|
logging/logger.go
|
Warning
|
func (l *Logger) Warning(v interface{}) {
if l.checkLogLevel(WarningLevel) {
l.Output(time.Now(), WarningLevel, format(v))
}
}
|
go
|
func (l *Logger) Warning(v interface{}) {
if l.checkLogLevel(WarningLevel) {
l.Output(time.Now(), WarningLevel, format(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Warning",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"WarningLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"WarningLevel",
",",
"format",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Warning produce a "Warning" log
|
[
"Warning",
"produce",
"a",
"Warning",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L339-L343
|
18,383 |
teambition/gear
|
logging/logger.go
|
Notice
|
func (l *Logger) Notice(v interface{}) {
if l.checkLogLevel(NoticeLevel) {
l.Output(time.Now(), NoticeLevel, format(v))
}
}
|
go
|
func (l *Logger) Notice(v interface{}) {
if l.checkLogLevel(NoticeLevel) {
l.Output(time.Now(), NoticeLevel, format(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Notice",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"NoticeLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"NoticeLevel",
",",
"format",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Notice produce a "Notice" log
|
[
"Notice",
"produce",
"a",
"Notice",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L346-L350
|
18,384 |
teambition/gear
|
logging/logger.go
|
Info
|
func (l *Logger) Info(v interface{}) {
if l.checkLogLevel(InfoLevel) {
l.Output(time.Now(), InfoLevel, format(v))
}
}
|
go
|
func (l *Logger) Info(v interface{}) {
if l.checkLogLevel(InfoLevel) {
l.Output(time.Now(), InfoLevel, format(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Info",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"InfoLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"InfoLevel",
",",
"format",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Info produce a "Informational" log
|
[
"Info",
"produce",
"a",
"Informational",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L353-L357
|
18,385 |
teambition/gear
|
logging/logger.go
|
Debug
|
func (l *Logger) Debug(v interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, format(v))
}
}
|
go
|
func (l *Logger) Debug(v interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, format(v))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Debug",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"DebugLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"DebugLevel",
",",
"format",
"(",
"v",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Debug produce a "Debug" log
|
[
"Debug",
"produce",
"a",
"Debug",
"log"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L360-L364
|
18,386 |
teambition/gear
|
logging/logger.go
|
Debugf
|
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, fmt.Sprintf(format, args...))
}
}
|
go
|
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, fmt.Sprintf(format, args...))
}
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Debugf",
"(",
"format",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"{",
"if",
"l",
".",
"checkLogLevel",
"(",
"DebugLevel",
")",
"{",
"l",
".",
"Output",
"(",
"time",
".",
"Now",
"(",
")",
",",
"DebugLevel",
",",
"fmt",
".",
"Sprintf",
"(",
"format",
",",
"args",
"...",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// Debugf produce a "Debug" log in the manner of fmt.Printf
|
[
"Debugf",
"produce",
"a",
"Debug",
"log",
"in",
"the",
"manner",
"of",
"fmt",
".",
"Printf"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L367-L371
|
18,387 |
teambition/gear
|
logging/logger.go
|
Panic
|
func (l *Logger) Panic(v interface{}) {
s := format(v)
l.Emerg(s)
panic(s)
}
|
go
|
func (l *Logger) Panic(v interface{}) {
s := format(v)
l.Emerg(s)
panic(s)
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Panic",
"(",
"v",
"interface",
"{",
"}",
")",
"{",
"s",
":=",
"format",
"(",
"v",
")",
"\n",
"l",
".",
"Emerg",
"(",
"s",
")",
"\n",
"panic",
"(",
"s",
")",
"\n",
"}"
] |
// Panic produce a "Emergency" log and then calls panic with the message
|
[
"Panic",
"produce",
"a",
"Emergency",
"log",
"and",
"then",
"calls",
"panic",
"with",
"the",
"message"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L374-L378
|
18,388 |
teambition/gear
|
logging/logger.go
|
Print
|
func (l *Logger) Print(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.Out, args...)
}
|
go
|
func (l *Logger) Print(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.Out, args...)
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Print",
"(",
"args",
"...",
"interface",
"{",
"}",
")",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"fmt",
".",
"Fprint",
"(",
"l",
".",
"Out",
",",
"args",
"...",
")",
"\n",
"}"
] |
// Print produce a log in the manner of fmt.Print, without timestamp and log level
|
[
"Print",
"produce",
"a",
"log",
"in",
"the",
"manner",
"of",
"fmt",
".",
"Print",
"without",
"timestamp",
"and",
"log",
"level"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L389-L393
|
18,389 |
teambition/gear
|
logging/logger.go
|
Printf
|
func (l *Logger) Printf(format string, args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintf(l.Out, format, args...)
}
|
go
|
func (l *Logger) Printf(format string, args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintf(l.Out, format, args...)
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Printf",
"(",
"format",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"l",
".",
"Out",
",",
"format",
",",
"args",
"...",
")",
"\n",
"}"
] |
// Printf produce a log in the manner of fmt.Printf, without timestamp and log level
|
[
"Printf",
"produce",
"a",
"log",
"in",
"the",
"manner",
"of",
"fmt",
".",
"Printf",
"without",
"timestamp",
"and",
"log",
"level"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L396-L400
|
18,390 |
teambition/gear
|
logging/logger.go
|
Println
|
func (l *Logger) Println(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintln(l.Out, args...)
}
|
go
|
func (l *Logger) Println(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintln(l.Out, args...)
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Println",
"(",
"args",
"...",
"interface",
"{",
"}",
")",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"l",
".",
"Out",
",",
"args",
"...",
")",
"\n",
"}"
] |
// Println produce a log in the manner of fmt.Println, without timestamp and log level
|
[
"Println",
"produce",
"a",
"log",
"in",
"the",
"manner",
"of",
"fmt",
".",
"Println",
"without",
"timestamp",
"and",
"log",
"level"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L403-L407
|
18,391 |
teambition/gear
|
logging/logger.go
|
Output
|
func (l *Logger) Output(t time.Time, level Level, s string) (err error) {
l.mu.Lock()
defer l.mu.Unlock()
if l := len(s); l > 0 && s[l-1] == '\n' {
s = s[0 : l-1]
}
_, err = fmt.Fprintf(l.Out, l.lf, t.UTC().Format(l.tf), level.String(), crlfEscaper.Replace(s))
if err == nil {
l.Out.Write([]byte{'\n'})
}
return
}
|
go
|
func (l *Logger) Output(t time.Time, level Level, s string) (err error) {
l.mu.Lock()
defer l.mu.Unlock()
if l := len(s); l > 0 && s[l-1] == '\n' {
s = s[0 : l-1]
}
_, err = fmt.Fprintf(l.Out, l.lf, t.UTC().Format(l.tf), level.String(), crlfEscaper.Replace(s))
if err == nil {
l.Out.Write([]byte{'\n'})
}
return
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"Output",
"(",
"t",
"time",
".",
"Time",
",",
"level",
"Level",
",",
"s",
"string",
")",
"(",
"err",
"error",
")",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"l",
":=",
"len",
"(",
"s",
")",
";",
"l",
">",
"0",
"&&",
"s",
"[",
"l",
"-",
"1",
"]",
"==",
"'\\n'",
"{",
"s",
"=",
"s",
"[",
"0",
":",
"l",
"-",
"1",
"]",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"fmt",
".",
"Fprintf",
"(",
"l",
".",
"Out",
",",
"l",
".",
"lf",
",",
"t",
".",
"UTC",
"(",
")",
".",
"Format",
"(",
"l",
".",
"tf",
")",
",",
"level",
".",
"String",
"(",
")",
",",
"crlfEscaper",
".",
"Replace",
"(",
"s",
")",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"l",
".",
"Out",
".",
"Write",
"(",
"[",
"]",
"byte",
"{",
"'\\n'",
"}",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Output writes a string log with timestamp and log level to the output.
// If the level is greater than logger level, the log will be omitted.
// The log will be format by timeFormat and logFormat.
|
[
"Output",
"writes",
"a",
"string",
"log",
"with",
"timestamp",
"and",
"log",
"level",
"to",
"the",
"output",
".",
"If",
"the",
"level",
"is",
"greater",
"than",
"logger",
"level",
"the",
"log",
"will",
"be",
"omitted",
".",
"The",
"log",
"will",
"be",
"format",
"by",
"timeFormat",
"and",
"logFormat",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L412-L424
|
18,392 |
teambition/gear
|
logging/logger.go
|
GetLevel
|
func (l *Logger) GetLevel() Level {
l.mu.Lock()
defer l.mu.Unlock()
return l.l
}
|
go
|
func (l *Logger) GetLevel() Level {
l.mu.Lock()
defer l.mu.Unlock()
return l.l
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"GetLevel",
"(",
")",
"Level",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"return",
"l",
".",
"l",
"\n",
"}"
] |
// GetLevel get the logger's log level
|
[
"GetLevel",
"get",
"the",
"logger",
"s",
"log",
"level"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L427-L431
|
18,393 |
teambition/gear
|
logging/logger.go
|
SetLevel
|
func (l *Logger) SetLevel(level Level) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
if level > DebugLevel {
panic(gear.Err.WithMsg("invalid logger level"))
}
l.l = level
return l
}
|
go
|
func (l *Logger) SetLevel(level Level) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
if level > DebugLevel {
panic(gear.Err.WithMsg("invalid logger level"))
}
l.l = level
return l
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"SetLevel",
"(",
"level",
"Level",
")",
"*",
"Logger",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"if",
"level",
">",
"DebugLevel",
"{",
"panic",
"(",
"gear",
".",
"Err",
".",
"WithMsg",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"l",
".",
"l",
"=",
"level",
"\n",
"return",
"l",
"\n",
"}"
] |
// SetLevel set the logger's log level
// The default logger level is DebugLevel
|
[
"SetLevel",
"set",
"the",
"logger",
"s",
"log",
"level",
"The",
"default",
"logger",
"level",
"is",
"DebugLevel"
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L435-L443
|
18,394 |
teambition/gear
|
logging/logger.go
|
SetLogInit
|
func (l *Logger) SetLogInit(fn func(Log, *gear.Context)) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.init = fn
return l
}
|
go
|
func (l *Logger) SetLogInit(fn func(Log, *gear.Context)) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.init = fn
return l
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"SetLogInit",
"(",
"fn",
"func",
"(",
"Log",
",",
"*",
"gear",
".",
"Context",
")",
")",
"*",
"Logger",
"{",
"l",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"l",
".",
"init",
"=",
"fn",
"\n",
"return",
"l",
"\n",
"}"
] |
// SetLogInit set a log init handle to the logger.
// It will be called when log created.
|
[
"SetLogInit",
"set",
"a",
"log",
"init",
"handle",
"to",
"the",
"logger",
".",
"It",
"will",
"be",
"called",
"when",
"log",
"created",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L466-L471
|
18,395 |
teambition/gear
|
logging/logger.go
|
New
|
func (l *Logger) New(ctx *gear.Context) (interface{}, error) {
log := Log{}
l.init(log, ctx)
return log, nil
}
|
go
|
func (l *Logger) New(ctx *gear.Context) (interface{}, error) {
log := Log{}
l.init(log, ctx)
return log, nil
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"New",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"log",
":=",
"Log",
"{",
"}",
"\n",
"l",
".",
"init",
"(",
"log",
",",
"ctx",
")",
"\n",
"return",
"log",
",",
"nil",
"\n",
"}"
] |
// New implements gear.Any interface,then we can use ctx.Any to retrieve a Log instance from ctx.
// Here also some initialization work after created.
|
[
"New",
"implements",
"gear",
".",
"Any",
"interface",
"then",
"we",
"can",
"use",
"ctx",
".",
"Any",
"to",
"retrieve",
"a",
"Log",
"instance",
"from",
"ctx",
".",
"Here",
"also",
"some",
"initialization",
"work",
"after",
"created",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L489-L493
|
18,396 |
teambition/gear
|
logging/logger.go
|
FromCtx
|
func (l *Logger) FromCtx(ctx *gear.Context) Log {
any, _ := ctx.Any(l)
return any.(Log)
}
|
go
|
func (l *Logger) FromCtx(ctx *gear.Context) Log {
any, _ := ctx.Any(l)
return any.(Log)
}
|
[
"func",
"(",
"l",
"*",
"Logger",
")",
"FromCtx",
"(",
"ctx",
"*",
"gear",
".",
"Context",
")",
"Log",
"{",
"any",
",",
"_",
":=",
"ctx",
".",
"Any",
"(",
"l",
")",
"\n",
"return",
"any",
".",
"(",
"Log",
")",
"\n",
"}"
] |
// FromCtx retrieve the Log instance from the ctx with ctx.Any.
// Logger.New and ctx.Any will guarantee it exists.
|
[
"FromCtx",
"retrieve",
"the",
"Log",
"instance",
"from",
"the",
"ctx",
"with",
"ctx",
".",
"Any",
".",
"Logger",
".",
"New",
"and",
"ctx",
".",
"Any",
"will",
"guarantee",
"it",
"exists",
"."
] |
a2c5175e04298aa0918a50d40e95475520e9d78e
|
https://github.com/teambition/gear/blob/a2c5175e04298aa0918a50d40e95475520e9d78e/logging/logger.go#L497-L500
|
18,397 |
ns3777k/go-shodan
|
shodan/datasets.go
|
GetDatasets
|
func (c *Client) GetDatasets(ctx context.Context) ([]*Dataset, error) {
datasets := make([]*Dataset, 0)
req, err := c.NewRequest("GET", datasetsPath, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &datasets); err != nil {
return nil, err
}
return datasets, nil
}
|
go
|
func (c *Client) GetDatasets(ctx context.Context) ([]*Dataset, error) {
datasets := make([]*Dataset, 0)
req, err := c.NewRequest("GET", datasetsPath, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &datasets); err != nil {
return nil, err
}
return datasets, nil
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"GetDatasets",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Dataset",
",",
"error",
")",
"{",
"datasets",
":=",
"make",
"(",
"[",
"]",
"*",
"Dataset",
",",
"0",
")",
"\n",
"req",
",",
"err",
":=",
"c",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"datasetsPath",
",",
"nil",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"c",
".",
"Do",
"(",
"ctx",
",",
"req",
",",
"&",
"datasets",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"datasets",
",",
"nil",
"\n",
"}"
] |
// GetDatasets provides list of the datasets that are available for download.
|
[
"GetDatasets",
"provides",
"list",
"of",
"the",
"datasets",
"that",
"are",
"available",
"for",
"download",
"."
] |
1320f6c93371f1828788ee9b7f3fcf568de18142
|
https://github.com/ns3777k/go-shodan/blob/1320f6c93371f1828788ee9b7f3fcf568de18142/shodan/datasets.go#L81-L93
|
18,398 |
ns3777k/go-shodan
|
shodan/datasets.go
|
GetDatasetFiles
|
func (c *Client) GetDatasetFiles(ctx context.Context, name string) ([]*DatasetFile, error) {
files := make([]*DatasetFile, 0)
path := fmt.Sprintf(datasetFilesPath, name)
req, err := c.NewRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &files); err != nil {
return nil, err
}
return files, nil
}
|
go
|
func (c *Client) GetDatasetFiles(ctx context.Context, name string) ([]*DatasetFile, error) {
files := make([]*DatasetFile, 0)
path := fmt.Sprintf(datasetFilesPath, name)
req, err := c.NewRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &files); err != nil {
return nil, err
}
return files, nil
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"GetDatasetFiles",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
")",
"(",
"[",
"]",
"*",
"DatasetFile",
",",
"error",
")",
"{",
"files",
":=",
"make",
"(",
"[",
"]",
"*",
"DatasetFile",
",",
"0",
")",
"\n",
"path",
":=",
"fmt",
".",
"Sprintf",
"(",
"datasetFilesPath",
",",
"name",
")",
"\n\n",
"req",
",",
"err",
":=",
"c",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"path",
",",
"nil",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"c",
".",
"Do",
"(",
"ctx",
",",
"req",
",",
"&",
"files",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"files",
",",
"nil",
"\n",
"}"
] |
// GetDatasetFiles returns a list of files that are available for download from the provided dataset.
|
[
"GetDatasetFiles",
"returns",
"a",
"list",
"of",
"files",
"that",
"are",
"available",
"for",
"download",
"from",
"the",
"provided",
"dataset",
"."
] |
1320f6c93371f1828788ee9b7f3fcf568de18142
|
https://github.com/ns3777k/go-shodan/blob/1320f6c93371f1828788ee9b7f3fcf568de18142/shodan/datasets.go#L96-L110
|
18,399 |
ns3777k/go-shodan
|
shodan/alert.go
|
GetAlerts
|
func (c *Client) GetAlerts(ctx context.Context) ([]*Alert, error) {
alerts := make([]*Alert, 0, 0)
req, err := c.NewRequest("GET", alertsInfoListPath, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &alerts); err != nil {
return nil, err
}
return alerts, err
}
|
go
|
func (c *Client) GetAlerts(ctx context.Context) ([]*Alert, error) {
alerts := make([]*Alert, 0, 0)
req, err := c.NewRequest("GET", alertsInfoListPath, nil, nil)
if err != nil {
return nil, err
}
if err := c.Do(ctx, req, &alerts); err != nil {
return nil, err
}
return alerts, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"GetAlerts",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Alert",
",",
"error",
")",
"{",
"alerts",
":=",
"make",
"(",
"[",
"]",
"*",
"Alert",
",",
"0",
",",
"0",
")",
"\n\n",
"req",
",",
"err",
":=",
"c",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"alertsInfoListPath",
",",
"nil",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"c",
".",
"Do",
"(",
"ctx",
",",
"req",
",",
"&",
"alerts",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"alerts",
",",
"err",
"\n",
"}"
] |
// GetAlerts returns a listing of all the network alerts that are currently active on the account.
|
[
"GetAlerts",
"returns",
"a",
"listing",
"of",
"all",
"the",
"network",
"alerts",
"that",
"are",
"currently",
"active",
"on",
"the",
"account",
"."
] |
1320f6c93371f1828788ee9b7f3fcf568de18142
|
https://github.com/ns3777k/go-shodan/blob/1320f6c93371f1828788ee9b7f3fcf568de18142/shodan/alert.go#L68-L81
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.