Skip to content

Commit

Permalink
Add example of an application with an .ini file and paste_prefix.
Browse files Browse the repository at this point in the history
Issue: Pylons#146
  • Loading branch information
Wim-De-Clercq committed Jun 29, 2021
1 parent 2904c34 commit 84aa4d1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/pserve_ini_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import sys

from pyramid.config import Configurator
from pyramid.request import Request
from pyramid.scripts.pserve import main
from pyramid.view import view_config

APP_FOLDER = os.path.dirname(__file__)


@view_config(route_name="version", renderer="json", request_method="GET", openapi=True)
def get(request: Request):
"""Get the app's version."""
return {"Version": "0.0.1"}


def wsgi_app(global_config, **settings):
"""Prepare a Pyramid app."""
with Configurator() as config:
config.include("pyramid_openapi3")
config.pyramid_openapi3_spec(
os.path.join(APP_FOLDER, "openapi.yaml")
)
config.pyramid_openapi3_add_explorer()
config.add_route("version", "/version")
config.scan(".")

return config.make_wsgi_app()


if __name__ == '__main__':
# The normal way of starting an app would be `pserve server.ini`
# we replicate this here by adding a sys.argv

# normally you'd have an egg or a proper package, but this app is simpler.
# This way can only work when the working directory is the same as where app.py is
# because of the hardcoded app:wsgi_app in server.ini
os.chdir(APP_FOLDER)
config_file = os.path.join(APP_FOLDER, 'server.ini')
sys.argv.append(config_file)
sys.exit(main())
37 changes: 37 additions & 0 deletions examples/pserve_ini_app/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: "3.0.0"

servers:
- url: '/v'

info:
version: "1.0.0"
title: A simple Todo app API

paths:
/version:
get:
summary: version
description: version
tags:
- version
responses:
"200":
description: version
content:
application/json:
schema:
$ref: "#/components/schemas/Item"


components:

schemas:

Item:
type: object
required:
- title
properties:
title:
type: string
maxLength: 40
20 changes: 20 additions & 0 deletions examples/pserve_ini_app/server.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
######
# app configuration
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
######
[app:test-app]
paste.app_factory = app:wsgi_app

[filter:paste_prefix]
use = egg:PasteDeploy#prefix
prefix = /v

[pipeline:main]
pipeline =
paste_prefix
test-app

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

0 comments on commit 84aa4d1

Please sign in to comment.