當前位置: 首頁>>代碼示例>>Golang>>正文


Golang http.HandleFunc函數代碼示例

本文整理匯總了Golang中net/http.HandleFunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang HandleFunc函數的具體用法?Golang HandleFunc怎麽用?Golang HandleFunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了HandleFunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: setHandlers

func (this *App) setHandlers() {
	fs := http.FileServer(http.Dir("static"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))
	http.HandleFunc("/events", this.eventsHandler)
	http.HandleFunc("/event_history", this.eventHistoryHandler)
	http.HandleFunc("/", this.dashboardHandler)
}
開發者ID:stealthly,項目名稱:edge-test,代碼行數:7,代碼來源:server.go

示例2: main

func main() {
	fmt.Println("starting server on http://localhost:3000/")
	http.HandleFunc("/", IndexHandler)
	http.HandleFunc("/redirect", CreateRedirectHandler)
	http.ListenAndServe(":3000", nil)

}
開發者ID:dsuarez4,項目名稱:GoStumbleUpon,代碼行數:7,代碼來源:main.go

示例3: setRouters

func setRouters() {
	http.HandleFunc("/", home)
	http.HandleFunc("/admin", MustLogin(admin))
	http.HandleFunc("/admin/login", login)
	http.HandleFunc("/admin/logout", MustLogin(logout))
	http.HandleFunc("/admin/list-tags", MustLogin(listTags))
}
開發者ID:kverko,項目名稱:kasiafot,代碼行數:7,代碼來源:routers.go

示例4: main

func main() {
	flag.Parse()
	templateManager, err := temple.New(*devMode, myTemplates, "templates")
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		ctx := homePageContext{
			baseContext{"Home"},
			[]string{"foo", "bar", "baz"},
		}
		// homepage composes shared templates by directly referencing "header" and "footer" templates.
		err := templateManager.Execute(w, ctx, "homepage")
		if err != nil {
			http.Error(w, err.Error(), 500)
		}
	})

	http.HandleFunc("/mc", func(w http.ResponseWriter, r *http.Request) {
		ctx := masterChildContext{
			baseContext{"Master / Child"},
			"AAAAA",
			"BBBBB",
		}
		// master / child templates let the library compose things for you
		// so no explicit reference to other templates is needed.
		err := templateManager.ExecuteMaster(w, ctx, "master", "child")
		if err != nil {
			http.Error(w, err.Error(), 500)
		}
	})
	http.ListenAndServe(":5555", nil)

}
開發者ID:captncraig,項目名稱:temple,代碼行數:35,代碼來源:main.go

示例5: main

