當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。