mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 21:30: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"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/Luzifer/rconfig"
|
"github.com/Luzifer/rconfig"
|
||||||
"github.com/gorilla/mux"
|
"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 {
|
type serviceHandler interface {
|
||||||
GetDocumentation() serviceHandlerDocumentation
|
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 {
|
func registerServiceHandler(service string, f serviceHandler) error {
|
||||||
|
@ -81,13 +84,16 @@ func generateServiceBadge(res http.ResponseWriter, r *http.Request) {
|
||||||
service := vars["service"]
|
service := vars["service"]
|
||||||
params := strings.Split(vars["parameters"], "/")
|
params := strings.Split(vars["parameters"], "/")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
handler, ok := serviceHandlers[service]
|
handler, ok := serviceHandlers[service]
|
||||||
if !ok {
|
if !ok {
|
||||||
http.Error(res, "Service not found: "+service, http.StatusNotFound)
|
http.Error(res, "Service not found: "+service, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
title, text, color, err := handler.Handle(params)
|
title, text, color, err := handler.Handle(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(res, "Error while executing service: "+err.Error(), http.StatusInternalServerError)
|
http.Error(res, "Error while executing service: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerServiceHandler("static", staticServiceHandler{})
|
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 {
|
if len(params) < 2 {
|
||||||
err = errors.New("You need to provide title and text")
|
err = errors.New("You need to provide title and text")
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,6 +5,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"golang.org/x/net/context/ctxhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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 {
|
if len(params) < 2 {
|
||||||
err = errors.New("You need to provide user and repo")
|
err = errors.New("You need to provide user and repo")
|
||||||
return
|
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]}, "/")
|
path := strings.Join([]string{"repos", params[0], params[1], "branches", params[2]}, "/")
|
||||||
|
|
||||||
var resp *http.Response
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue