forked from gojp/goreportcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (105 loc) · 3.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"time"
"github.com/boltdb/bolt"
"github.com/golang/glog"
"github.com/goreporter/goreporterweb/handlers"
"github.com/goreporter/goreporterweb/static"
)
var (
addr = flag.String("http", "0.0.0.0:8000", "HTTP listen address")
dev = flag.Bool("dev", false, "dev mode")
)
func makeHandler(name string, dev bool, fn func(http.ResponseWriter, *http.Request, string, bool)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
validPath := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_\/\.]+)$`, name))
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
if len(m) < 1 || m[1] == "" {
http.Error(w, "Please enter a repository", http.StatusBadRequest)
return
}
repo := m[1]
// for backwards-compatibility, we must support URLs formatted as
// /report/[org]/[repo]
// and they will be assumed to be github.com URLs. This is because
// at first Go Report Card only supported github.com URLs, and assumed
// took only the org name and repo name as parameters. This is no longer the
// case, but we do not want external links to break.
oldFormat := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)$`, name))
m2 := oldFormat.FindStringSubmatch(r.URL.Path)
if m2 != nil {
// old format is being used
repo = "github.com/" + repo
glog.Infoln("Assuming intended repo is %q, redirecting", repo)
http.Redirect(w, r, fmt.Sprintf("/%s/%s", name, repo), http.StatusMovedPermanently)
return
}
fn(w, r, repo, dev)
}
}
// initDB opens the bolt database file (or creates it if it does not exist), and creates
// a bucket for saving the repos, also only if it does not exist.
func initDB() error {
db, err := bolt.Open(handlers.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return err
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(handlers.RepoBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(handlers.MetaBucket))
return err
})
return err
}
func initStatic() {
dirs := []string{"templates", "assets"}
isSuccess := true
for _, dir := range dirs {
if err := static.RestoreAssets("./", dir); err != nil {
isSuccess = false
break
}
}
if !isSuccess {
for _, dir := range dirs {
os.RemoveAll(filepath.Join("./", dir))
}
}
}
func main() {
flag.Parse()
if err := os.MkdirAll("repos/src/github.com", 0755); err != nil && !os.IsExist(err) {
glog.Fatal("ERROR: could not create repos dir: ", err)
}
// initialize database
if err := initDB(); err != nil {
glog.Fatal("ERROR: could not open bolt db: ", err)
}
// initialize static source
initStatic()
http.HandleFunc("/assets/", handlers.AssetsHandler)
http.HandleFunc("/favicon.ico", handlers.FaviconHandler)
http.HandleFunc("/checks", handlers.CheckHandler)
http.HandleFunc("/report/", makeHandler("report", *dev, handlers.ReportHandler))
http.HandleFunc("/badge/", makeHandler("badge", *dev, handlers.BadgeHandler))
http.HandleFunc("/high_scores/", handlers.HighScoresHandler)
http.HandleFunc("/about/", handlers.AboutHandler)
http.HandleFunc("/features/", handlers.FeaturesHandler)
http.HandleFunc("/", handlers.HomeHandler)
glog.Infoln("Running on %s ...", *addr)
glog.Fatal(http.ListenAndServe(*addr, nil))
}