demo-app/app.py
ningning 7a27c89067
Some checks failed
Build and deploy demo app / deploy (push) Has been cancelled
feat: add local cicd demo app
2026-06-26 12:02:15 +08:00

52 lines
1.6 KiB
Python

import json
import os
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
APP_NAME = os.environ.get("APP_NAME", "demo-app")
APP_VERSION = os.environ.get("APP_VERSION", "local")
PORT = int(os.environ.get("PORT", "8080"))
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/health":
self._send_json(200, {"status": "ok", "app": APP_NAME, "version": APP_VERSION})
return
if self.path == "/":
body = f"{APP_NAME} is running\nversion={APP_VERSION}\n"
self._send_text(200, body)
return
self._send_json(404, {"error": "not found"})
def log_message(self, fmt, *args):
print("%s - %s" % (self.address_string(), fmt % args))
def _send_json(self, status, payload):
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _send_text(self, status, body_text):
body = body_text.encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def main():
server = ThreadingHTTPServer(("0.0.0.0", PORT), Handler)
print(f"{APP_NAME} listening on 0.0.0.0:{PORT}")
server.serve_forever()
if __name__ == "__main__":
main()