2013-07-18 22:02:03 -06:00
|
|
|
// Copyright 2010 The Go Authors. 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 (
|
2015-09-23 22:33:14 -06:00
|
|
|
"encoding/json"
|
|
|
|
"go/format"
|
2013-07-29 22:23:23 -06:00
|
|
|
"log"
|
2013-07-18 22:02:03 -06:00
|
|
|
"net/http"
|
2013-07-29 22:23:23 -06:00
|
|
|
"text/template"
|
2013-07-18 22:02:03 -06:00
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/godoc"
|
|
|
|
"golang.org/x/tools/godoc/redirect"
|
|
|
|
"golang.org/x/tools/godoc/vfs"
|
2013-07-29 22:23:23 -06:00
|
|
|
)
|
|
|
|
|
2019-11-18 14:16:45 -07:00
|
|
|
// This package registers "/compile" and "/share" handlers
|
|
|
|
// that redirect to the golang.org playground.
|
|
|
|
import _ "golang.org/x/tools/playground"
|
|
|
|
|
2013-07-29 22:23:23 -06:00
|
|
|
var (
|
|
|
|
pres *godoc.Presentation
|
|
|
|
fs = vfs.NameSpace{}
|
2013-07-18 22:02:03 -06:00
|
|
|
)
|
|
|
|
|
2019-11-18 14:16:45 -07:00
|
|
|
func registerHandlers(pres *godoc.Presentation) {
|
2013-07-18 22:02:03 -06:00
|
|
|
if pres == nil {
|
|
|
|
panic("nil Presentation")
|
|
|
|
}
|
2015-09-21 21:53:20 -06:00
|
|
|
mux := http.NewServeMux()
|
2019-11-18 14:16:45 -07:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/" {
|
|
|
|
http.Redirect(w, req, "/pkg/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pres.ServeHTTP(w, req)
|
|
|
|
})
|
2015-09-21 21:53:20 -06:00
|
|
|
mux.Handle("/pkg/C/", redirect.Handler("/cmd/cgo/"))
|
2015-09-23 22:33:14 -06:00
|
|
|
mux.HandleFunc("/fmt", fmtHandler)
|
2015-09-21 21:53:20 -06:00
|
|
|
redirect.Register(mux)
|
2015-09-23 22:33:14 -06:00
|
|
|
|
2019-11-18 14:16:45 -07:00
|
|
|
http.Handle("/", mux)
|
2013-07-18 22:02:03 -06:00
|
|
|
}
|
2013-07-29 22:23:23 -06:00
|
|
|
|
|
|
|
func readTemplate(name string) *template.Template {
|
|
|
|
if pres == nil {
|
|
|
|
panic("no global Presentation set yet")
|
|
|
|
}
|
|
|
|
path := "lib/godoc/" + name
|
|
|
|
|
|
|
|
// use underlying file system fs to read the template file
|
|
|
|
// (cannot use template ParseFile functions directly)
|
|
|
|
data, err := vfs.ReadFile(fs, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("readTemplate: ", err)
|
|
|
|
}
|
|
|
|
// be explicit with errors (for app engine use)
|
|
|
|
t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("readTemplate: ", err)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:21:09 -06:00
|
|
|
func readTemplates(p *godoc.Presentation) {
|
|
|
|
p.CallGraphHTML = readTemplate("callgraph.html")
|
|
|
|
p.DirlistHTML = readTemplate("dirlist.html")
|
|
|
|
p.ErrorHTML = readTemplate("error.html")
|
|
|
|
p.ExampleHTML = readTemplate("example.html")
|
|
|
|
p.GodocHTML = readTemplate("godoc.html")
|
|
|
|
p.ImplementsHTML = readTemplate("implements.html")
|
|
|
|
p.MethodSetHTML = readTemplate("methodset.html")
|
|
|
|
p.PackageHTML = readTemplate("package.html")
|
|
|
|
p.PackageRootHTML = readTemplate("packageroot.html")
|
|
|
|
p.SearchHTML = readTemplate("search.html")
|
|
|
|
p.SearchDocHTML = readTemplate("searchdoc.html")
|
|
|
|
p.SearchCodeHTML = readTemplate("searchcode.html")
|
|
|
|
p.SearchTxtHTML = readTemplate("searchtxt.html")
|
2013-07-29 22:23:23 -06:00
|
|
|
}
|
2015-09-23 22:33:14 -06:00
|
|
|
|
|
|
|
type fmtResponse struct {
|
|
|
|
Body string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmtHandler takes a Go program in its "body" form value, formats it with
|
|
|
|
// standard gofmt formatting, and writes a fmtResponse as a JSON object.
|
|
|
|
func fmtHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := new(fmtResponse)
|
|
|
|
body, err := format.Source([]byte(r.FormValue("body")))
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Body = string(body)
|
|
|
|
}
|
2015-09-24 20:10:38 -06:00
|
|
|
w.Header().Set("Content-type", "application/json; charset=utf-8")
|
2015-09-23 22:33:14 -06:00
|
|
|
json.NewEncoder(w).Encode(resp)
|
|
|
|
}
|