Getting started
This page will help you get started with glim. You'll be up and running in a jiffy!
After we have a working glim app, now it can be further learned what's inside a typical glim app and some configuration tips.
App Structure
$ glim new
command creates the following directory structure;
app
├── commands.py => custom command line utilities
├── config => configuration sources
│ └── default.py => a sample default config
| └── development.py => the development environment config
├── controllers.py => controller sources
├── models.py => model layer sources
├── routes.py => application routes source
├── services.py => application services source
├── static => static folder to hold css,img,js,etc.
├── start.py => functions run before the web app starts
└── storage => storage folder for logs, sessions, etc.
ext => ext folder to keep extensions
Configuration
Glim provides a structure to keep application level constants. app/config
folder has application level configuration. After creating a sample app, glim automatically creates this folder.
By default, app/config
has two files namely default.py
& development.py
. The name of these files are mapped to the possible environments to run the app. The default environment is development
, which is mapped into development.py
. In this folder, it's highly suggested not to use default.py
. This file is there because of backup purposes.
The simplest configuration file would be the following;
# config.py
import os
import glim.paths
import os
import glim.paths
config = {
# the configurations of extensions
'extensions': {
# 'gredis' : {
# 'default' : {
# 'host' : 'localhost',
# 'port' : '6379',
# 'db' : 0
# }
# }
},
# logging configuration, it has a default configuration
# if you don't provide one.
'log' : {
'app' : {
'level': 'info',
'format': '[%(levelname)s] - application : %(message)s',
'colored': True
# 'file' : 'app/storage/logs/app.log'
},
'glim' : {
'level' : 'info',
'format' : '[%(levelname)s] : %(message)s',
'colored': True
# 'file' : 'app/storage/logs/glim.log'
},
},
# the glim.app.App configuration
'app': {
'server': {
'host': '127.0.0.1',
'port': 8080,
'wsgi': 'wsgiref',
'reloader': True,
'debugger': True,
'options': {}
},
'assets': {
'path': glim.paths.ASSET_PATH,
'url': '/assets'
}
}
}
Most of these configuration variables have internal default values. So, even if they don't exist, the default variables will be assigned automatically by glim. However, currently, it is required to have at least the keys in the dictionary. Some of the keys and their usages are the following;
Don't remove the keys, instead, remove the contents of the keys
For not re remembering all those keys, it is highly suggested to change & remove the values of the keys but not removing the keys itself. Moreover, glim may require the existence of these keys even it doesn't require the values.
Key | Usage |
---|---|
extensions | Dynamic extension loading and keeping the extension configurations with respect to different environments |
log | Holds configuration for logging level of the environment, the logging format and the file name if provided. There exist a default logging setting if you don't provide one. |
app | This key holds configuration mosty about the web server. If you are familiar with bottle , you may notice these keys are used by the wsgi of glim. Note that glim serves the static files using /assets url with assets folder. |
The keys can be accessed from the application using the Config
module. There exists a Config
instance to access, modify, flush these keys;
from glim import Config
# deeply get using dot "." notation
Config.get('app.server.host')
# returns 127.0.0.1
Config.set('app.server.host', 'localhost')
# sets app.server.host as localhost
Use Config.set() function smartly!
Although configuration should be readonly, there exists the set() function for setting configuration in runtime. Don't use set() function unless there is an absolute need.
Adding key/values to config
You can add other constants simply by adding a new key to the config dict
. For instance, if you have a facebook connect in your glim app, you can append the following and have access by the following Config
function call;
# app/config/<env>.py
# append this key inside config
{
'facebook': {
'key': 'your-application-key',
'secret': 'your-application-secret'
}
}
# access these keys by the following;
from glim import Config
Config.get('facebook.key') # returns the key
Config.get('facebook.secret') # returns the secret
Environments
As it is mentioned above, glim has support for multi environment configurations. In glim, each configuration file in app/config
maps to an environment. By default, the development
environment is enabled for the app. You can start the app with different environments using the following command;
glim start --env production
In the example above, glim searches for app/config/production.py
to load to the Config
class.
Adding new environments
Suppose that you need a testing environment. This environment can easily be generated and started by the following;
cp app/config/default.py app/config/testing.py
glim start --env testing
Updated less than a minute ago