forked from rande/pkgmirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
85 lines (66 loc) · 1.67 KB
/
app.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
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package pkgmirror
import (
"errors"
"fmt"
"net/http"
"github.com/NYTimes/gziphandler"
log "github.com/Sirupsen/logrus"
"github.com/bakins/logrus-middleware"
"github.com/rande/goapp"
"goji.io"
)
func GetApp(conf *Config, l *goapp.Lifecycle) (*goapp.App, error) {
app := goapp.NewApp()
// init logger
logger := log.New()
if level, err := log.ParseLevel(conf.LogLevel); err != nil {
return app, errors.New(fmt.Sprintf("Unable to parse the log level: %s", conf.LogLevel))
} else {
logger.Level = level
}
if len(conf.DataDir) == 0 {
return app, errors.New("Please configure DataDir")
}
app.Set("logger", func(app *goapp.App) interface{} {
return logger
})
app.Set("config", func(app *goapp.App) interface{} {
return conf
})
app.Set("mux", func(app *goapp.App) interface{} {
m := goji.NewMux()
m.Use(func(h http.Handler) http.Handler {
gzip := gziphandler.GzipHandler(h)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
skip := false
if len(r.URL.Path) > 4 && r.URL.Path[1:4] == "npm" {
skip = true
}
if len(r.URL.Path) > 8 && r.URL.Path[1:9] == "composer" {
skip = true
}
if r.URL.Path == "/api/sse" {
skip = true
}
if skip {
h.ServeHTTP(w, r)
} else {
gzip.ServeHTTP(w, r)
}
})
})
m.Use(func(h http.Handler) http.Handler {
lm := &logrusmiddleware.Middleware{
Logger: logger,
Name: "pkgmirror",
}
return lm.Handler(h, "http")
})
return m
})
return app, nil
}