diff --git a/app.go b/app.go index b87a97f..17445cd 100644 --- a/app.go +++ b/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 diff --git a/service_static.go b/service_static.go index 51237f6..653d6fd 100644 --- a/service_static.go +++ b/service_static.go @@ -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 diff --git a/service_travis.go b/service_travis.go index 4c7b11d..afdd465 100644 --- a/service_travis.go +++ b/service_travis.go @@ -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 }