Yesterday I released my first Python framework. No, not another one. This is in the world - another one. And for me so far the first. And I admit that he is the first of its kind. It is a framework for building custom servers. And they will be created through the config. Wow, let's create now ...
In the beginning there was a config
So, config. Since we have already installed the framework by this point. And if not, then it's easy and simple to do with the command:
pip3 install idewavecore==0.0.1
This is assuming you have Python 3.6+, internet, and computer .
The config itself looks something like this:
# settings.yml
settings:
servers: !inject config_dir/servers.yml
db_connections:
sqlite:
host: ~
username: ~
password: ~
# default mysql 3306, postgresql 5432, sqlite don't need port
port: ~
# currently allowed: mysql, postgresql, sqlite
dialect: sqlite
# supported drivers:
# mysql: mysqlconnector, pymysql, pyodbc
# postgresql: psycopg2, pg8000, pygresql
driver: ~
# to use with sqlite this should be absolute db path
# can be empty to keep db in memory (sqlite only)
db_name: ~
charset: ~
!inject
- yaml . , yaml- -.
# servers.yml
sample_server:
connection:
host: 1.2.3.4
port: 1234
# possible values: tcp, websocket
connection_type: tcp
# optional
proxy:
host: ~
port: ~
# possible values: tcp, websocket
connection_type: tcp
options:
server_name: Sample Server
is_debug: false
middlewares: !pipe
- !fn native.test.mock_middleware
- !fn native.test.mock_middleware
- !infinite_loop
- !fn native.test.mock_middleware
- !fn native.test.mock_middleware
- !fn native.test.mock_middleware
- !router
ROUTE_1: !fn native.test.mock_middleware
ROUTE_2: !fn native.test.mock_middleware
ROUTE_3:
- !fn native.test.mock_middleware
- !fn native.test.mock_middleware
- !fn native.test.mock_middleware
# optional
db_connection: sqlite
.
!pipe
- , . , middlewares ( - ), . - .
!infinite_loop
- , . (, websocket).
!router
- , . , , (route).
, , !fn
- , ( - middleware) . , - - middlewares - . , , :
!fn <__>.<_>
. (middlewares) - . (! !), native
, :
!fn native.test.mock_middleware
, .
Middle where
- , . , - . .
, - , ( - ). :
from idewavecore.session import Storage, ItemFlag
async def sample_middleware(**kwargs):
global_storage: Storage = kwargs.pop('global_storage')
server_storage: Storage = kwargs.pop('server_storage')
session_storage: Storage = kwargs.pop('session_storage')
session_storage.set_items([
{
'key1': {
'value': 'some_tmp_value'
}
},
{
'key2': {
'value': 'some_persistent_value',
'flags': ItemFlag.PERSISTENT
}
},
{
'key3': {
'value': 'some_persistent_constant_value',
'flags': ItemFlag.PERSISTENT | ItemFlag.FROZEN
}
},
{
'key4': {
'value': 'some_constant_value',
'flags': ItemFlag.FROZEN
}
}
])
value_of_key3 = session_storage.get_value('key3')
(storage). : (global storage), (server storage) (session storage).
, .
.
.
.
( , . . , -. - , .)
...
# run.py
import asyncio
from idewavecore import Assembler
from idewavecore.session import Storage
if __name__ == '__main__':
loop = asyncio.get_event_loop()
global_storage = Storage()
assembler = Assembler(
global_storage=global_storage,
#
config_path='settings.yml'
)
servers = assembler.assemble()
for server in servers:
server.start()
loop.run_until_complete(
asyncio.gather(*[server.get() for server in servers])
)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.close()
- ( , ) , . - , curl, mmo rpg ...
And now I want to say thank you to everyone who read my post to the end. I would be grateful for any constructive comments. Even if my framework is not the first of its kind, I want to bring it as close as possible to this bar.
Join us .