Popcorn application.

The App is the main point of the application. It acts as a Router that holds the top level route handlers.

Here an example to create a simple web app with Popcorn:

import popcorn

class HelloHandler
    super Handler

    redef fun get(req, res) do res.html "<h1>Hello World!</h1>"
end

var app = new App
app.use("/", new HelloHandler)

The Popcorn app listens on port 3000 for connections. The app responds with "Hello World!" for request to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.

The req (request) and res (response) parameters are the same that nitcorn provides so you can do anything else you would do in your route without Popcorn involved.

Run the app with the following command:

nitc app.nit && ./app

Then, load http://localhost:3000 in a browser to see the output.

Introduced properties

fun error_tpl(status: Int, message: nullable String): Template

popcorn :: App :: error_tpl

fun listen(host: String, port: Int)

popcorn :: App :: listen

Start listening on host:port.
fun quiet: Bool

popcorn :: App :: quiet

Do not show anything on console
fun quiet=(quiet: Bool)

popcorn :: App :: quiet=

Do not show anything on console
fun register_task(task: PopTask)

popcorn :: App :: register_task

Register a new task in self
fun run_tasks

popcorn :: App :: run_tasks

Run all registered tasks
fun tasks: Array[PopTask]

popcorn :: App :: tasks

Tasks to run
protected fun tasks=(tasks: Array[PopTask])

popcorn :: App :: tasks=

Tasks to run

Redefined properties

redef type SELF: App

popcorn $ App :: SELF

Type of this instance, automatically specialized in every class
redef fun answer(req: HttpRequest, uri: String): HttpResponse

popcorn :: popcorn $ App :: answer

Handle request from nitcorn

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type BODY: Serializable

popcorn :: Handler :: BODY

Kind of objects returned by deserialize_body
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun all(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: all

Handler to all kind of HTTP request methods.
abstract fun answer(request: HttpRequest, truncated_uri: String): HttpResponse

nitcorn :: Action :: answer

Prepare a HttpResponse destined to the client in response to the request
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun delete(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: delete

DELETE handler.
fun deserialize_body(req: HttpRequest, res: HttpResponse): nullable BODY

popcorn :: Handler :: deserialize_body

Deserialize the request body
fun error_tpl(status: Int, message: nullable String): Template

popcorn :: App :: error_tpl

fun get(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: get

GET handler.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun listen(host: String, port: Int)

popcorn :: App :: listen

Start listening on host:port.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun post(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: post

POST handler.
protected fun prepare_respond_and_close(request: HttpRequest, truncated_uri: String, http_server: HttpServer)

nitcorn :: Action :: prepare_respond_and_close

Full to a request with sending the response and closing of the http_server
fun put(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: put

PUT handler.
fun quiet: Bool

popcorn :: App :: quiet

Do not show anything on console
fun quiet=(quiet: Bool)

popcorn :: App :: quiet=

Do not show anything on console
fun register_task(task: PopTask)

popcorn :: App :: register_task

Register a new task in self
fun run_tasks

popcorn :: App :: run_tasks

Run all registered tasks
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun tasks: Array[PopTask]

popcorn :: App :: tasks

Tasks to run
protected fun tasks=(tasks: Array[PopTask])

popcorn :: App :: tasks=

Tasks to run
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun use(path: String, handler: Handler)

popcorn :: Router :: use

Register a handler for a route path.
fun use_after(path: String, handler: Handler)

popcorn :: Router :: use_after

Register a post-handler for a route path.
fun use_before(path: String, handler: Handler)

popcorn :: Router :: use_before

Register a pre-handler for a route path.
fun validate_body(req: HttpRequest, res: HttpResponse): nullable String

popcorn :: Handler :: validate_body

Validate body input with validator
fun validator: nullable DocumentValidator

popcorn :: Handler :: validator

Validator used to check body input
protected fun validator=(validator: nullable DocumentValidator)

popcorn :: Handler :: validator=

Validator used to check body input
package_diagram popcorn::App App popcorn::Router Router popcorn::App->popcorn::Router nitcorn::Action Action popcorn::App->nitcorn::Action popcorn::Handler Handler popcorn::Router->popcorn::Handler core::Object Object nitcorn::Action->core::Object ...popcorn::Handler ... ...popcorn::Handler->popcorn::Handler ...core::Object ... ...core::Object->core::Object

Ancestors

abstract class Handler

popcorn :: Handler

Class handler for a route.
interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Action

nitcorn :: Action

Action executed to answer a request
class Router

popcorn :: Router

Mountable routers

Class definitions

popcorn $ App
# Popcorn application.
#
# The `App`  is the main point of the application.
# It acts as a `Router` that holds the top level route handlers.
#
# Here an example to create a simple web app with Popcorn:
#
# ~~~
# import popcorn
#
# class HelloHandler
#	super Handler
#
#	redef fun get(req, res) do res.html "<h1>Hello World!</h1>"
# end
#
# var app = new App
# app.use("/", new HelloHandler)
# # app.listen("localhost", 3000)
# ~~~
#
# The Popcorn app listens on port 3000 for connections.
# The app responds with "Hello World!" for request to the root URL (`/`) or **route**.
# For every other path, it will respond with a **404 Not Found**.
#
# The `req` (request) and `res` (response) parameters are the same that nitcorn provides
# so you can do anything else you would do in your route without Popcorn involved.
#
# Run the app with the following command:
#
# ~~~bash
# nitc app.nit && ./app
# ~~~
#
# Then, load [http://localhost:3000](http://localhost:3000) in a browser to see the output.
class App
	super Router
end
lib/popcorn/pop_handlers.nit:383,1--420,3

popcorn :: pop_tasks $ App
redef class App

	# Tasks to run
	var tasks = new Array[PopTask]

	# Register a new task in `self`
	fun register_task(task: PopTask) do tasks.add task

	# Run all registered tasks
	fun run_tasks do for task in tasks do task.start
end
lib/popcorn/pop_tasks.nit:68,1--78,3

popcorn :: popcorn $ App
# App acts like a wrapper around a nitcorn `Action`.
redef class App
	super Action

	# Do not show anything on console
	var quiet = false is writable

	# Start listening on `host:port`.
	fun listen(host: String, port: Int) do
		var iface = "{host}:{port}"
		var vh = new VirtualHost(iface)

		vh.routes.add new Route("/", self)

		var fac = new HttpFactory.and_libevent
		fac.config.virtual_hosts.add vh

		if not quiet then
			print "Launching server on http://{iface}/"
		end
		fac.run
	end

	# Handle request from nitcorn
	redef fun answer(req, uri) do
		uri = uri.simplify_path
		var res = new HttpResponse(404)
		for route, handler in pre_handlers do
			handler.handle(route, uri, req, res)
		end
		for route, handler in handlers do
			handler.handle(route, uri, req, res)
			if res.sent then break
		end
		if not res.sent then
			res.send(error_tpl(res.status_code, res.status_message), 404)
		end
		for route, handler in post_handlers do
			handler.handle(route, uri, req, res)
		end
		res.session = req.session
		return res
	end

	#
	fun error_tpl(status: Int, message: nullable String): Template do
		return new ErrorTpl(status, message)
	end
end
lib/popcorn/popcorn.nit:26,1--74,3