-
Notifications
You must be signed in to change notification settings - Fork 139
/
analyzer.go
149 lines (123 loc) · 3.23 KB
/
analyzer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package handle
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"time"
"github.com/rodrigo-brito/gocity/analyzer"
"github.com/rodrigo-brito/gocity/handle/middlewares"
"github.com/rodrigo-brito/gocity/lib"
"github.com/rodrigo-brito/gocity/model"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
log "github.com/sirupsen/logrus"
)
//go:embed assets
var assets embed.FS
//go:embed assets/index.html
var indexPage []byte
var ErrInvalidPath = fmt.Errorf("invalid path")
type AnalyzerHandle struct {
Cache lib.Cache
CacheTTL time.Duration
TmpFolder string
Port int
ProjectPath *string
Branch *string
Local bool
}
func (h *AnalyzerHandle) Handler(w http.ResponseWriter, r *http.Request) {
var (
ok bool
projectAddress string
)
if h.ProjectPath != nil {
projectAddress = *h.ProjectPath
}
if q := r.URL.Query().Get("q"); q != "local" {
projectAddress, ok = lib.GetGithubBaseURL(r.URL.Query().Get("q"))
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
}
branch := "master"
if b := r.URL.Query().Get("b"); b != "" {
branch = b
} else if h.Branch != nil {
branch = *h.Branch
}
key := fmt.Sprintf("%s:%s", projectAddress, branch)
result, err := h.Cache.GetSet(key, func() ([]byte, error) {
analyzer := analyzer.NewAnalyzer(projectAddress, branch, h.TmpFolder, analyzer.WithIgnoreList("/vendor/"))
path := projectAddress
stat, err := os.Stat(projectAddress)
if os.IsNotExist(err) {
path, err = analyzer.FetchPackage()
if err != nil {
return nil, err
}
} else if !stat.IsDir() {
return nil, ErrInvalidPath
}
summary, err := analyzer.Analyze(path)
if err != nil {
return nil, err
}
body, err := json.Marshal(model.New(summary, projectAddress, branch))
if err != nil {
return nil, err
}
return body, nil
}, h.CacheTTL)
if err == ErrInvalidPath {
w.WriteHeader(http.StatusBadRequest)
log.Print(err)
} else if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
log.Print(err)
return
}
if len(result) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(result)
}
func (h *AnalyzerHandle) Serve() error {
router := chi.NewRouter()
cors := middlewares.GetCors("*")
baseURL := fmt.Sprintf("http://localhost:%d", h.Port)
router.Use(cors.Handler)
router.Use(middlewares.APIHeader(fmt.Sprintf("%s/api", baseURL)))
router.Use(middleware.DefaultLogger)
router.Get("/api", h.Handler)
router.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write(indexPage)
if err != nil {
log.Error(err)
}
})
var staticFS = fs.FS(assets)
content, err := fs.Sub(staticFS, "assets")
if err != nil {
log.Fatal(err)
}
router.Handle("/*", http.FileServer(http.FS(content)))
if h.Local {
log.Infof("Visualization available at: %s/#/local", baseURL)
} else if h.ProjectPath != nil {
log.Infof("Visualization available at: %s/#/%s", baseURL, *h.ProjectPath)
} else {
log.Infof("Server started at %s", baseURL)
}
return http.ListenAndServe(fmt.Sprintf(":%d", h.Port), router)
}