mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"regexp"
|
|
|
|
"github.com/gojp/goreportcard/handlers"
|
|
)
|
|
|
|
func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string, string)) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
validPath := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)$`, name))
|
|
|
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
// catch the special period cases that github does not allow for repos
|
|
if m[2] == "." || m[2] == ".." {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
fn(w, r, m[1], m[2])
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if err := os.MkdirAll("repos/src/github.com", 0755); err != nil && !os.IsExist(err) {
|
|
log.Fatal("ERROR: could not create repos dir: ", err)
|
|
}
|
|
|
|
http.HandleFunc("/assets/", handlers.AssetsHandler)
|
|
http.HandleFunc("/checks", handlers.CheckHandler)
|
|
http.HandleFunc("/report/", makeHandler("report", handlers.ReportHandler))
|
|
http.HandleFunc("/badge/", makeHandler("badge", handlers.BadgeHandler))
|
|
http.HandleFunc("/high_scores/", handlers.HighScoresHandler)
|
|
http.HandleFunc("/", handlers.HomeHandler)
|
|
|
|
fmt.Println("Running on 127.0.0.1:8080...")
|
|
log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
|
|
}
|