示例#1
0
// initConfig reads server config file into the config global var.
// Args provided to this func take precedence over config file values.
func initConfig(configPath, addr string) error {
	file, err := os.Open(configPath)
	if err != nil {
		return err
	}
	defer file.Close()
	if err := json.NewDecoder(file).Decode(&config); err != nil {
		return err
	}
	if config.Schedule.Location, err = time.LoadLocation(config.Schedule.Timezone); err != nil {
		return err
	}
	if addr != "" {
		config.Addr = addr
	}
	if config.Prefix == "" || config.Prefix[0] != '/' {
		config.Prefix = "/" + config.Prefix
	}
	sort.Strings(config.Whitelist)
	sort.Strings(config.Survey.Answers)

	// init http/2 preload manifest even if the file doesn't exist
	p := filepath.Dir(configPath)
	if p != "." {
		p = filepath.Join(p, "..")
	}
	p = filepath.Join(p, "h2preload.json")
	if h2config, err = http2preload.ReadManifest(p); err != nil {
		h2config = http2preload.Manifest{}
	}

	return nil
}
示例#2
0
// init registers HTTP handlers.
func init() {
	m, err := http2preload.ReadManifest("preload.json")
	if err != nil {
		panic(err)
	}
	http.Handle("/", m.Handler(handleRoot))
	http.HandleFunc("/img.png", handleImg)
	http.HandleFunc("/gopher", handleGopher)
}
示例#3
0
func TestH2Preload(t *testing.T) {
	defer preserveConfig()()
	// verify we have a h2preload config file
	var err error
	if h2config, err = http2preload.ReadManifest("h2preload.json"); err != nil {
		t.Fatalf("h2preload: %v", err)
	}
	// replace actual file content with test entries
	h2config = http2preload.Manifest{
		"home": {
			"elements/elements.html": http2preload.AssetOpt{Type: "document"},
			"elements/elements.js":   http2preload.AssetOpt{Type: "script"},
			"styles/main.css":        http2preload.AssetOpt{Type: "style"},
		},
	}
	config.Prefix = "/root"

	r, _ := aetestInstance.NewRequest("GET", "/", nil)
	r.Host = "example.org"
	w := httptest.NewRecorder()
	serveTemplate(w, r)

	if w.Code != http.StatusOK {
		t.Errorf("w.Code = %d; want %d", w.Code, http.StatusOK)
	}
	links := strings.Join(w.Header()["Link"], "\n")
	want := []string{
		"https://example.org/root/elements/elements.html",
		"https://example.org/root/elements/elements.js",
		"https://example.org/root/styles/main.css",
	}
	for _, v := range want {
		if !strings.Contains(links, v) {
			t.Errorf("want %s in\n%v", v, links)
		}
	}
}