GoPLS Viewer

Home|gopls/cmd/godoc/handlers.go
1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8    "encoding/json"
9    "go/format"
10    "log"
11    "net/http"
12    "text/template"
13
14    "golang.org/x/tools/godoc"
15    "golang.org/x/tools/godoc/redirect"
16    "golang.org/x/tools/godoc/vfs"
17
18    _ "golang.org/x/tools/playground" // register "/compile" playground redirect
19)
20
21var (
22    pres *godoc.Presentation
23    fs   = vfs.NameSpace{}
24)
25
26func registerHandlers(pres *godoc.Presentation) {
27    if pres == nil {
28        panic("nil Presentation")
29    }
30    mux := http.NewServeMux()
31    mux.HandleFunc("/", func(w http.ResponseWriterreq *http.Request) {
32        if req.URL.Path == "/" {
33            http.Redirect(wreq"/pkg/"http.StatusFound)
34            return
35        }
36        pres.ServeHTTP(wreq)
37    })
38    mux.Handle("/pkg/C/"redirect.Handler("/cmd/cgo/"))
39    mux.HandleFunc("/fmt"fmtHandler)
40    redirect.Register(mux)
41
42    http.Handle("/"mux)
43}
44
45func readTemplate(name string) *template.Template {
46    if pres == nil {
47        panic("no global Presentation set yet")
48    }
49    path := "lib/godoc/" + name
50
51    // use underlying file system fs to read the template file
52    // (cannot use template ParseFile functions directly)
53    dataerr := vfs.ReadFile(fspath)
54    if err != nil {
55        log.Fatal("readTemplate: "err)
56    }
57    // be explicit with errors (for app engine use)
58    terr := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
59    if err != nil {
60        log.Fatal("readTemplate: "err)
61    }
62    return t
63}
64
65func readTemplates(p *godoc.Presentation) {
66    p.CallGraphHTML = readTemplate("callgraph.html")
67    p.DirlistHTML = readTemplate("dirlist.html")
68    p.ErrorHTML = readTemplate("error.html")
69    p.ExampleHTML = readTemplate("example.html")
70    p.GodocHTML = readTemplate("godoc.html")
71    p.ImplementsHTML = readTemplate("implements.html")
72    p.MethodSetHTML = readTemplate("methodset.html")
73    p.PackageHTML = readTemplate("package.html")
74    p.PackageRootHTML = readTemplate("packageroot.html")
75    p.SearchHTML = readTemplate("search.html")
76    p.SearchDocHTML = readTemplate("searchdoc.html")
77    p.SearchCodeHTML = readTemplate("searchcode.html")
78    p.SearchTxtHTML = readTemplate("searchtxt.html")
79}
80
81type fmtResponse struct {
82    Body  string
83    Error string
84}
85
86// fmtHandler takes a Go program in its "body" form value, formats it with
87// standard gofmt formatting, and writes a fmtResponse as a JSON object.
88func fmtHandler(w http.ResponseWriterr *http.Request) {
89    resp := new(fmtResponse)
90    bodyerr := format.Source([]byte(r.FormValue("body")))
91    if err != nil {
92        resp.Error = err.Error()
93    } else {
94        resp.Body = string(body)
95    }
96    w.Header().Set("Content-type""application/json; charset=utf-8")
97    json.NewEncoder(w).Encode(resp)
98}
99
MembersX
pres
registerHandlers.pres
readTemplate
readTemplate.err
fmtResponse.Body
format
log
fmtHandler.r
fmtHandler.body
json
registerHandlers
readTemplates
fmtResponse
fmtHandler.w
redirect
vfs
registerHandlers.mux
readTemplate.data
readTemplates.p
fmtResponse.Error
fmtHandler.err
readTemplate.t
fmtHandler.resp
template
godoc
_
readTemplate.name
fmtHandler
Members
X