Skip to content

Instantly share code, notes, and snippets.

@yrashk
Last active October 28, 2023 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yrashk/0c085b5541189b685d702dcc25fa7b32 to your computer and use it in GitHub Desktop.
Save yrashk/0c085b5541189b685d702dcc25fa7b32 to your computer and use it in GitHub Desktop.
Omnigres Python and Flask experience: Early Preview (Oct 23, 2023) https://www.loom.com/share/b4701cbc77e941919cd5dbff7b15465e
--- omni_schema[[ignore]]
---
--- NB: most, if not all of this will be handled by the upcoming [CLI] tooling
---
-- Extensions
create extension if not exists omni_schema cascade;
create extension if not exists omni_python cascade;
create extension if not exists omni_httpd cascade;
create extension if not exists omni_vfs cascade;
create extension if not exists omni_mimetypes cascade;
--- Local filesystem
create or replace function demo() returns omni_vfs.local_fs language sql
as $$
select omni_vfs.local_fs('/demo')
$$;
insert into omni_python.config (name, value) values ('pip_find_links', '/python-wheels');
-- Can use as a :reload command
\set reload 'select omni_schema.load_from_fs(demo());'
select omni_schema.load_from_fs(demo());
-- Application listener
with
listener as (insert into omni_httpd.listeners (address, port) values ('0.0.0.0', 5000) returning id),
handler as (insert into omni_httpd.handlers (query) values
(
$$ select handle(request.*) from request$$
) returning id)
insert
into
omni_httpd.listeners_handlers (listener_id, handler_id)
select
listener.id,
handler.id
from
listener,
handler;
call omni_httpd.wait_for_configuration_reloads(1);
omni_python
omni_http[Flask]
docker run -p 127.0.0.1:5000:5000 -p 127.0.0.1:5432:5432 -v $(pwd)/demo:/demo --rm -ti ghcr.io/omnigres/omnigres-slim
### Home page
GET http://localhost:5000/
### Post
GET http://localhost:5000/post/4
### JSON
GET http://localhost:5000/test.json
from omni_python import pg
from omni_http import omni_httpd
from omni_http.omni_httpd import flask
@pg
def test() -> int:
return 1+1
@pg
def hello() -> str:
return "World"
from flask import Flask, jsonify, make_response
app = Flask('myapp')
@app.route("/")
def hello_world():
return "<h1>Hello, World!</h2>"
@app.route('/post/<int:post_id>')
def show_post(post_id):
resp = make_response(f'<h1>Post #{post_id}</h1>')
resp.set_cookie('cookie', 'yum')
return resp
@app.route("/test.json")
def json():
return jsonify({"test": "passed"})
app_ = flask.Adapter(app)
@pg
def handle(req: omni_httpd.HTTPRequest) -> omni_httpd.HTTPOutcome:
return app_(req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment