Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions application/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"embed"
"fmt"
"net/http"
"strings"

"github.com/factorysh/microdensity/html"
"github.com/factorysh/microdensity/version"
Expand All @@ -18,21 +17,6 @@ var (
homeTemplate string
)

func acceptsHTML(r *http.Request) bool {
accepts, found := r.Header["Accept"]
if !found {
return false
}

for _, h := range accepts {
if strings.Contains(h, "text/html") {
return true
}
}

return false
}

const logo = `
_ _ _
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
Expand All @@ -51,7 +35,7 @@ func (a *Application) HomeHandler() http.HandlerFunc {

// if you ask for something that is not html
// nice geeky ascii art
if !acceptsHTML(r) {
if !html.Accepts(r, "text/html") {
w.Header().Set("content-type", "text/plain")
w.Write([]byte(logo))
fmt.Fprintf(w, "Version: %s", version.Version())
Expand Down
17 changes: 16 additions & 1 deletion application/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/docker/docker/pkg/stdcopy"
Expand Down Expand Up @@ -98,8 +99,22 @@ func (a *Application) PostTaskHandler(w http.ResponseWriter, r *http.Request) {
l.Error("error when adding task", zap.String("task", t.Id.String()), zap.Error(err))
return
}

url := strings.Join([]string{a.Domain, "service", t.Service, t.Project, t.Branch, t.Commit, "volumes", "data", "result.html"}, "/")

if html.Accepts(r, "text/plain") {
w.Header().Add("content-type", "text/plain")
_, err := w.Write([]byte(fmt.Sprintf("Result URL:\n\n\t\t%s\n\n", url)))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
return
}

render.JSON(w, r, map[string]string{
"id": id.String(),
"id": id.String(),
"url": url,
})
}

Expand Down
17 changes: 17 additions & 0 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
_template "html/template"
"net/http"
"strings"

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

return template.HTML(buffer.Bytes()), err
}

// Accepts checks is a request accept content of a specified kind
func Accepts(r *http.Request, kind string) bool {
accepts, found := r.Header["Accept"]
if !found {
return false
}

for _, h := range accepts {
if strings.Contains(h, kind) {
return true
}
}

return false
}