-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdoc.go
210 lines (169 loc) · 5.11 KB
/
doc.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2013 Chris McGee <[email protected]>. 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 (
"bufio"
"go/build"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
func init() {
dirs := build.Default.SrcDirs()
godoc_templates_dir := ""
for i := len(dirs) - 1; i >= 0; i-- {
srcDir := dirs[i]
if godoc_templates_dir == "" {
_, err := os.Stat(srcDir + "/github.com/sirnewton01/godev/godoc-templates")
if err == nil {
godoc_templates_dir = srcDir + "/github.com/sirnewton01/godev/godoc-templates"
break
}
}
}
// Try again with with the srcdir parameter
if godoc_templates_dir == "" {
_, err := os.Stat(*godev_src_dir + "/godoc-templates")
if err == nil {
godoc_templates_dir = *godev_src_dir + "/godoc-templates"
}
}
// Find an available port number
godocPort := int64(6060)
godocPortStr := strconv.FormatInt(godocPort, 10)
for {
_, err := net.DialTimeout("tcp", "127.0.0.1:"+godocPortStr, 100*time.Millisecond)
if err != nil {
break
}
godocPort++
godocPortStr = strconv.FormatInt(godocPort, 10)
}
if godoc_templates_dir != "" {
go func() {
cmd := exec.Command("godoc", "-http=127.0.0.1:"+godocPortStr, "-index=true", "-templates="+godoc_templates_dir)
cmd.Run()
}()
}
}
func docHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
// Start a simple proxy for many of the different types of requests (except for source code)
case req.Method == "GET" && pathSegs[1] != "src" && pathSegs[1] != "text":
delegatePath := "/" + strings.Join(pathSegs[1:], "/")
queryParam := req.URL.RawQuery
// Check to see if the search param is a precise package and redirect
// since the godoc tool doesn't do this very well.
if queryParam != "" && pathSegs[1] == "search" {
pkgName := req.URL.Query().Get("q")
gorootsrc := filepath.Join(goroot, "/src/pkg")
for _, srcDir := range append(srcDirs, gorootsrc) {
info, err := os.Stat(filepath.Join(srcDir, pkgName))
if err == nil && info.IsDir() {
http.Redirect(writer, req, "/godoc/pkg/"+pkgName, 302)
return true
}
}
}
resp, err := http.Get("http://127.0.0.1:6060" + delegatePath + "?" + queryParam)
if err != nil {
ShowError(writer, 500, "Error connecting to godoc server", err)
return true
}
defer resp.Body.Close()
contentType := req.Header.Get("Content-Type")
writer.Header().Add("Content-Type", contentType)
writer.WriteHeader(resp.StatusCode)
// Read line-by line replacing any hrefs to /pkg to /godoc/pkg
r := bufio.NewReader(resp.Body)
w := bufio.NewWriter(writer)
defer w.Flush()
for {
line, _ := r.ReadString('\n')
if line != "" {
line = strings.Replace(line, `a href="/pkg/`, `a href="/godoc/pkg/`, -1)
w.WriteString(line)
} else {
break
}
}
return true
// Redirect source code to the Orion editor interface
case req.Method == "GET" && pathSegs[1] == "src":
lineNumber := ""
redirectLocation := ""
// Check for the line number
sourceRange := req.URL.Query().Get("s")
sourceStartOffset := -1
if sourceRange != "" {
offsetString := strings.Split(sourceRange, ":")[0]
offset, err := strconv.ParseInt(offsetString, 10, 64)
if err == nil {
sourceStartOffset = int(offset)
}
}
gorootsrc := filepath.Join(goroot, "/src/pkg")
for _, srcDir := range append(srcDirs, gorootsrc) {
potentialMatch := filepath.Join(srcDir, filepath.Join(pathSegs[3:]...))
if _, err := os.Stat(potentialMatch); err == nil {
file, err := os.Open(potentialMatch)
defer file.Close()
if sourceStartOffset != -1 && err == nil {
buf := make([]byte, sourceStartOffset, sourceStartOffset)
bytesRead, _ := file.Read(buf)
if bytesRead == sourceStartOffset {
// We read all of the necessary bytes to count lines
lines := 1
for _, b := range buf {
if b == '\n' {
lines++
}
}
lineNumber = strconv.FormatInt(int64(lines), 10)
}
}
if file != nil && srcDir != gorootsrc {
redirectLocation = "/file/" + strings.Join(pathSegs[3:], "/")
break
}
if file != nil && srcDir == gorootsrc {
redirectLocation = "/file/GOROOT/" + strings.Join(pathSegs[3:], "/")
break
}
}
}
if redirectLocation == "" {
ShowError(writer, 404, "Not Found", nil)
return true
}
if lineNumber != "" {
redirectLocation = redirectLocation + ",line=" + lineNumber
}
http.Redirect(writer, req, "/edit/edit.html#"+redirectLocation, 302)
return true
// Get the textual godoc for a package and optional name
case req.Method == "GET" && pathSegs[1] == "text":
pkg := req.URL.Query().Get("pkg")
name := req.URL.Query().Get("name")
if pkg == "" {
ShowError(writer, 400, "No package provided", nil)
return true
}
cmd := exec.Command("godoc", pkg, name)
output, err := cmd.Output()
if err != nil {
ShowError(writer, 500, "Error invoking godoc tool", err)
return true
}
writer.WriteHeader(200)
writer.Write(output)
return true
}
return false
}