mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 13:20:02 +00:00
Add context to timeout badge generators
This commit is contained in:
parent
125e723724
commit
0342b26a10
3 changed files with 19 additions and 6 deletions
10
app.go
10
app.go
|
@ -9,6 +9,9 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/Luzifer/rconfig"
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -51,7 +54,7 @@ func (s serviceHandlerDocumentationList) Swap(i, j int) { s[i], s[j] = s[j], s[i
|
|||
|
||||
type serviceHandler interface {
|
||||
GetDocumentation() serviceHandlerDocumentation
|
||||
Handle(params []string) (title, text, color string, err error)
|
||||
Handle(ctx context.Context, params []string) (title, text, color string, err error)
|
||||
}
|
||||
|
||||
func registerServiceHandler(service string, f serviceHandler) error {
|
||||
|
@ -81,13 +84,16 @@ func generateServiceBadge(res http.ResponseWriter, r *http.Request) {
|
|||
service := vars["service"]
|
||||
params := strings.Split(vars["parameters"], "/")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
handler, ok := serviceHandlers[service]
|
||||
if !ok {
|
||||
http.Error(res, "Service not found: "+service, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
title, text, color, err := handler.Handle(params)
|
||||
title, text, color, err := handler.Handle(ctx, params)
|
||||
if err != nil {
|
||||
http.Error(res, "Error while executing service: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package main
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerServiceHandler("static", staticServiceHandler{})
|
||||
|
@ -16,7 +20,7 @@ func (s staticServiceHandler) GetDocumentation() serviceHandlerDocumentation {
|
|||
}
|
||||
}
|
||||
|
||||
func (s staticServiceHandler) Handle(params []string) (title, text, color string, err error) {
|
||||
func (s staticServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
||||
if len(params) < 2 {
|
||||
err = errors.New("You need to provide title and text")
|
||||
return
|
||||
|
|
|
@ -5,6 +5,9 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/net/context/ctxhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -21,7 +24,7 @@ func (t travisServiceHandler) GetDocumentation() serviceHandlerDocumentation {
|
|||
}
|
||||
}
|
||||
|
||||
func (t travisServiceHandler) Handle(params []string) (title, text, color string, err error) {
|
||||
func (t travisServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
||||
if len(params) < 2 {
|
||||
err = errors.New("You need to provide user and repo")
|
||||
return
|
||||
|
@ -34,7 +37,7 @@ func (t travisServiceHandler) Handle(params []string) (title, text, color string
|
|||
path := strings.Join([]string{"repos", params[0], params[1], "branches", params[2]}, "/")
|
||||
|
||||
var resp *http.Response
|
||||
resp, err = http.Get("https://api.travis-ci.org/" + path)
|
||||
resp, err = ctxhttp.Get(ctx, http.DefaultClient, "https://api.travis-ci.org/"+path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue