You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

  1. // Copyright 2011 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. // +build appengine
  5. package main
  6. // This file replaces main.go when running godoc under app-engine.
  7. // See README.godoc-app for details.
  8. import (
  9. "archive/zip"
  10. "log"
  11. "net/http"
  12. "path"
  13. "regexp"
  14. "golang.org/x/tools/godoc"
  15. "golang.org/x/tools/godoc/dl"
  16. "golang.org/x/tools/godoc/proxy"
  17. "golang.org/x/tools/godoc/short"
  18. "golang.org/x/tools/godoc/static"
  19. "golang.org/x/tools/godoc/vfs"
  20. "golang.org/x/tools/godoc/vfs/mapfs"
  21. "golang.org/x/tools/godoc/vfs/zipfs"
  22. "google.golang.org/appengine"
  23. )
  24. func init() {
  25. enforceHosts = !appengine.IsDevAppServer()
  26. playEnabled = true
  27. log.Println("initializing godoc ...")
  28. log.Printf(".zip file = %s", zipFilename)
  29. log.Printf(".zip GOROOT = %s", zipGoroot)
  30. log.Printf("index files = %s", indexFilenames)
  31. goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
  32. // read .zip file and set up file systems
  33. const zipfile = zipFilename
  34. rc, err := zip.OpenReader(zipfile)
  35. if err != nil {
  36. log.Fatalf("%s: %s\n", zipfile, err)
  37. }
  38. // rc is never closed (app running forever)
  39. fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
  40. fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
  41. corpus := godoc.NewCorpus(fs)
  42. corpus.Verbose = false
  43. corpus.MaxResults = 10000 // matches flag default in main.go
  44. corpus.IndexEnabled = true
  45. corpus.IndexFiles = indexFilenames
  46. if err := corpus.Init(); err != nil {
  47. log.Fatal(err)
  48. }
  49. corpus.IndexDirectory = indexDirectoryDefault
  50. go corpus.RunIndexer()
  51. pres = godoc.NewPresentation(corpus)
  52. pres.TabWidth = 8
  53. pres.ShowPlayground = true
  54. pres.ShowExamples = true
  55. pres.DeclLinks = true
  56. pres.NotesRx = regexp.MustCompile("BUG")
  57. readTemplates(pres, true)
  58. mux := registerHandlers(pres)
  59. dl.RegisterHandlers(mux)
  60. short.RegisterHandlers(mux)
  61. // Register /compile and /share handlers against the default serve mux
  62. // so that other app modules can make plain HTTP requests to those
  63. // hosts. (For reasons, HTTPS communication between modules is broken.)
  64. proxy.RegisterHandlers(http.DefaultServeMux)
  65. log.Println("godoc initialization complete")
  66. }