coverage/server/server.go

54 lines
1.5 KiB
Go

package server
import (
"fmt"
"log"
"net/http"
"strconv"
"gitea.teamortix.com/team-ortix/coverage/pill"
"github.com/gorilla/mux"
)
func StartServer(port string) {
r := mux.NewRouter()
r.HandleFunc("/badge/{percentage}", handleBadge)
r.HandleFunc("/report/branch/{namespace}/{project}/{branch}", handleBranchReport)
r.HandleFunc("/report/pulls/{namespace}/{project}/{pull}", handlePullReport)
r.HandleFunc("/badge/branch/{namespace}/{project}/{branch}", handleBadgeBranch)
r.HandleFunc("/badge/pulls/{namespace}/{project}/{pull}", handlePullBadge)
r.HandleFunc("/upload/branch/{namespace}/{project}/{branch}", uploadBranch)
r.HandleFunc("/upload/pulls/{namespace}/{project}/{branch}", uploadPull)
portStr := fmt.Sprintf(":%s", port)
err := http.ListenAndServe(portStr, r)
if err != nil {
log.Fatalf("could not start server: %v", err)
}
}
func handleBadge(w http.ResponseWriter, r *http.Request) {
percentString, ok := mux.Vars(r)["percentage"]
if !ok {
http.Error(w, "could not find percentage in path", http.StatusBadRequest)
return
}
percent, err := strconv.ParseFloat(percentString, 64)
if err != nil {
http.Error(w, fmt.Sprintf("could not convert %s to a number", percentString), http.StatusBadRequest)
return
}
p := pill.NewPill(percent / 100)
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
}
}