当前位置: 首页>>代码示例>>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;未经允许,请勿转载。