github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/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  )
    18  
    19  func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
    20  	w.WriteHeader(http.StatusNotFound)
    21  	servePage(w, Page{
    22  		Title:    "File " + relpath,
    23  		Subtitle: relpath,
    24  		Body:     applyTemplate(errorHTML, "errorHTML", err), // err may contain an absolute path!
    25  	})
    26  }
    27  
    28  func init() {
    29  	log.Println("initializing godoc ...")
    30  	log.Printf(".zip file   = %s", zipFilename)
    31  	log.Printf(".zip GOROOT = %s", zipGoroot)
    32  	log.Printf("index files = %s", indexFilenames)
    33  
    34  	// initialize flags for app engine
    35  	*goroot = path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
    36  	*indexEnabled = true
    37  	*indexFiles = indexFilenames
    38  	*maxResults = 100    // reduce latency by limiting the number of fulltext search results
    39  	*indexThrottle = 0.3 // in case *indexFiles is empty (and thus the indexer is run)
    40  	*showPlayground = true
    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("/", NewZipFS(rc, zipFilename), *goroot, bindReplace)
    50  
    51  	// initialize http handlers
    52  	readTemplates()
    53  	initHandlers()
    54  	registerPublicHandlers(http.DefaultServeMux)
    55  	registerPlaygroundHandlers(http.DefaultServeMux)
    56  
    57  	// initialize default directory tree with corresponding timestamp.
    58  	initFSTree()
    59  
    60  	// Immediately update metadata.
    61  	updateMetadata()
    62  
    63  	// initialize search index
    64  	if *indexEnabled {
    65  		go indexer()
    66  	}
    67  
    68  	log.Println("godoc initialization complete")
    69  }