本文整理汇总了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)
}
示例2: main
func main() {
fmt.Println("starting server on http://localhost:3000/")
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/redirect", CreateRedirectHandler)
http.ListenAndServe(":3000", nil)
}
示例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))
}
示例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)
}
示例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))
}
示例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)
}
}
示例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)
}
}
示例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)
})
}
示例9: init
func init() {
http.HandleFunc("/", handler)
http.HandleFunc("/savetheme", saveThemeHandler)
http.HandleFunc("/randomcolorswarm", randomColorWarmHandler)
http.HandleFunc("/randomcolorshappy", randomColorHappyHandler)
http.HandleFunc("/randomcolorssoft", randomColorSoftHandler)
}
示例10: main
func main() {
http.HandleFunc("/locations", posting)
http.HandleFunc("/locations/", posting)
connectdb()
log.Fatal(http.ListenAndServe(":8082", nil))
}
示例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)
}
示例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)
}
}
示例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)
}
}
示例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))
}
示例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)
}
}