Beispiel #1
0
// makeClosureHandler returns a handler to serve Closure files.
// root is either:
// 1) empty: use the Closure files compiled in to the binary (if
//    available), else redirect to the Internet.
// 2) a URL prefix: base of Camlistore to get Closure to redirect to
// 3) a path on disk to the root of camlistore's source (which
//    contains the necessary subset of Closure files)
func makeClosureHandler(root, handlerName string) (http.Handler, error) {
	// devcam server environment variable takes precedence:
	if d := os.Getenv("CAMLI_DEV_CLOSURE_DIR"); d != "" {
		log.Printf("%v: serving Closure from devcam server's $CAMLI_DEV_CLOSURE_DIR: %v", handlerName, d)
		return http.FileServer(http.Dir(d)), nil
	}
	if root == "" {
		fs, err := closurestatic.FileSystem()
		if err == os.ErrNotExist {
			log.Printf("%v: no configured setting or embedded resources; serving Closure via %v", handlerName, closureBaseURL)
			return closureBaseURL, nil
		}
		if err != nil {
			return nil, fmt.Errorf("error loading embedded Closure zip file: %v", err)
		}
		log.Printf("%v: serving Closure from embedded resources", handlerName)
		return http.FileServer(fs), nil
	}
	if strings.HasPrefix(root, "http") {
		log.Printf("%v: serving Closure using redirects to %v", handlerName, root)
		return closureRedirector(root), nil
	}

	path := filepath.Join("third_party", "closure", "lib", "closure")
	return makeFileServer(root, path, filepath.Join("goog", "base.js"))
}
Beispiel #2
0
// makeClosureHandler returns a handler to serve Closure files.
// root is either:
// 1) empty: use the Closure files compiled in to the binary (if
//    available), else redirect to the Internet.
// 2) a URL prefix: base of Camlistore to get Closure to redirect to
// 3) a path on disk to the root of camlistore's source (which
//    contains the necessary subset of Closure files)
func makeClosureHandler(root, handlerName string) (http.Handler, error) {
	// dev-server environment variable takes precendence:
	if d := os.Getenv("CAMLI_DEV_CLOSURE_DIR"); d != "" {
		log.Printf("%v: serving Closure from dev-server's $CAMLI_DEV_CLOSURE_DIR: %v", handlerName, d)
		return http.FileServer(http.Dir(d)), nil
	}
	if root == "" {
		fs, err := closurestatic.FileSystem()
		if err == os.ErrNotExist {
			log.Printf("%v: no configured setting or embedded resources; serving Closure via %v", handlerName, closureBaseURL)
			return closureBaseURL, nil
		}
		if err != nil {
			return nil, fmt.Errorf("error loading embedded Closure zip file: %v", err)
		}
		log.Printf("%v: serving Closure from embedded resources", handlerName)
		return http.FileServer(fs), nil
	}
	if strings.HasPrefix(root, "http") {
		log.Printf("%v: serving Closure using redirects to %v", handlerName, root)
		return closureRedirector(root), nil
	}
	fi, err := os.Stat(root)
	if err != nil {
		return nil, err
	}
	if !fi.IsDir() {
		return nil, errors.New("not a directory")
	}
	closureRoot := filepath.Join(root, "third_party", "closure", "lib", "closure")
	_, err = os.Stat(filepath.Join(closureRoot, "goog", "base.js"))
	if err != nil {
		return nil, fmt.Errorf("directory doesn't contain closure/goog/base.js; wrong directory?")
	}
	log.Printf("%v: serving Closure from disk: %v", handlerName, closureRoot)
	return http.FileServer(http.Dir(closureRoot)), nil
}