func main() {
	flag.Parse()

	storageDriver, err := NewStorageDriver(*argDbDriver)
	if err != nil {
		glog.Fatalf("Failed to connect to database: %s", err)
	}

	containerManager, err := manager.New(storageDriver)
	if err != nil {
		glog.Fatalf("Failed to create a Container Manager: %s", err)
	}

	// Register Docker.
	if err := docker.Register(containerManager); err != nil {
		glog.Errorf("Docker registration failed: %v.", err)
	}

	// Register the raw driver.
	if err := raw.Register(containerManager); err != nil {
		glog.Fatalf("raw registration failed: %v.", err)
	}

	// Handler for static content.
	http.HandleFunc(static.StaticResource, func(w http.ResponseWriter, r *http.Request) {
		err := static.HandleRequest(w, r.URL)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	// Register API handler.
	if err := api.RegisterHandlers(containerManager); err != nil {
		glog.Fatalf("failed to register API handlers: %s", err)
	}

	// Redirect / to containers page.
	http.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

	// Register the handler for the containers page.
	http.HandleFunc(pages.ContainersPage, func(w http.ResponseWriter, r *http.Request) {
		err := pages.ServerContainersPage(containerManager, w, r.URL)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	defer glog.Flush()

	go func() {
		glog.Fatal(containerManager.Start())
	}()

	glog.Infof("Starting cAdvisor version: %q", info.VERSION)
	glog.Infof("About to serve on port ", *argPort)

	addr := fmt.Sprintf(":%v", *argPort)

	glog.Fatal(http.ListenAndServe(addr, nil))
}
開發者ID:jmyounker,項目名稱:cadvisor,代碼行數:60,代碼來源:cadvisor.go

示例6: startHttp

func startHttp() {
	http.HandleFunc("/register", register)
	http.HandleFunc("/unregister", unregister)
	laddr := fmt.Sprintf(":%d", *port)

	tlsConfig := tlsdefaults.Server()
	_, _, err := keyman.StoredPKAndCert(PKFile, CertFile, "Lantern", "localhost")
	if err != nil {
		log.Fatalf("Unable to initialize private key and certificate: %v", err)
	}
	cert, err := tls.LoadX509KeyPair(CertFile, PKFile)
	if err != nil {
		log.Fatalf("Unable to load certificate and key from %s and %s: %s", CertFile, PKFile, err)
	}
	tlsConfig.Certificates = []tls.Certificate{cert}

	log.Debugf("About to listen at %v", laddr)
	l, err := tls.Listen("tcp", laddr, tlsConfig)
	if err != nil {
		log.Fatalf("Unable to listen for tls connections at %s: %s", laddr, err)
	}

	log.Debug("About to serve")
	err = http.Serve(l, nil)
	if err != nil {
		log.Fatalf("Unable to serve: %s", err)
	}
}
開發者ID:2722,項目名稱:lantern,代碼行數:28,代碼來源:web.go

示例7: main

func main() {
	dat = make(map[string](map[string]string))
	reportlog = make(map[string](map[string]string))
	buf, _ := ioutil.ReadFile("static/data/data.json")
	if len(buf) > 0 {
		if err := json.Unmarshal(buf, &dat); err != nil {
			panic(err)
		}
	}
	buf, _ = ioutil.ReadFile("static/data/reportlog.json")
	if len(buf) > 0 {
		if err := json.Unmarshal(buf, &reportlog); err != nil {
			panic(err)
		}
	}
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/", index)
	http.HandleFunc("/build", build) //設置訪問的路由
	http.HandleFunc("/setdat", setdat)
	http.HandleFunc("/report", report)
	http.HandleFunc("/upload", UploadServer)
	err := http.ListenAndServe(dat["servermap"]["server"], nil) //設置監聽的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
開發者ID:gwenlei,項目名稱:linux,代碼行數:26,代碼來源:build.go

示例8: configSwRoutes

func configSwRoutes() {

	http.HandleFunc("/page/sw/time", func(w http.ResponseWriter, req *http.Request) {
		RenderDataJson(w, time.Now().Format("2006-01-02 15:04:05"))
	})

	http.HandleFunc("/page/sw/iprange", func(w http.ResponseWriter, req *http.Request) {
		RenderDataJson(w, strings.Join(g.Config().Switch.IpRange, "\n"))
	})

	http.HandleFunc("/page/sw/live", func(w http.ResponseWriter, req *http.Request) {
		RenderDataJson(w, len(funcs.AliveIp))
	})

	http.HandleFunc("/page/sw/list", func(w http.ResponseWriter, r *http.Request) {

		var ret [][]interface{} = make([][]interface{}, 0)
		for _, swSystem := range funcs.SwSystemInfo() {
			ret = append(ret,
				[]interface{}{
					swSystem.Ip,
					swSystem.Hostname,
					swSystem.Model,
					swSystem.Uptime,
					fmt.Sprintf("%d%%", swSystem.Cpu),
					fmt.Sprintf("%d%%", swSystem.Mem),
					fmt.Sprintf("%sms", swSystem.Ping),
				})
		}
		RenderDataJson(w, ret)
	})
}
開發者ID:heafod,項目名稱:swcollector,代碼行數:32,代碼來源:sw.go

示例9: init

func init() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/savetheme", saveThemeHandler)
	http.HandleFunc("/randomcolorswarm", randomColorWarmHandler)
	http.HandleFunc("/randomcolorshappy", randomColorHappyHandler)
	http.HandleFunc("/randomcolorssoft", randomColorSoftHandler)
}
開發者ID:TYOMINNU,項目名稱:theme-creator,代碼行數:7,代碼來源:theme-creator.go

示例10: main

func main() {

	http.HandleFunc("/locations", posting)
	http.HandleFunc("/locations/", posting)
	connectdb()
	log.Fatal(http.ListenAndServe(":8082", nil))
}
開發者ID:NamithaNinan,項目名稱:cmpe273-Assignment2,代碼行數:7,代碼來源:assignment2.go

示例11: Startserver

func Startserver(b *gib.Board, addr string) {
	board = b
	http.HandleFunc("/catalog", cataloghandler)
	http.HandleFunc("/post", posthandler)
	http.HandleFunc("/postform", postformhandler)
	http.ListenAndServe(addr, nil)
}
開發者ID:spedru,項目名稱:gib-old-as-fucking-shit,代碼行數:7,代碼來源:serve.go

示例12: main

func main() {
	fileServer := http.FileServer(http.Dir(STATIC_PATH))
	http.Handle("/", fileServer)

	http.HandleFunc("/laps", makeHandler(lapsHandler))
	http.HandleFunc(TIME_LOGS_URL, makeHandler(timeLogsHandler))
	http.HandleFunc("/results", makeHandler(resultsHandler))
	// admin pages
	http.HandleFunc("/race/", makeHandler(raceHandler))
	http.HandleFunc(TEAMS_URL, makeHandler(teamsHandler))

	var interrupted = make(chan os.Signal)
	go func() {
		<-interrupted
		closePool()
		trace("Bye bye")
		os.Exit(0)
	}()
	signal.Notify(interrupted, os.Interrupt)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fatal("error running race webserver: %v\n", err)
	}
}
開發者ID:gwenn,項目名稱:gorace,代碼行數:25,代碼來源:main.go

示例13: main

func main() {

	// set the auth parameters
	auth.Config.CookieSecret = []byte("7H9xiimk2QdTdYI7rDddfJeV")
	auth.Config.LoginSuccessRedirect = "/private"
	auth.Config.CookieSecure = false

	// create the login handler
	endpoint := "https://accounts.google.com/o/openid2/auth"
	http.Handle("/auth/login", auth.OpenId(endpoint))

	// public urls
	http.HandleFunc("/", Public)

	// private, secured urls
	http.HandleFunc("/private", auth.SecureFunc(Private))

	// logout handler
	http.HandleFunc("/auth/logout", Logout)

	println("openid demo starting on port 8080")
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err)
	}
}
開發者ID:bibinbin,項目名稱:go.auth,代碼行數:26,代碼來源:openid_demo.go

示例14: InitHandlers

func InitHandlers() {
	http.HandleFunc(UrlUploadSend, loghttp.Adapter(sendUpload))
	http.HandleFunc(UrlUploadReceive, loghttp.Adapter(receiveUpload))
	http.HandleFunc("/mnt00/", loghttp.Adapter(ServeDsFsFile))
	http.HandleFunc("/mnt01/", loghttp.Adapter(ServeDsFsFile))
	http.HandleFunc("/mnt02/", loghttp.Adapter(ServeDsFsFile))
}
開發者ID:aarzilli,項目名稱:tools,代碼行數:7,代碼來源:register_handlers.go

示例15: main

func main() {
	flag.Parse()

	yamlFile, err := ioutil.ReadFile(*configFile)

	if err != nil {
		log.Fatalf("Error reading config file: %s", err)
	}

	config := Config{}

	err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
		log.Fatalf("Error parsing config file: %s", err)
	}

	http.Handle("/metrics", prometheus.Handler())
	http.HandleFunc("/probe",
		func(w http.ResponseWriter, r *http.Request) {
			probeHandler(w, r, &config)
		})
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`<html>
            <head><title>Blackbox Exporter</title></head>
            <body>
            <h1>Blackbox Exporter</h1>
            <p><a href="/probe?target=prometheus.io&module=http_2xx">Probe prometheus.io for http_2xx</a></p>
            <p><a href="/metrics">Metrics</a></p>
            </body>
            </html>`))
	})
	if err := http.ListenAndServe(*addr, nil); err != nil {
		log.Fatalf("Error starting HTTP server: %s", err)
	}
}
開發者ID:flecno,項目名稱:blackbox_exporter,代碼行數:35,代碼來源:main.go


注:本文中的net/http.HandleFunc函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。