Starting up an Application

The apy.main.Application class is the heart of the apy system, this class will be your main entry point, and will manage your configuration files, run your web server, manage the router and the dispatcher, etc.

The directory structure

myapplication/
    config/
        settings_prod.yml
    __init__.py
    index.py

The config/ dir contains the configuration files.

  • The settings_*.yml file is the main configuration file. Example:
imports:
  - others_parameters.yml
  - foo.yml

server_adapter: tornado

server:
  port: 80
  workers: 5

routes:
  hello_world:
    path:       /hello/world
    methods:    [GET]
    controller: myapplication.hello.HelloWorldController
  foo_bar:
    path:       /foo/bar
    methods:    [GET, POST]
    controller: myapplication.foo.FooBarController

debug: no

error_handler: apy.application.DefaultErrorHandler

parameters:
  foo: bar
  hello: world

services:
  my_mailer:
    class: myapplication.mymailer.Mailer
    arguments:
      name: '{{ foo }}'
  • The imports will include the config of the desired.yml (you can nest as many as you want).

    The parameters, routes and services imported sub-elements will be merged with the importer’s sub-elements. All other parameters will be overwritten with no-merge. At the two cases (merge and no-merge), at conflicts, the importer has more priority and will overwrite the imported others.

    You can use the imports to split your routes, parameters, etc in single files, etc.

  • The routes sets the application routes. (see routing)

  • The services and parameters defines the container. (see container)

Running the application as server

In the previous directory structure example, the next file is named index.py, but you can name it as you want.

1
2
3
4
5
from apy.main import Application


application = Application()
application.run_server()

Note

By default the Application(environment='prod', config_dir='config') will use the config relative directory, (you can use other directory name, or specify an absolute path) and the settings_prod.yml will be chosen (if you set the environment as dev, the application will use the settings_dev.yml).