Exemplo n.º 1
0
func makeHandler() http.Handler {
	router := httprouter.New()

	GET(router, "/", HandleIndex)
	GET(router, "/auth/user", HandleAuthUser)
	POST(router, "/auth/login", HandleLogin)
	POST(router, "/auth/register", HandleRegister)
	POST(router, "/auth/logout", HandleLogout)
	POST(router, "/auth/stripe", Authed(HandleUpdateStripe))
	POST(router, "/model/create", Authed(HandleCreateModel))
	GET(router, "/user/username/:username", HandleUserByUsername)
	GET(router, "/models/username/:username", HandleModelsByUsername)
	GET(router, "/models/public/latest", HandleLatestPublicModels)
	GET(router, "/models/public/top/:period", HandleTopPublicModels)
	GET(router, "/model/username/:username/slug/:slug", HandleModelByUsernameAndSlug)
	POST(router, "/model/id/:id/readme", Authed(HandleUpdateModelReadme))
	POST(router, "/model/id/:id/deleted", Authed(HandleDeleteModel))
	POST(router, "/file/:username/:slug/:framework/:filename", Authed(HandleFileUpload))
	GET(router, "/file/:username/:slug/:framework/:filename", HandleFile)
	GET(router, "/file-id/:id", HandleFileById)
	GET(router, "/file-versions/:username/:slug/:framework/:filename", HandleFileVersions)
	GET(router, "/model/username/:username/slug/:slug/latest-files", HandleLatestFilesByUsernameAndSlug)

	n := negroni.New(negroni.NewLogger())

	// In production, redirect all traffic to https (except /, for LB health check)
	if utils.Conf.Production {
		n.UseHandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
			if r.URL.Path != "/" && r.Header.Get("X-Forwarded-Proto") != "https" {
				http.RedirectHandler(
					"https://"+r.Host+r.URL.RequestURI(),
					http.StatusFound,
				).ServeHTTP(rw, r)
			}
		})
	}

	n.Use(gzip.Gzip(gzip.BestCompression))
	n.Use(negronilogrus.NewMiddleware())
	n.UseHandler(router)

	return n
}
Exemplo n.º 2
0
func (a *API) Serve(addr string) {
	router := mux.NewRouter()

	v1 := router.PathPrefix("/1").Subrouter()

	// tasks
	tasksHandler := TasksHandler{a.store.Tasks()}

	task := v1.PathPrefix("/tasks/{id}").Subrouter()
	task.Methods("GET").HandlerFunc(tasksHandler.Get)
	task.Methods("PUT").HandlerFunc(tasksHandler.Set)
	task.Methods("DELETE").HandlerFunc(tasksHandler.Delete)
	task.Methods("POST", "PATCH").HandlerFunc(MethodNotAllowed)

	tasks := v1.PathPrefix("/tasks").Subrouter()
	tasks.Methods("GET").HandlerFunc(tasksHandler.List)
	tasks.Methods("POST").HandlerFunc(tasksHandler.Set)
	tasks.Methods("PUT", "DELETE", "PATCH").HandlerFunc(MethodNotAllowed)

	// events
	eventsHandler := EventsHandler{a.store.Events(), a.Events}

	event := v1.PathPrefix("/events/{id}").Subrouter()
	event.Methods("GET").HandlerFunc(eventsHandler.Get)
	event.Methods("PUT", "POST", "DELETE", "PATCH").HandlerFunc(MethodNotAllowed)

	events := v1.PathPrefix("/events").Subrouter()
	events.Methods("GET").HandlerFunc(eventsHandler.List)
	events.Methods("POST").HandlerFunc(eventsHandler.Create)
	events.Methods("PUT", "DELETE", "PATCH").HandlerFunc(MethodNotAllowed)

	// set up event handlers

	n := negroni.New()
	n.Use(negroni.NewRecovery())
	n.Use(negronilogrus.NewMiddleware())
	n.Use(gzip.Gzip(gzip.DefaultCompression))
	n.UseHandler(router)

	logrus.WithField("addr", addr).Info("listening")
	graceful.Run(addr, 10*time.Second, n)
}