Route Handlers Inside a Class

Route Handlers Inside a Class

You just need to inherit from Sinatra::Base:

require "sinatra/base"

class Example < Sinatra::Base
def say_hello
"Hello"
end

get "/hello" do
say_hello
end
end

You can run your app with Example.run!.


If you need more separation between parts of your application, just make another Sinatra app. Put shared functionality in model classes and helpers, and run all your apps together with Rack.

module HelloHelpers
def say_hello
"Hello"
end
end

class Hello < Sinatra::Base
helpers HelloHelpers

get "/?" do
@message = say_hello
haml :index
end
end

class HelloAdmin < Sinatra::Base
helpers HelloHelpers

get "/?" do
@message = say_hello
haml :"admin/index"
end
end

config.ru:

map "/" do
run Hello
end

map "/admin" do
run HelloAdmin
end

Install Thin, and run your app with thin start.

Tornado: Different methods in same class in routes

Tornado uses the concept of "handlers", which, well, handle requests at a certain path. Handlers are classes. Internally Tornado selects a method from these classes corresponding to HTTP verb used in the request.

In your case, you have 2 paths: / and /login, let's call them "Home" and "Login' respectively. Now, you need to have 2 handlers: HomeHandler and LoginHandler and assign them to corresponding routes...

Routes:

(r"/", HomeHandler),
(r"/login", LoginHandler, {"db": db})

Handler classes:

class HomeHandler(BaseHandler):

def get(self):
# Will work for GET yoursite.com/, e.g. when opened in a browser
# The next line will render a template and return it to the browser
self.render("home.html")


class LoginHandler(BaseHandler):

def initialize(self, db):
# That `db` from route declaration is passed as an argument
# to this Tornado specific method
self.db = db

def get(self):
# Will work for GET yoursite.com/login, e.g. when opened in a browser
# You may use self.db here
# The next line will render a template and return it to the browser
self.render("login.html")

def post(self):
# Will work for POST yoursite.com/login, e.g. when the data
# from the form on the Login page is sent back to the server
# You may use self.db here
return

What is the intended/better way to offload route handling to a different class in Spark Java?

Setting status to 200 without using body() afterwards:

Spark.get("/", (req, res) -> {
res.status(200);
return "";
});

Delegating work to certain handlers can be done via java8 method references.
Imagine you have a method like this in a class LoginHandler:

public static String login(Request req, Response res) {
// do stuff
}

Then in your main class you could call:

Spark.get("/login", LoginHandler::login);

Regarding serialization into json or other formats I suggest you take a look at the

Response Transformer

section of the docs: http://sparkjava.com/documentation.html#response-transformer

how to handle multiple routes in the same class with tornado web server

Yes, just use the same class in your route specs:

routes = [
(r'/route1', MainHandler1),
(r'/route2', MainHandler1)
]
application = tornado.web.Application(routes, **settings)

EDIT re "how will I differentiate route1 and route2 in MainHandler1":

I would suggest you not to tie your handler to any explicit routes; instead try to parametrise it based on variable parts of the route. If we take your original example, where you have two routes differing by a number and serving a different template based on that number, you might have something like:

class MainHandler(tornado.web.RequestHandler):
def get(self, page_num):
self.render("page{}.html".format(page_num))

routes = [
(r'/route(\d+)', MainHandler),
]
application = tornado.web.Application(routes, **settings)

This way you define one route, but effectively have as many as you have templates. On the other hand, if you need a completely different response for each route, it's much better to keep them in separate handlers.

How to create routes with FastAPI within a class

For creating class-based views you can use @cbv decorator from fastapi-utils. The motivation of using it:

Stop repeating the same dependencies over and over in the signature of related endpoints.

Your sample could be rewritten like this:

from fastapi import Depends, FastAPI
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter


def get_x():
return 10


app = FastAPI()
router = InferringRouter() # Step 1: Create a router


@cbv(router) # Step 2: Create and decorate a class to hold the endpoints
class Foo:
# Step 3: Add dependencies as class attributes
x: int = Depends(get_x)

@router.get("/somewhere")
def bar(self) -> int:
# Step 4: Use `self.<dependency_name>` to access shared dependencies
return self.x


app.include_router(router)

Routing events by the event object type

try this:

((dynamic)this).On((dynamic)e);


Related Topics



Leave a reply



Submit