122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"gitea.teamortix.com/team-ortix/coverage/db"
|
|
"gitea.teamortix.com/team-ortix/coverage/pill"
|
|
)
|
|
|
|
type endpointData struct {
|
|
thirdParam string
|
|
notFound func()
|
|
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 {
|
|
d.notFound()
|
|
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,
|
|
notFound: func() {
|
|
http.Error(w, "404 page not found", http.StatusNotFound)
|
|
},
|
|
handle: func(cd db.CoverageData) {
|
|
w.Header().Set("Cache-Control", "max-age=1800")
|
|
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,
|
|
notFound: func() {
|
|
p := pill.UnknownPill()
|
|
|
|
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
|
|
}
|
|
},
|
|
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,
|
|
notFound: func() {
|
|
http.Error(w, "404 page not found", http.StatusNotFound)
|
|
},
|
|
handle: func(cd db.CoverageData) {
|
|
w.Header().Set("Cache-Control", "max-age=1800")
|
|
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,
|
|
notFound: func() {
|
|
p := pill.UnknownPill()
|
|
|
|
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
|
|
}
|
|
},
|
|
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)
|
|
}
|