You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
9 years ago
|
#!/usr/bin/env python
|
||
8 years ago
|
|
||
1 year ago
|
# Runs an HTTP server on localhost:8000 which will serve the generated OpenAPI
|
||
|
# JSON so that it can be viewed in an online OpenAPI viewer.
|
||
8 years ago
|
|
||
|
# Copyright 2016 OpenMarket Ltd
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
9 years ago
|
#
|
||
8 years ago
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
9 years ago
|
|
||
|
import argparse
|
||
|
import os
|
||
6 years ago
|
import http.server
|
||
|
import socketserver
|
||
9 years ago
|
|
||
|
# Thanks to http://stackoverflow.com/a/13354482
|
||
6 years ago
|
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
|
||
9 years ago
|
def end_headers(self):
|
||
|
self.send_my_headers()
|
||
6 years ago
|
http.server.SimpleHTTPRequestHandler.end_headers(self)
|
||
9 years ago
|
|
||
|
def send_my_headers(self):
|
||
|
self.send_header("Access-Control-Allow-Origin", "*")
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
scripts_dir = os.path.dirname(os.path.abspath(__file__))
|
||
|
parser = argparse.ArgumentParser()
|
||
8 years ago
|
parser.add_argument(
|
||
8 years ago
|
'--port', '-p',
|
||
8 years ago
|
type=int, default=8000,
|
||
|
help='TCP port to listen on (default: %(default)s)',
|
||
|
)
|
||
|
parser.add_argument(
|
||
1 year ago
|
'openapi_dir', nargs='?',
|
||
|
default=os.path.join(scripts_dir, 'openapi'),
|
||
8 years ago
|
help='directory to serve (default: %(default)s)',
|
||
|
)
|
||
9 years ago
|
args = parser.parse_args()
|
||
|
|
||
1 year ago
|
os.chdir(args.openapi_dir)
|
||
9 years ago
|
|
||
6 years ago
|
httpd = socketserver.TCPServer(("localhost", args.port),
|
||
8 years ago
|
MyHTTPRequestHandler)
|
||
6 years ago
|
print("Serving at http://localhost:%i/api-docs.json" % args.port)
|
||
9 years ago
|
httpd.serve_forever()
|