forked from Pylons/pyramid_openapi3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of an application with an .ini file and paste_prefix.
Issue: Pylons#146
- Loading branch information
1 parent
2904c34
commit 84aa4d1
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |