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