Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of an application with an .ini file and paste_prefix. #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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