Build-in Modules and Configuration¶
Table of Contents
Server Configuration¶
- name server name
- port port number
- debug enable(True)/disable(False) debug mode
- ssl: enable ssl module if specified and start project with subcommand
-s
which means hosting on tornado.httpserver.HTTPServer - component block of component configurations
- controller block of component configurations
config:
# in server_config.yaml
name: hostray server # server name
port: 8888 # port number
debug: True # enable debug mode
ssl:
crt: xxx.crt # absolute path of ssl certificate
key: xxx.key # absolute path of private key file
ca: xxx.ca # optional: absolute path of ca file
controller:
/api_route: # api routing path
enum: controller_key # key of ControllerType
params: # input arguments of initialize()
...
...
component:
component_key: # key of ComponentType
... # configuration vary
...
Controllers¶
The controllers are the implementation of RESTful APIs to handle the incomming requests.
config format:
controller:
/api_route: # api routing path
enum: key # key of ControllerType
params: # input arguments of initialize()
...
Build-in Controllers¶
enum hostray.web.controller.DefaultControllerType.Frontend: | |||||
---|---|---|---|---|---|
Enable a general web http server, this controller directly import tornado.web.StaticFileHandler
rest: get config: controller:
/(.*): # handle all routes
enum: frontend
params:
path: frontend
default_filename: index.html
|
|||||
enum hostray.web.controller.DefaultControllerType.SystemAlive: | |||||
Response 1 to check server is alive
rest: get config: controller:
/alive:
enum: server_alive
|
|||||
enum hostray.web.controller.DefaultControllerType.ComponentsInfo: | |||||
Response with the information of server loaded components by calling info()
rest: get config: controller:
/components_info:
enum: components_info
|
Components¶
The components of hostray is the functional utilities. hostray implements a simple composite pattern to extend the functionalities of project. Configuration format vary.
Build-in Default Components¶
Attention
default components are always loaded when server start.
enum hostray.web.component.DefaultComponentTypes.Localization: | |||||||||
---|---|---|---|---|---|---|---|---|---|
Provides language localization, parameter
config: component:
localization:
dir: 'local'
lang: 'en'
code,en,tw
10000,"this is code 10000",這是 code 10000
|
|||||||||
enum hostray.web.component.DefaultComponentTypes.Logger: | |||||||||
Provides hostray customized logger, parameter
config: component:
logger:
dir: 'logs'
|
|||||||||
enum hostray.web.component.DefaultComponentTypes.Callback: | |||||||||
Callback management with customized
|
|||||||||
enum hostray.web.component.DefaultComponentTypes.WorkerPool: | |||||||||
Provides blocking access thread pools to execute functions
config: component:
worker_pool:
default: 2 # pool_id default with the worker maximum is 2
|
|||||||||
enum hostray.web.component.DefaultComponentTypes.TaskQueue: | |||||||||
Provides non-blocking access thread pool to execute functions
component:
task_queue:
worker_count: 2 # 2 task queue workers
|
Build-in Optional Components¶
enum hostray.web.component.OptionalComponentTypes.Service: | |||||
---|---|---|---|---|---|
Invokes web api, specified method name to enable rest mehtods
config: component:
services:
https://www.google.com: # url
/: # api_route
name: google # name of this invoker
get: # enable method get
|
|||||
enum hostray.web.component.OptionalComponentTypes.MemoryCache: | |||||
Simple backend Session(cache) system
config: component:
memory_cache:
sess_lifetime: 600
save_file: file_name
renew_lifetime: False
renew_id: False
|
|||||
enum hostray.web.component.OptionalComponentTypes.OrmDB: | |||||
Orm component for accessing databases based on sqlalchemy which support many backend databses.
config: component:
orm_db:
db_0: # id of db module
module: sqlite_memory # switch: use sqlite_memory
worker: 1 # number of db access worker (connection)
connection_refresh: 60 # no effect
db_1:
module: sqlite # switch: use sqlite
worker: 1
connection_refresh: 60 # minimum interval in seconds to refresh connection
file_name: data.db # sqlite file path under project directory
db_2:
module: mysql # switch: use mysql
worker: 1
connection_refresh: 60 # minimum interval in seconds to refresh connection
host: xxx.xxx.xxx.xxx # mysql host ip
port: 3306 # mysql host port
db_name: xxxxxxx # mysql database_name
user: xxxxxxxx # mysql login user
password: xxxxxxxx # mysql login password
|
Note
The worker instances hold the sessions and database connections and refresh them until next db accession considers the parameter ‘connection_refresh’ as the minimum interval.
Note
Module ‘sqlite_memory’ does not refresh connections since it is a memory database and will be released if the connection closed.
Unittest Cases¶
hostray reserves module unit_test base on unittest to test the server project or hostray library. Define enum inherits hostray.unit_test.UnitTestTypes to allow hostray tests projects
Run test in command prompt:
- Test hostray library:
python3 -m hostray test
- Test hostray project:
python3 -m hostray test <project directory path>
- Test hostray library:
Packing Project¶
Packing project by typing python3 -m hostray pack <project directory path>
in command prompt.
The optional flags of command pack
:
- Adding
-w
downloads and pack the wheel.whl
lists inrequirements.txt
.- In default,
.py
files are compiled to.pyc
. Adding-d
to disable the compilation.
In hostray project, pack.yaml
indicated the files should be packed. The block of include
lists the external files or directories,
and the block of exclude
lists the files, directories, or extensions should be ignored.
example:
# inside pack.yaml...
include:
- some_file.txt # pack some_file.txt
- some_dir/ # pack directory 'some_dir' recursively
exclude:
- '.log' # excludes files with extension '.log'
- some_dir2/ # excludes files and sub directories under some_dir2 recursively
- some_file2.txt # excludes some_file2.txt