本文整理汇总了Golang中http.Handle函数的典型用法代码示例。如果您正苦于以下问题:Golang Handle函数的具体用法?Golang Handle怎么用?Golang Handle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Handle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: startServer
func startServer() {
http.Handle("/echo", Handler(echoServer))
http.Handle("/echoDraft75", Draft75Handler(echoServer))
server := httptest.NewServer(nil)
serverAddr = server.Listener.Addr().String()
log.Print("Test WebSocket server listening on ", serverAddr)
}
示例2: StartHtmlHandler
// starts http handlers for HTML content based on the given configuration file
// optional parameters: default.contentDirectory (location of html content to be served at https://example.com/ or https://example.com/html/index.html
func StartHtmlHandler() {
if contentDir, _ := configFile.GetString("default", "contentDirectory"); contentDir != "" {
logger.Printf("StartHtmlHandler(): serving HTML content from [%v]", contentDir)
http.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir(contentDir))))
http.Handle("/", http.RedirectHandler("/html/", http.StatusTemporaryRedirect))
}
}
示例3: init
func init() {
fmt.Println("Selog filekkkk")
http.Handle("/index", http.HandlerFunc(Render))
http.Handle("/index.ghtml", http.HandlerFunc(RenderGoPagesForbidden))
http.Handle("/", http.HandlerFunc(func(conn http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/" {
defaultPage := "index"
if strings.TrimSpace(defaultPage) != "" {
http.Redirect(conn, defaultPage, 307)
}
return
}
val := "src" + request.URL.Path
input, err := os.OpenFile(val, os.O_RDONLY, 0666)
// input,err := os.Open(val)
if err != nil {
conn.WriteHeader(404)
conn.Write([]byte("<h1>404 Not Found</h1>"))
return
}
s, _ := input.Stat()
conn.Header.Set("Content-Length", fmt.Sprintf("%d(MISSING)", s.Size))
// conn.SetHeader("Content-Type", mime.TypeByExtension(strings.ToLower(path.Ext(val))))
fmt.Sprintf("%d(MISSING)", s.Size)
mime.TypeByExtension(strings.ToLower(path.Ext(val)))
conn.WriteHeader(200)
http.ServeFile(conn, request, val)
}))
http.Handle("/src", http.HandlerFunc(RenderGoPagesForbidden))
http.Handle("/pages", http.HandlerFunc(RenderGoPagesForbidden))
}
示例4: main
func main() {
log.SetFlags(0)
flag.Parse()
cgiHandler := &cgi.Handler{
Path: *cgitPath,
Env: []string{},
InheritEnv: []string{
"CGIT_CONFIG",
},
}
if *config != "" {
cgiHandler.Env = append(cgiHandler.Env,
"CGIT_CONFIG="+*config)
}
fs := http.FileServer(http.Dir(*cgitRes))
http.Handle("/cgit.css", fs)
http.Handle("/cgit.png", fs)
http.Handle("/", cgiHandler)
err := http.ListenAndServe(*addr+":"+strconv.Itoa(*port), nil)
if err != nil {
log.Fatal(err)
}
// Everything seems to work: daemonize (close file handles)
os.Stdin.Close()
os.Stdout.Close()
os.Stderr.Close()
}
示例5: main
func main() {
mode := flag.Int("mode", 0, "0: Broadcast, 1: LIFO, 2: FILO")
flag.Parse()
config := pusher.DefaultConfiguration
config.GCInterval = 0 // disable garbage collecting
config.ConcurrencyMode = *mode
// Create a new pusher context, where all publisher and subscriber
// locations are statically mapped to "static"-channel.
push := pusher.New(pusher.StaticAcceptor("static"), config)
http.Handle("/pub", push.PublisherHandler)
http.Handle("/sub", push.SubscriberHandler)
http.Handle("/", http.FileServer("www/", "/"))
// Create the "static"-channel explictly, since AllowChannelCreation is false
// in the DefaultConfiguration.
channel, _ := push.Channel("static")
go func() {
i := 0
for {
channel.PublishString(fmt.Sprintf("--- Greetings from the server #%d ---", i), false)
time.Sleep(10e9)
i++
}
}()
log.Print("Tune your browser tab(s) to http://localhost:8080/")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
示例6: main
func main() {
flag.Parse()
runtime.GOMAXPROCS(*threads)
http.Handle("/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
http.Handle("/pprof/heap", http.HandlerFunc(pprof.Heap))
http.Handle("/pprof/symbol", http.HandlerFunc(pprof.Symbol))
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()
if *accesslog != "" {
logf, err := os.OpenFile(*accesslog, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Print("open " + *accesslog + " failed")
return
}
memcache.AccessLog = log.New(logf, "", log.Ldate|log.Ltime)
} else if *debug {
memcache.AccessLog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
}
tbefore := int64(0)
if *before != "" {
t, err := time.Parse(time.RFC3339[:len(*before)], *before)
if err != nil {
log.Print("parse time error", err.String())
return
}
t.ZoneOffset = 8 * 60 * 60
tbefore = t.Seconds()
log.Print("load data before", t.Format(time.RFC3339))
}
log.Print("start to open db ", *dbpath)
store := NewStore(*dbpath, *dbdepth, tbefore)
defer store.Close()
addr := fmt.Sprintf("%s:%d", *listen, *port)
s := memcache.NewServer(store)
e := s.Listen(addr)
if e != nil {
log.Print("Listen at ", *listen, "failed")
return
}
// monitor mem usage
go func() {
ul := uint64(*memlimit) * 1024 * 1024
for runtime.MemStats.HeapSys < ul {
time.Sleep(1e9)
}
log.Print("Mem used by Go is over limitation ", runtime.MemStats.HeapSys/1024/1024, *memlimit)
s.Shutdown()
}()
s.Serve()
log.Print("shut down gracefully.")
}
示例7: main
func main() {
http.Handle("/admin", http.HandlerFunc(admin))
http.Handle("/banana", http.HandlerFunc(banana))
err := http.ListenAndServe(":11118", nil)
if err != nil {
panic("ListenAndServe: ", err.String())
}
}
示例8: main
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
http.Handle("/lobby", websocket.Handler(LobbyServer))
http.Handle("/", http.FileServer(http.Dir("/tmp")))
fmt.Println("Listening on:", listenAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
panic("ListenAndServe: " + err.String())
}
}
示例9: Serve
func Serve(listener net.Listener) {
http.HandleFunc("/", viewHtml)
http.HandleFunc("/$stats.html", statsHtml)
http.Handle("/$main.js", stringHandler{"application/javascript", main_js})
http.Handle("/$main.css", stringHandler{"text/css", main_css})
http.HandleFunc("/$events/", evServer)
http.Serve(listener, nil)
}
示例10: main
func main() {
fmt.Printf("Starting http Server ... ")
http.Handle("/args", http.HandlerFunc(ArgServer))
http.Handle("/hello", http.HandlerFunc(sayHello))
http.Handle("/sine", http.HandlerFunc(sineServer))
err := http.ListenAndServe("0.0.0.0:8080", nil)
if err != nil {
fmt.Printf("ListenAndServe Error :" + err.String())
}
}
示例11: main
func main() {
http.Handle("/blog", http.HandlerFunc(views.Index))
http.Handle("/blog/entry/add", http.HandlerFunc(views.AddEntry))
http.Handle("/blog/entry/", http.HandlerFunc(views.Entry))
http.Handle("/blog/comment/add", http.HandlerFunc(views.AddComment))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: ", err.String())
}
}
示例12: startServer
func startServer() {
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
if e != nil {
log.Exitf("net.Listen tcp :0 %v", e)
}
serverAddr = l.Addr().String()
log.Print("Test WebSocket server listening on ", serverAddr)
http.Handle("/echo", Handler(echoServer))
http.Handle("/echoDraft75", Draft75Handler(echoServer))
go http.Serve(l, nil)
}
示例13: main
func main() {
flag.Parse()
http.Handle("/circle/", http.HandlerFunc(circle))
http.Handle("/rect/", http.HandlerFunc(rect))
http.Handle("/arc/", http.HandlerFunc(arc))
http.Handle("/text/", http.HandlerFunc(text))
err := http.ListenAndServe(*port, nil)
if err != nil {
log.Println("ListenAndServe:", err)
}
}
示例14: Serve
func Serve(listener net.Listener) {
prefix := "/d/" + ClusterName
evPrefix = "/events" + prefix
http.Handle("/", http.RedirectHandler("/view/d/"+ClusterName+"/", 307))
http.HandleFunc("/view/", viewHtml)
http.Handle("/main.js", stringHandler{"application/javascript", main_js})
http.Handle("/main.css", stringHandler{"text/css", main_css})
http.HandleFunc(evPrefix+"/", evServer)
http.Serve(listener, nil)
}
示例15: Run
func Run(addr string) {
// Configure the delimiters
// Parse the file once
statsTempl.SetDelims("<?", "?>")
statsTempl.ParseFile(statsTemplatePath)
http.Handle("/stats", http.HandlerFunc(Stats))
http.Handle("/add/", http.HandlerFunc(Add))
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}