Exemplo n.º 1
0
func main() {
	router := fasthttprouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
}
Exemplo n.º 2
0
func newRouter(g *Engine) *Router {
	fastR := fasthttprouter.New()
	r := &Router{}
	r.Router = fastR
	r.g = g

	return r
}
Exemplo n.º 3
0
func main() {
	user := []byte("gordon")
	pass := []byte("secret!")

	router := fasthttprouter.New()
	router.GET("/", Index)
	router.GET("/protected/", BasicAuth(Protected, user, pass))

	log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
}
Exemplo n.º 4
0
func main() {
	// Initialize a router as usual
	router := fasthttprouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	// Make a new HostSwitch and insert the router (our http handler)
	// for example.com and port 12345
	hs := make(HostSwitch)
	hs["example.com:12345"] = router.Handler

	// Use the HostSwitch to listen and serve on port 12345
	log.Fatal(fasthttp.ListenAndServe(":12345", hs.CheckHost))
}
Exemplo n.º 5
0
func main() {
	var (
		port = flag.Int("port", 8000, "Port to listen on")
	)
	flag.Parse()
	zing.Logger.SetHandler(log.LvlFilterHandler(log.LvlDebug, log.StdoutHandler))

	go func() {
		http.ListenAndServe("localhost:6060", nil)
	}()

	router := fasthttprouter.New()
	router.GET("/album/", zingAlbumHandler)
	fasthttp.ListenAndServe(fmt.Sprintf(":%d", *port), router.Handler)
}
Exemplo n.º 6
0
func newPubServer(httpAddr, httpsAddr string, maxClients int, gw *Gateway) *pubServer {
	this := &pubServer{
		name:   "fastpub",
		gw:     gw,
		router: fasthttprouter.New(),
	}

	logger := golog.New(os.Stdout, "fasthttp ", golog.LstdFlags|golog.Lshortfile)
	if httpAddr != "" {
		this.httpServer = &fastServerWithAddr{
			Server: &fasthttp.Server{
				Name:                 "kateway",
				Concurrency:          maxClients,
				MaxConnsPerIP:        5000, // TODO
				MaxRequestsPerConn:   0,    // unlimited
				MaxKeepaliveDuration: options.HttpReadTimeout,
				ReadTimeout:          options.HttpReadTimeout,
				WriteTimeout:         options.HttpWriteTimeout,
				MaxRequestBodySize:   int(options.MaxPubSize + 1),
				ReduceMemoryUsage:    false, // TODO
				Handler:              this.router.Handler,
				Logger:               logger,
			},
			Addr: httpAddr,
		}
	}

	if httpsAddr != "" {
		this.httpsServer = &fastServerWithAddr{
			Server: &fasthttp.Server{
				Name:                 "kateway",
				Concurrency:          maxClients,
				MaxConnsPerIP:        5000, // TODO
				MaxRequestsPerConn:   0,    // unlimited
				MaxKeepaliveDuration: options.HttpReadTimeout,
				ReadTimeout:          options.HttpReadTimeout,
				WriteTimeout:         options.HttpWriteTimeout,
				MaxRequestBodySize:   int(options.MaxPubSize + 1),
				ReduceMemoryUsage:    false, // TODO
				Handler:              this.router.Handler,
				Logger:               logger,
			},
			Addr: httpsAddr,
		}
	}

	return this
}
Exemplo n.º 7
0
func main() {
	flag.Parse()

	r := fasthttprouter.New()
	errCh := make(chan error, 10)

	log.Printf("Starting HTTP server on port %s\n", *httpAddr)
	log.Printf("pprof server on port %s\n", *pprofAddr)

	r.GET("/api", handler.Info)
	r.GET("/api/posts", handler.GetAllPosts)
	r.GET("/api/posts/:id", handler.GetPostJSON)
	r.POST("/api/posts", handler.SetPost)
	r.ServeFiles("/static/*filepath", "./static")

	// pprof server
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("go to /pprof/debug"))
	})

	go func() {
		errCh <- fasthttp.ListenAndServe(":"+*httpAddr, r.Handler)
	}()
	go func() {
		errCh <- http.ListenAndServe(":"+*pprofAddr, nil)
	}()

	signalCh := make(chan os.Signal, 1)
	signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

	for {
		select {
		case err := <-errCh:
			if err != nil {
				log.Fatalf("%s\n", err.Error())
			}
		case s := <-signalCh:
			log.Printf("Captured %v. Exiting...", s)
			os.Exit(0)
		}
	}

}
Exemplo n.º 8
0
func startFastHttpRouter() {
	mux := fasthttprouter.New()
	mux.GET("/hello", fastHttpHandler)
	fasthttp.ListenAndServe(":"+strconv.Itoa(port), mux.Handler)
}