Skip to content

Commit 62b4f61

Browse files
committed
updates, added swagger to all
1 parent ebe9a39 commit 62b4f61

File tree

14 files changed

+1022
-83
lines changed

14 files changed

+1022
-83
lines changed

app_flask.py

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,84 @@
22
import os
33
from flask import Flask, jsonify, request
44
from flask_cors import CORS
5+
from flask_restx import Api, Resource, fields
56

67
app = Flask(__name__)
7-
CORS(app) # Enable CORS for all routes
8+
# Enable CORS for all routes with explicit origins
9+
CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)
10+
11+
# Setup Flask-RESTx
12+
api = Api(
13+
app,
14+
version='1.0.0',
15+
title='Flask API',
16+
description='Flask API Documentation',
17+
doc='/docs', # Set documentation endpoint to /docs as requested
18+
)
819

920
# Get environment or use default
1021
env = os.environ.get("FLASK_ENV", "development")
1122

23+
# Define namespaces
24+
ns = api.namespace('', description='Core operations')
25+
26+
# Define models
27+
service_info_model = api.model('ServiceInfo', {
28+
'message': fields.String(example='Hello from Flask!'),
29+
'service': fields.String(example='Flask API'),
30+
'environment': fields.String(example='development'),
31+
'version': fields.String(example='1.0.0')
32+
})
33+
34+
health_model = api.model('Health', {
35+
'status': fields.String(example='healthy'),
36+
'service': fields.String(example='Flask API')
37+
})
38+
39+
echo_model = api.model('Echo', {
40+
'echo': fields.Raw(example={'message': 'Hello world', 'data': {'key': 'value'}}),
41+
'service': fields.String(example='Flask API')
42+
})
43+
44+
input_model = api.model('EchoInput', {
45+
'message': fields.String(example='Hello world'),
46+
'data': fields.Raw(example={'key': 'value'})
47+
})
48+
1249

