Skip to content

ApplicationSettings

Pierre Quentel edited this page Apr 24, 2016 · 1 revision

Introduction

The server can run one or several applications. By default, starting the server with

Karrigell.run()

means that there is only one application, serving scripts and documents from the directory where the server script stands

This page explains how you can change these default settings

Application settings

An application is described by a subclass of the class Karrigell.App. Each subclass defines the following attributes :

###root_url A string beginning with a forward slash (/), indicates the first part of the urls served by this application. Defaults to "/"

###root_dir The directory in the file system to serve scripts and documents from. Defaults to the directory where the server script stands

###session_storage_class The class used to manage the storage and retrieval of session objects (see SessionManagement). Defaults to the class SQLiteSessionManagement in module <font color#"#336633" size"2">Karrigell.sessions

###users_db An object representing the users database. It must have the same interface as the class SQLiteUsersDb in module <font color#"#336633" size"2">Karrigell.admin_db

You can use a SQLite users database by specifying its path in the file system :

users_db = Karrigell.admin_db.SQLiteUsersDb(db_path)

Defaults to None, meaning that there is no users management for this application

###login_url If a users database has been defined, indicates the url where a script must be redirected if it requires user login

Defaults to root_url/admin/login.py/login (the script <font color#"#106010" face"courier">login.py is provided in the directory <font color#"#106010" face"courier">www/admin of the distribution)

###filters A list of methods of the application class. Each method takes a single argument, an instance of the class RequestHandler in module <font color#"#336633" size"2">Karrigell

The filters are applied one after the other when the request has been received from the client. The function can either return a value or raise an exception :

  • if it returns None, the next filter in the list is applied
  • if it returns another value, this value is the absolute path of a script in the file system
  • if it raises HTTP_REDIRECTION(url), the request is redirected to the specified url
  • if it raises HTTP_ERROR(code,message), the request handling stops and the specified HTTP error is sent to the client

Filters offer a flexible way of managing directives like access control to files according to their extension, or to folders according to the user's role, etc

Serving applications

Edit the script server.py :

import Karrigell
import Karrigell.admin_db

class Default(Karrigell.App):
    pass

class Pictures(Karrigell.App):
    root_url = "/pics"
    root_dir = "/path/to/pictures"
    users_db = Karrigell.admin_db.SQLiteUsersDb('/path/to/users_db')

Karrigell.run(apps=[Default,Pictures])

The function run() takes a keyword argument apps, set to a list of application classes

In this example, the server runs 2 application : the default application, and an application serving requests for urls starting with /pics/, taking the files and scripts from the specified root_dir. This application manages users login in an SQLite database at the specified location in the file system

For obvious security reasons, the users database should be located outside of the root directory

GettingStarted

Clone this wiki locally