golang.org/x/tools@v0.21.0/cmd/godoc/handlers.go (about)

     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  
     5  package main
     6  
     7  import (
     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  
    21  var (
    22  	pres *godoc.Presentation
    23  	fs   = vfs.NameSpace{}
    24  )
    25  
    26  func registerHandlers(pres *godoc.Presentation) {
    27  	if pres == nil {
    28  		panic("nil Presentation")
    29  	}
    30  	mux := http.NewServeMux()
    31  	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    32  		if req.URL.Path == "/" {
    33  			http.Redirect(w, req, "/pkg/", http.StatusFound)
    34  			return
    35  		}
    36  		pres.ServeHTTP(w, req)
    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  
    45  func 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  	data, err := vfs.ReadFile(fs, path)
    54  	if err != nil {
    55  		log.Fatal("readTemplate: ", err)
    56  	}
    57  	// be explicit with errors (for app engine use)
    58  	t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
    59  	if err != nil {
    60  		log.Fatal("readTemplate: ", err)
    61  	}
    62  	return t
    63  }
    64  
    65  func 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  
    81  type 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.
    88  func fmtHandler(w http.ResponseWriter, r *http.Request) {
    89  	resp := new(fmtResponse)
    90  	body, err := 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  }