forked from fzipp/pythia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
158 lines (143 loc) · 4.19 KB
/
serve.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
150
151
152
153
154
155
156
157
158
// Copyright 2013 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"sort"
"strings"
"time"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/godoc"
"github.com/fzipp/pythia/static"
)
var (
indexView = parseTemplate("index.html")
sourceView = parseTemplate("source.html")
)
// serveIndex delivers the scope index page, which is the first
// page presented to the user.
func serveIndex(w http.ResponseWriter, req *http.Request) {
err := indexView.Execute(w, struct {
Scope string
Packages []*loader.PackageInfo
}{
Scope: strings.Join(args, " "),
Packages: packages,
})
if err != nil {
log.Println(err)
}
}
// serveSource delivers the source view page, which is the main
// workspace of the tool, where the user creates the queries to
// the guru and browses their results.
//
// The request parameter 'file' determines the source file to be
// shown initially, e.g. "/path/to/file.go". The contents of the
// file are not loaded in this request, but in a subsequent
// asynchronous request handled by serveFile.
//
// Returns a "403 Forbidden" status code if the requested file
// is not within the import scope.
func serveSource(w http.ResponseWriter, req *http.Request) {
file := req.FormValue("file")
if isForbidden(file) {
errorForbidden(w)
return
}
err := sourceView.Execute(w, file)
if err != nil {
log.Println(err)
}
}
// serveFile delivers an HTML fragment of a Go source file with
// highlighted comments and an (optional) highlighted selection.
// The request parameters are:
//
// path: "/path/to/file.go"
// s: optional selection range like "line.col-line.col", e.g. "24.4-25.10"
//
// Returns a "403 Forbidden" status code if the requested file
// is not within the import scope, or a "404 Not Found" if the
// file can't be read.
func serveFile(w http.ResponseWriter, req *http.Request) {
path := req.FormValue("path")
if isForbidden(path) {
errorForbidden(w)
return
}
content, err := ioutil.ReadFile(path)
if err != nil {
log.Println(req.RemoteAddr, err)
http.NotFound(w, req)
return
}
var sel godoc.Selection
s, err := parseSelection(req.FormValue("s"))
if err == nil {
offsets := s.byteOffsetsIn(content)
sel = godoc.RangeSelection(offsets)
}
var buf bytes.Buffer
godoc.FormatText(&buf, content, -1, true, "", sel)
buf.WriteTo(w)
}
// isForbidden checks if the given file path is in the file set of the
// imported scope and returns true if not, otherwise false.
func isForbidden(path string) bool {
// files must be sorted!
i := sort.SearchStrings(files, path)
return i >= len(files) || files[i] != path
}
func errorForbidden(w http.ResponseWriter) {
http.Error(w, "Forbidden", 403)
}
// serveQuery executes a query to the guru and delivers the results
// in the specified format. The request parameters are:
//
// mode: e.g. "describe", "callers", "freevars", ...
// pos: file name with byte offset(s), e.g. "/path/to/file.go:#1457,#1462"
// format: "json" or "plain", no "xml" at the moment
//
// If the application was launched in verbose mode, each query will be
// logged like an invocation of the guru command.
func serveQuery(w http.ResponseWriter, req *http.Request) {
mode := req.FormValue("mode")
pos := req.FormValue("pos")
format := req.FormValue("format")
if format != "json" && format != "plain" {
fmt.Println("Warning: incorrect format:", mode, pos, format)
}
// Call guru
var args []string
if format == "json" {
args = append(args, "-json")
}
args = append(args, "-scope", scope, mode, pos)
if *verbose {
log.Println("guru", strings.Join(args, " "))
}
out, err := exec.Command(guruPath, args...).Output()
if err != nil {
log.Println("launch guru command:", err)
http.Error(w, "guru command failed", http.StatusInternalServerError)
return
}
w.Write(out)
}
// serveStatic delivers the contents of a file from the static file map.
func serveStatic(w http.ResponseWriter, req *http.Request) {
name := req.URL.Path
data, ok := static.Files[name]
if !ok {
http.NotFound(w, req)
return
}
http.ServeContent(w, req, name, time.Time{}, strings.NewReader(data))
}