Skip to content

Commit 2ab0463

Browse files
authored
Merge pull request #56 from factorysh/features/result-url-in-response
Add result url in http response
2 parents 2341dec + ce9cfb6 commit 2ab0463

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

application/home.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"embed"
55
"fmt"
66
"net/http"
7-
"strings"
87

98
"github.com/factorysh/microdensity/html"
109
"github.com/factorysh/microdensity/version"
@@ -18,21 +17,6 @@ var (
1817
homeTemplate string
1918
)
2019

21-
func acceptsHTML(r *http.Request) bool {
22-
accepts, found := r.Header["Accept"]
23-
if !found {
24-
return false
25-
}
26-
27-
for _, h := range accepts {
28-
if strings.Contains(h, "text/html") {
29-
return true
30-
}
31-
}
32-
33-
return false
34-
}
35-
3620
const logo = `
3721
_ _ _
3822
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
@@ -51,7 +35,7 @@ func (a *Application) HomeHandler() http.HandlerFunc {
5135

5236
// if you ask for something that is not html
5337
// nice geeky ascii art
54-
if !acceptsHTML(r) {
38+
if !html.Accepts(r, "text/html") {
5539
w.Header().Set("content-type", "text/plain")
5640
w.Write([]byte(logo))
5741
fmt.Fprintf(w, "Version: %s", version.Version())

application/task.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
"github.com/docker/docker/pkg/stdcopy"
@@ -98,8 +99,22 @@ func (a *Application) PostTaskHandler(w http.ResponseWriter, r *http.Request) {
9899
l.Error("error when adding task", zap.String("task", t.Id.String()), zap.Error(err))
99100
return
100101
}
102+
103+
url := strings.Join([]string{a.Domain, "service", t.Service, t.Project, t.Branch, t.Commit, "volumes", "data", "result.html"}, "/")
104+
105+
if html.Accepts(r, "text/plain") {
106+
w.Header().Add("content-type", "text/plain")
107+
_, err := w.Write([]byte(fmt.Sprintf("Result URL:\n\n\t\t%s\n\n", url)))
108+
if err != nil {
109+
w.WriteHeader(http.StatusInternalServerError)
110+
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
111+
}
112+
return
113+
}
114+
101115
render.JSON(w, r, map[string]string{
102-
"id": id.String(),
116+
"id": id.String(),
117+
"url": url,
103118
})
104119
}
105120

html/html.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"html/template"
77
_template "html/template"
88
"net/http"
9+
"strings"
910

1011
"github.com/yosssi/gohtml"
1112
)
@@ -76,3 +77,19 @@ func (c *Partial) Render() (_template.HTML, error) {
7677

7778
return template.HTML(buffer.Bytes()), err
7879
}
80+
81+
// Accepts checks is a request accept content of a specified kind
82+
func Accepts(r *http.Request, kind string) bool {
83+
accepts, found := r.Header["Accept"]
84+
if !found {
85+
return false
86+
}
87+
88+
for _, h := range accepts {
89+
if strings.Contains(h, kind) {
90+
return true
91+
}
92+
}
93+
94+
return false
95+
}

0 commit comments

Comments
 (0)