Skip to content

Commit

Permalink
Convert the helloworld to fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinna Kiisuo committed Aug 21, 2023
1 parent bdf732b commit 6d58ac2
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 23 deletions.
36 changes: 15 additions & 21 deletions borg_lockservice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from absl import flags, logging, app
from absl import flags
import os, sys
import uvicorn

from fastapi import FastAPI

FLAGS = flags.FLAGS
app = FastAPI()

PREFIX = "BORG_LOCKSERVICE"

Expand All @@ -19,31 +21,23 @@
"Listen port for the service. Defaults to 8000",
)

flags.DEFINE_boolean(
"dev",
os.getenv(f"{PREFIX}_DEV", False),
"Enable development mode. Defaults to False, should not be enabled in production.",
)

async def main(scope, receive, send):
assert scope['type'] == 'http'

await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})
await send({
'type': 'http.response.body',
'body': bytes(f"Hewwo World :3 .. I'm listening on {FLAGS.port}", 'utf-8'),
})

@app.get("/")
async def root():
return {
'message': f"uwu :3",
}

def run_uvicorn(argv):
del argv
logging.info(f"Will listen on {FLAGS.host}:{FLAGS.port}")
uvicorn.run("borg_lockservice:main", host=FLAGS.host, port=FLAGS.port)

# script endpoint installed by package
def run():
app.run(run_uvicorn)
FLAGS(sys.argv)
uvicorn.run("borg_lockservice:app", host=FLAGS.host, port=FLAGS.port, reload=FLAGS.dev)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 6d58ac2

Please sign in to comment.