当前位置: 首页>>代码示例>>Golang>>正文


Golang http.HandlerFunc函数代码示例

本文整理汇总了Golang中http.HandlerFunc函数的典型用法代码示例。如果您正苦于以下问题:Golang HandlerFunc函数的具体用法?Golang HandlerFunc怎么用?Golang HandlerFunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了HandlerFunc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	err := loadTemplates()
	if err != nil {
		fmt.Println(err)
		return
	}

	homepage = "<a href=\"/sim/10/1/1\">Basic Simulation</a><br /><table><tr><td>SpawnTime</td><td>10 mins</tr><tr><td># Nurses</td><td>1</td></tr><tr><td># Doctors</td><td>1</td></tr></table>"

	// Simulation
	wd, err := os.Getwd()
	if err != nil {
		log.Println("ERROR: Failed to get Working Directory", err)
		return
	}

	//indexHtml, err := ioutil.ReadFile(wd + "/assets/index.html")
	//if err != nil { fmt.Println("ERROR:", err); return; }

	http.Handle("/assets/", http.FileServer(wd, ""))
	http.HandleFunc("/sim/", http.HandlerFunc(SimulationHandler))
	http.HandleFunc("/", http.HandlerFunc(HomeHandler))

	err = http.ListenAndServe(":6060", nil)
	log.Println("LOG: Server Shutting Down")

	if err != nil {
		log.Println("ERROR:", err)
	}

}
开发者ID:ghthor,项目名称:hospitSim.go,代码行数:31,代码来源:main.go

示例2: 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))

}
开发者ID:santinopnipa,项目名称:SUT_gopages,代码行数:34,代码来源:handler.go

示例3: 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.")
}
开发者ID:haiger,项目名称:beansdb,代码行数:59,代码来源:beansdb.go

示例4: 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())
	}
}
开发者ID:machinaut,项目名称:book-checker,代码行数:8,代码来源:server.go

示例5: 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())
	}
}
开发者ID:wtaysom,项目名称:tau,代码行数:10,代码来源:hello.go

示例6: 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())
	}
}
开发者ID:tung,项目名称:goblog,代码行数:10,代码来源:goblog.go

示例7: 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)
	}
}
开发者ID:johan,项目名称:svgo,代码行数:11,代码来源:websvg.go

示例8: 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)
	}
}
开发者ID:pquerna,项目名称:streamplay,代码行数:13,代码来源:status.go

示例9: main

/**
* Main Function
 */
func main() {
	fmt.Printf("Starting http Server ... \n")

	http.Handle("/$sys", http.HandlerFunc(handleSys))
	http.Handle("/$db", http.HandlerFunc(handleDb))

	//internal webserver should be disabled
	http.Handle("/", http.FileServer(http.Dir("web/")))

	err := http.ListenAndServe("127.0.0.1:8080", nil)
	if err != nil {
		fmt.Printf("ListenAndServe Error :" + err.String())
	}
}
开发者ID:okard,项目名称:depot,代码行数:17,代码来源:server.go

示例10: Start

func Start(ch *chan int, st *State.State) {
	world = *ch
	state = st
	go http.ListenAndServe(":25560", http.HandlerFunc(httpServe))
	go http.ListenAndServe(":25561", websocket.Handler(wssServe))
	//	os.ForkExec("http://localhost:25560/index.oc", []string{}, []string{}, "", []*os.File{})
}
开发者ID:CasualSuperman,项目名称:Ourcraft,代码行数:7,代码来源:gui.go

示例11: main

func main() {
	http.Handle("/", http.HandlerFunc(PushServer))
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		panic("ListenAndServe: ", err.String())
	}
}
开发者ID:stampzilla,项目名称:stampzilla,代码行数:7,代码来源:jonaz.go

示例12: main

func main() {
	if len(os.Args) > 1 {
		upass = os.Args[1]
	}
	t := time.UTC()
	initend = isodatestring(t)
	initbegin = isodatestring(time.SecondsToUTC(t.Seconds() -
		(secondsPerDay * maxDays)))
	showparams("init")
	http.Handle("/users/", http.HandlerFunc(tfusers))
	http.Handle("/list/", http.HandlerFunc(tflist))
	err := http.ListenAndServe(":1958", nil)
	if err != nil {
		panic("ListenAndServe: ", err.String())
	}
}
开发者ID:J5lx,项目名称:luminous,代码行数:16,代码来源:wtf.go

示例13: serveTestFiles

func serveTestFiles(t *testing.T) (l net.Listener, err os.Error) {
	foundValidPort := false
	var port_str string
	for !foundValidPort {
		port := (rand.Int() & 0x7FFF) + 0x08000
		port_str = strconv.Itoa(port)
		addr, err := net.ResolveTCPAddr("127.0.0.1:" + port_str)
		if err != nil {
			t.Error("Create TCP Address: ", err.String())
			return nil, err
		}
		l, err = net.ListenTCP("tcp4", addr)
		if err != nil {
			if err == os.EADDRINUSE || strings.LastIndex(err.String(), os.EADDRINUSE.String()) != -1 {
				continue
			}
			t.Error("Unable to listen on TCP port: ", err.String())
			return l, err
		}
		foundValidPort = true
	}
	OverrideRapleafHostPort("127.0.0.1", port_str)
	go http.Serve(l, http.HandlerFunc(ServeTestHTTP))
	return l, err
}
开发者ID:pomack,项目名称:rapleaf-bindings,代码行数:25,代码来源:rapleaf_test.go

示例14: view

func view(prefix string) http.Handler {
	f := func(w http.ResponseWriter, r *http.Request) {
		c := appengine.NewContext(r)
		p := r.URL.Path[len(prefix):]
		if p == "" {
			p = "index"
		}
		s := new(Page)
		k := datastore.NewKey(c, "string", p, 0, nil)
		if item, err := memcache.Get(c, p); err == memcache.ErrCacheMiss {
			datastore.Get(c, k, s)
			err = memcache.Set(c, &memcache.Item{Key: p, Value: []byte(s.Content)})
			if err != nil {
				panic(err)
			}
		} else if err != nil {
			panic(err)
		} else {
			s.Content = string(item.Value)
		}
		output := string(blackfriday.MarkdownCommon([]byte(s.Content)))
		viewTemplate.Execute(w, Foo{p, output, ""})
	}
	return http.HandlerFunc(f)
}
开发者ID:georgerogers42,项目名称:gwiki2,代码行数:25,代码来源:hello.go

示例15: RegisterWsHttpHandler

func (c *WsHttpCore) RegisterWsHttpHandler(id string, pattern string, methods []string) os.Error {
	if c.lookupConn(id) == nil {
		log.Println("Connection not found!", id)
		return os.NewError("connection not found")
	}
	if strings.HasPrefix(pattern, "/meta/wsconnections") {
		log.Println("Request to get async notifications about connections")
		c.AddConnectionListener(c.lookupConn(id), id)
		//c.DispatchResponse([]byte("true"),nil,connid,r.Id)
		// }
		return nil
	}

	handler := getHandlerFor(pattern, c.mux)
	fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rsp, err := c.SendReceive(id, r)
		if err != nil {
			w.WriteHeader(500)
			log.Println(pattern, "handling error", err)
			return
		}
		w.Write(rsp)
		return
	})
	for _, meth := range methods {
		handler.Add(meth, id, fn)
	}
	c.onDisconnect(id, func() {
		handler.RemoveAll(id, []string{})
	})
	log.Println("RegisterWsHttpHandler done")
	return nil
}
开发者ID:cheng81,项目名称:goexp-wshttp,代码行数:33,代码来源:core.go


注:本文中的http.HandlerFunc函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。