13-
@app.route("/")
14-
def hello_flask():
15-
return jsonify(
16-
{
50+
@ns.route('/')
51+
class Root(Resource):
52+
@ns.response(200, 'Success', service_info_model)
53+
def get(self):
54+
"""Root endpoint - returns basic service information"""
55+
return {
1756
"message": "Hello from Flask!",
1857
"service": "Flask API",
1958
"environment": env,
2059
"version": "1.0.0",
2160
}
22-
)
2361

2462

25-
@app.route("/health")
26-
def health_check():
27-
return jsonify({"status": "healthy", "service": "Flask API"})
63+
@ns.route('/health')
64+
class Health(Resource):
65+
@ns.response(200, 'Success', health_model)
66+
def get(self):
67+
"""Health check endpoint - returns service health status"""
68+
return {"status": "healthy", "service": "Flask API"}
2869

2970

30-
@app.route("/echo", methods=["POST"])
31-
def echo():
32-
data = request.get_json()
33-
return jsonify({"echo": data, "service": "Flask API"})
71+
@ns.route('/echo')
72+
class Echo(Resource):
73+
@ns.expect(input_model, validate=True)
74+
@ns.response(200, 'Success', echo_model)
75+
def post(self):
76+
"""Echo endpoint - returns the JSON sent in the request"""
77+
data = request.get_json()
78+
return {"echo": data, "service": "Flask API"}
3479

80+
# Add a plain route outside of RESTx for simple health checks
81+
@app.route('/ping')
82+
def ping():
83+
return jsonify({"status": "ok"})
3584

3685
# Gunicorn will run this, so no app.run() here.

app_node.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// app_node.js
22
const express = require('express');
33
const cors = require('cors');
4+
const swaggerJsDoc = require('swagger-jsdoc');
5+
const swaggerUi = require('swagger-ui-express');
46
const app = express();
57

68
// Enable JSON body parsing
@@ -13,6 +15,58 @@ app.use(cors());
1315
const port = process.env.PORT || 8080;
1416
const env = process.env.NODE_ENV || 'development';
1517

18+
// Swagger configuration
19+
const swaggerOptions = {
20+
definition: {
21+
openapi: '3.0.0',
22+
info: {
23+
title: 'Node.js Express API',
24+
version: '1.0.0',
25+
description: 'Node.js Express API Documentation',
26+
contact: {
27+
name: 'API Support'
28+
}
29+
},
30+
servers: [
31+
{
32+
url: `http://localhost:${port}`,
33+
description: 'Development server'
34+
}
35+
]
36+
},
37+
apis: ['./app_node.js'] // Path to the API docs
38+
};
39+
40+
const swaggerDocs = swaggerJsDoc(swaggerOptions);
41+
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
42+
43+
/**
44+
* @swagger
45+
* /:
46+
* get:
47+
* summary: Root endpoint
48+
* description: Returns basic information about the service
49+
* responses:
50+
* 200:
51+
* description: Basic service information
52+
* content:
53+
* application/json:
54+
* schema:
55+
* type: object
56+
* properties:
57+
* message:
58+
* type: string
59+
* example: Hello from Node.js Express!
60+
* service:
61+
* type: string
62+
* example: Node.js Express API
63+
* environment:
64+
* type: string
65+
* example: development
66+
* version:
67+
* type: string
68+
* example: 1.0.0
69+
*/
1670
app.get('/', (req, res) => {
1771
res.json({
1872
message: 'Hello from Node.js Express!',
@@ -22,13 +76,68 @@ app.get('/', (req, res) => {
2276
});
2377
});
2478

79+
/**
80+
* @swagger
81+
* /health:
82+
* get:
83+
* summary: Health check endpoint
84+
* description: Returns the health status of the service
85+
* responses:
86+
* 200:
87+
* description: Service health status
88+
* content:
89+
* application/json:
90+
* schema:
91+
* type: object
92+
* properties:
93+
* status:
94+
* type: string
95+
* example: healthy
96+
* service:
97+
* type: string
98+
* example: Node.js Express API
99+
*/
25100
app.get('/health', (req, res) => {
26101
res.json({
27102
status: 'healthy',
28103
service: 'Node.js Express API'
29104
});
30105
});
31106

107+
/**
108+
* @swagger
109+
* /echo:
110+
* post:
111+
* summary: Echo endpoint
112+
* description: Returns the JSON sent in the request
113+
* requestBody:
114+
* required: true
115+
* content:
116+
* application/json:
117+
* schema:
118+
* type: object
119+
* example:
120+
* message: Hello world
121+
* data:
122+
* key: value
123+
* responses:
124+
* 200:
125+
* description: Echoed response
126+
* content:
127+
* application/json:
128+
* schema:
129+
* type: object
130+
* properties:
131+
* echo:
132+
* type: object
133+
* example:
134+
* message: Hello world
135+
* data:
136+
* key: value
137+
* service:
138+
* type: string
139+
* example: Node.js Express API
140+
*/
32141
app.post('/echo', (req, res) => {
33142
res.json({
34143
echo: req.body,

compose.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@ services:
3232
build:
3333
context: .
3434
container_name: web_flask_dev
35-
stdin_open: true # Keep stdin open
36-
tty: true # Allocate a pseudo-TTY
35+
stdin_open: true
36+
tty: true
3737
env_file:
38-
- ./.env # Load variables from a local .env file
38+
- ./.env
3939
environment:
4040
- FLASK_ENV=development
4141
- PORT=8080
4242
ports:
43-
- "8082:8080" # Map host 8082 to container 8080
43+
- "8082:8080" # Make sure this binding is correct
4444
volumes:
4545
- .:/app:rw
46-
command: ["gunicorn", "-w", "1", "--bind", "0.0.0.0:8080", "--reload", "app_flask:app"]
46+
command: ["gunicorn", "--bind", "0.0.0.0:8080", "--reload", "app_flask:app"]
4747
healthcheck:
48-
test: ["CMD", "/usr/local/bin/healthcheck.sh"]
48+
test: ["CMD", "curl", "-f", "http://localhost:8080/ping"]
4949
interval: 30s
5050
timeout: 10s
5151
retries: 3
5252
start_period: 10s
53-
init: true # Use an init system like tini
5453
restart: unless-stopped
5554
networks:
5655
- dev-network

0 commit comments

Comments
 (0)