from flask import Flask, jsonify, request import grpc import hr_pb2 import hr_pb2_grpc app = Flask(__name__) # gRPC channel setup channel = grpc.insecure_channel('localhost:50051') iam_stub = hr_pb2_grpc.IAMServiceStub(channel) test_stub = hr_pb2_grpc.PersonalityTestServiceStub(channel) interview_stub = hr_pb2_grpc.InterviewServiceStub(channel) @app.route('/') def home(): return '''

HR gRPC Gateway

Available Endpoints:

''' @app.route('/user/') def get_user(user_id): try: response = iam_stub.GetUser(hr_pb2.UserRequest(id=user_id)) return jsonify({ 'id': response.id, 'name': response.name, 'role': response.role }) except grpc.RpcError as e: return jsonify({'error': str(e)}), 500 @app.route('/login', methods=['POST']) def login(): try: data = request.json response = iam_stub.Login(hr_pb2.LoginRequest( username=data['username'], password=data['password'] )) return jsonify({ 'success': response.success, 'message': response.message }) except grpc.RpcError as e: return jsonify({'error': str(e)}), 500 @app.route('/test/') def get_test(user_id): try: response = test_stub.GetTestResult(hr_pb2.TestRequest(user_id=user_id)) return jsonify({ 'user_id': response.user_id, 'result': response.result }) except grpc.RpcError as e: return jsonify({'error': str(e)}), 500 @app.route('/interview', methods=['POST']) def schedule_interview(): try: data = request.json response = interview_stub.ScheduleInterview(hr_pb2.InterviewRequest( user_id=data['user_id'], date=data['date'] )) return jsonify({ 'success': response.success, 'message': response.message }) except grpc.RpcError as e: return jsonify({'error': str(e)}), 500 @app.route('/interviews/') def get_interviews(user_id): try: response = interview_stub.GetInterviews(hr_pb2.InterviewListRequest(user_id=user_id)) return jsonify({ 'user_id': user_id, 'interviews': list(response.interviews) }) except grpc.RpcError as e: return jsonify({'error': str(e)}), 500 if __name__ == '__main__': print("🌐 HTTP Gateway running on http://localhost:5000") app.run(debug=True, port=5000)