117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"gitea.teamortix.com/team-ortix/coverage/db"
|
|
"gitea.teamortix.com/team-ortix/coverage/pill"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type endpointData struct {
|
|
thirdParam string
|
|
queryFunc func(string, string, string) (db.CoverageData, error)
|
|
handle func(db.CoverageData)
|
|
}
|
|
|
|
func (d endpointData) run(w http.ResponseWriter, r *http.Request) {
|
|
params, ok := getAllParams(w, r, d.thirdParam)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
data, err := d.queryFunc(params[0], params[1], params[2])
|
|
if err == db.ErrNoData {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("could not query from db: %v", err), http.StatusInternalServerError)
|
|
log.Printf("error occurred while querying data from db: %v", err)
|
|
return
|
|
}
|
|
|
|
d.handle(data)
|
|
}
|
|
|
|
func handleBranchReport(w http.ResponseWriter, r *http.Request) {
|
|
endpointData{
|
|
thirdParam: "branch",
|
|
queryFunc: db.QueryByBranch,
|
|
handle: func(cd db.CoverageData) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
fmt.Fprint(w, cd.HTML)
|
|
},
|
|
}.run(w, r)
|
|
}
|
|
|
|
func handleBadgeBranch(w http.ResponseWriter, r *http.Request) {
|
|
endpointData{
|
|
thirdParam: "branch",
|
|
queryFunc: db.QueryByBranch,
|
|
handle: func(cd db.CoverageData) {
|
|
p := pill.NewPill(cd.Coverage)
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
err := p.Execute(w)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("could not write pill: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
},
|
|
}.run(w, r)
|
|
}
|
|
|
|
func handlePullReport(w http.ResponseWriter, r *http.Request) {
|
|
endpointData{
|
|
thirdParam: "pull",
|
|
queryFunc: db.QueryByPull,
|
|
handle: func(cd db.CoverageData) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
fmt.Fprint(w, cd.HTML)
|
|
},
|
|
}.run(w, r)
|
|
}
|
|
|
|
func handlePullBadge(w http.ResponseWriter, r *http.Request) {
|
|
endpointData{
|
|
thirdParam: "pull",
|
|
queryFunc: db.QueryByPull,
|
|
handle: func(cd db.CoverageData) {
|
|
p := pill.NewPill(cd.Coverage)
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
err := p.Execute(w)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("could not write pill: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
},
|
|
}.run(w, r)
|
|
}
|
|
|
|
func getAllParams(w http.ResponseWriter, r *http.Request, thirdParam string) ([3]string, bool) {
|
|
vars := mux.Vars(r)
|
|
namespace, ok := vars["namespace"]
|
|
if !ok {
|
|
http.Error(w, "Path 'namespace' not found", http.StatusBadRequest)
|
|
return [3]string{}, false
|
|
}
|
|
|
|
project, ok := vars["project"]
|
|
if !ok {
|
|
http.Error(w, "Path 'project' not found", http.StatusBadRequest)
|
|
return [3]string{}, false
|
|
}
|
|
|
|
third, ok := vars[thirdParam]
|
|
if !ok {
|
|
http.Error(w, fmt.Sprintf("Path '%s' not found", thirdParam), http.StatusBadRequest)
|
|
return [3]string{}, false
|
|
}
|
|
|
|
return [3]string{namespace, project, third}, true
|
|
}
|