github.com/april1989/origin-go-tools@v0.0.32/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  	"github.com/april1989/origin-go-tools/godoc"
    15  	"github.com/april1989/origin-go-tools/godoc/redirect"
    16  	"github.com/april1989/origin-go-tools/godoc/vfs"
    17  )
    18  
    19  // This package registers "/compile" and "/share" handlers
    20  // that redirect to the golang.org playground.
    21  import _ "github.com/april1989/origin-go-tools/playground"
    22  
    23  var (
    24  	pres *godoc.Presentation
    25  	fs   = vfs.NameSpace{}
    26  )
    27  
    28  func registerHandlers(pres *godoc.Presentation) {
    29  	if pres == nil {
    30  		panic("nil Presentation")
    31  	}
    32  	mux := http.NewServeMux()
    33  	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    34  		if req.URL.Path == "/" {
    35  			http.Redirect(w, req, "/pkg/", http.StatusFound)
    36  			return
    37  		}
    38  		pres.ServeHTTP(w, req)
    39  	})
    40  	mux.Handle("/pkg/C/", redirect.Handler("/cmd/cgo/"))
    41  	mux.HandleFunc("/fmt", fmtHandler)
    42  	redirect.Register(mux)
    43  
    44  	http.Handle("/", mux)
    45  }
    46  
    47  func readTemplate(name string) *template.Template {
    48  	if pres == nil {
    49  		panic("no global Presentation set yet")
    50  	}
    51  	path := "lib/godoc/" + name
    52  
    53  	// use underlying file system fs to read the template file
    54  	// (cannot use template ParseFile functions directly)
    55  	data, err := vfs.ReadFile(fs, path)
    56  	if err != nil {
    57  		log.Fatal("readTemplate: ", err)
    58  	}
    59  	// be explicit with errors (for app engine use)
    60  	t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
    61  	if err != nil {
    62  		log.Fatal("readTemplate: ", err)
    63  	}
    64  	return t
    65  }
    66  
    67  func readTemplates(p *godoc.Presentation) {
    68  	p.CallGraphHTML = readTemplate("callgraph.html")
    69  	p.DirlistHTML = readTemplate("dirlist.html")
    70  	p.ErrorHTML = readTemplate("error.html")
    71  	p.ExampleHTML = readTemplate("example.html")
    72  	p.GodocHTML = readTemplate("godoc.html")
    73  	p.ImplementsHTML = readTemplate("implements.html")
    74  	p.MethodSetHTML = readTemplate("methodset.html")
    75  	p.PackageHTML = readTemplate("package.html")
    76  	p.PackageRootHTML = readTemplate("packageroot.html")
    77  	p.SearchHTML = readTemplate("search.html")
    78  	p.SearchDocHTML = readTemplate("searchdoc.html")
    79  	p.SearchCodeHTML = readTemplate("searchcode.html")
    80  	p.SearchTxtHTML = readTemplate("searchtxt.html")
    81  }
    82  
    83  type fmtResponse struct {
    84  	Body  string
    85  	Error string
    86  }
    87  
    88  // fmtHandler takes a Go program in its "body" form value, formats it with
    89  // standard gofmt formatting, and writes a fmtResponse as a JSON object.
    90  func fmtHandler(w http.ResponseWriter, r *http.Request) {
    91  	resp := new(fmtResponse)
    92  	body, err := format.Source([]byte(r.FormValue("body")))
    93  	if err != nil {
    94  		resp.Error = err.Error()
    95  	} else {
    96  		resp.Body = string(body)
    97  	}
    98  	w.Header().Set("Content-type", "application/json; charset=utf-8")
    99  	json.NewEncoder(w).Encode(resp)
   100  }