-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
581 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2014 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hooks | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/gogits/gogs/modules/httplib" | ||
"github.com/gogits/gogs/modules/log" | ||
) | ||
|
||
// Hook task types. | ||
const ( | ||
HTT_WEBHOOK = iota + 1 | ||
HTT_SERVICE | ||
) | ||
|
||
type PayloadAuthor struct { | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
} | ||
|
||
type PayloadCommit struct { | ||
Id string `json:"id"` | ||
Message string `json:"message"` | ||
Url string `json:"url"` | ||
Author *PayloadAuthor `json:"author"` | ||
} | ||
|
||
// Payload represents payload information of hook. | ||
type Payload struct { | ||
Secret string `json:"secret"` | ||
Ref string `json:"ref"` | ||
Commits []*PayloadCommit `json:"commits"` | ||
Pusher *PayloadAuthor `json:"pusher"` | ||
} | ||
|
||
// HookTask represents hook task. | ||
type HookTask struct { | ||
Type int | ||
Url string | ||
*Payload | ||
ContentType int | ||
IsSsl bool | ||
} | ||
|
||
var ( | ||
taskQueue = make(chan *HookTask, 1000) | ||
) | ||
|
||
// AddHookTask adds new hook task to task queue. | ||
func AddHookTask(t *HookTask) { | ||
taskQueue <- t | ||
} | ||
|
||
func init() { | ||
go handleQueue() | ||
} | ||
|
||
func handleQueue() { | ||
for { | ||
select { | ||
case t := <-taskQueue: | ||
// Only support JSON now. | ||
data, err := json.MarshalIndent(t.Payload, "", "\t") | ||
if err != nil { | ||
log.Error("hooks.handleQueue(json): %v", err) | ||
continue | ||
} | ||
|
||
_, err = httplib.Post(t.Url).SetTimeout(5*time.Second, 5*time.Second). | ||
Body(data).Response() | ||
if err != nil { | ||
log.Error("hooks.handleQueue: Fail to deliver hook: %v", err) | ||
continue | ||
} | ||
log.Info("Hook delivered") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# httplib | ||
httplib is an libs help you to curl remote url. | ||
|
||
# How to use? | ||
|
||
## GET | ||
you can use Get to crawl data. | ||
|
||
import "httplib" | ||
|
||
str, err := httplib.Get("http://beego.me/").String() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fmt.Println(str) | ||
|
||
## POST | ||
POST data to remote url | ||
|
||
b:=httplib.Post("http://beego.me/") | ||
b.Param("username","astaxie") | ||
b.Param("password","123456") | ||
str, err := b.String() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fmt.Println(str) | ||
|
||
## set timeout | ||
you can set timeout in request.default is 60 seconds. | ||
|
||
set Get timeout: | ||
|
||
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | ||
|
||
set post timeout: | ||
|
||
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | ||
|
||
- first param is connectTimeout. | ||
- second param is readWriteTimeout | ||
|
||
## debug | ||
if you want to debug the request info, set the debug on | ||
|
||
httplib.Get("http://beego.me/").Debug(true) | ||
|
||
## support HTTPS client | ||
if request url is https. You can set the client support TSL: | ||
|
||
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | ||
|
||
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config | ||
|
||
## set cookie | ||
some http request need setcookie. So set it like this: | ||
|
||
cookie := &http.Cookie{} | ||
cookie.Name = "username" | ||
cookie.Value = "astaxie" | ||
httplib.Get("http://beego.me/").SetCookie(cookie) | ||
|
Oops, something went wrong.