Ejemplo n.º 1
0
func main() {
	r := chi.NewRouter()

	r.Use(middleware.Logger)
	r.Use(httpcoala.Route("HEAD", "GET")) // or, Route("*")

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(100 * time.Millisecond) // expensive op
		w.WriteHeader(200)
		w.Write([]byte("hi"))
	})

	http.ListenAndServe(":1111", r)
}
Ejemplo n.º 2
0
func main() {
	controllers.Asset = Asset
	controllers.GitHubClient = initGitHubClient()

	top := controllers.TopController{}
	ranking := controllers.RankingController{}

	goji.Get("/assets/*", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "/"}))
	goji.Get("/frontend/*", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "/"}))
	goji.Get("/api/ranking/:username", ranking.ShowAPI)
	goji.Get("/", top.Index)

	goji.Use(httpcoala.Route("*"))
	goji.Serve()
}
Ejemplo n.º 3
0
func main() {
	var err error

	yosage.LGTM, err = Asset("lgtm.png")
	if err != nil {
		log.Fatalln(err)
	}

	controllers.Asset = Asset
	top := controllers.TopController{}
	lgtm := controllers.LgtmController{}

	goji.Get("/assets/*", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "/"}))
	goji.Get("/frontend/*", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "/"}))
	goji.Post("/api/lgtm", lgtm.CreateAPI)
	goji.Get("/", top.Index)

	goji.Use(httpcoala.Route("*"))
	goji.Serve()
}
Ejemplo n.º 4
0
func (srv *Server) NewRouter() http.Handler {
	cf := srv.Config

	conrd, err := consistentrd.New(cf.Cluster.LocalNode, cf.Cluster.Nodes)
	if err != nil {
		panic(err)
	}

	r := chi.NewRouter()

	r.Use(ParentContext(srv.Ctx))
	r.Use(middleware.RequestID)
	r.Use(middleware.RealIP)
	r.Use(middleware.Logger)
	r.Use(middleware.Recoverer)

	r.Use(middleware.ThrottleBacklog(cf.Limits.MaxRequests, cf.Limits.BacklogSize, cf.Limits.BacklogTimeout))

	r.Use(middleware.CloseNotify)
	r.Use(middleware.Timeout(cf.Limits.RequestTimeout))
	r.Use(httpcoala.Route("HEAD", "GET"))

	r.Use(heartbeat.Route("/ping"))
	r.Use(heartbeat.Route("/favicon.ico"))

	if cf.Airbrake.ApiKey != "" {
		r.Use(AirbrakeRecoverer(cf.Airbrake.ApiKey))
	}

	if srv.Config.Profiler {
		r.Mount("/debug", middleware.NoCache, Profiler())
	}

	r.Get("/", trackRoute("root"), func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("."))
	})

	r.Get("/info", trackRoute("imageInfo"), GetImageInfo)

	r.Route("/:bucket", func(r chi.Router) {
		r.Post("/", BucketImageUpload)

		r.Group(func(r chi.Router) {
			cors := cors.New(cors.Options{
				AllowedOrigins:   []string{"*"},
				AllowedMethods:   []string{"GET", "OPTIONS"},
				AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
				ExposedHeaders:   []string{"Link"},
				AllowCredentials: true,
				MaxAge:           300, // Maximum value not ignored by any of major browsers
			})
			r.Use(cors.Handler)

			r.Get("/", conrd.RouteWithParams("url"), trackRoute("bucketV1GetItem"), BucketGetIndex)
			r.Get("/fetch", conrd.RouteWithParams("url"), trackRoute("bucketV1GetItem"), BucketFetchItem)
		})

		// TODO: review
		r.Get("/add", trackRoute("bucketAddItems"), BucketAddItems)
		r.Get("/:key", conrd.Route(), BucketGetItem)
		r.Delete("/:key", conrd.Route(), BucketDeleteItem)
	})

	// DEPRECATED
	r.Get("/fetch", trackRoute("bucketV0GetItem"), BucketV0FetchItem) // for Pressilla v2 apps
	r.Get("/:bucket//fetch", conrd.RouteWithParams("url"), trackRoute("bucketV1GetItem"), BucketFetchItem)
	// --

	return r
}