Dynamic service discovery can be a feature in Microservice, I have my own private server setup, but this public IP changes every time the router is reallocated per restart. I have services that I want to run on this powerful private server because a cloud server is expensive but with low setup, so I wrote a Python file to help me keep the newest my home IP, so frontend pages etc. can always redirect or get forwarded to my server which has more powerful services and apps running,
#app.py from flask import Flask,request,json,render_template,jsonify,logging from wsgiref.simple_server import make_server app = Flask(__name__) import os @app.route('/') def sayHi(): yourIp=request.remote_addr os.environ["homeIp"]=yourIp return'Hi!'+yourIp @app.route('/test',methods=['get']) def hello_world(): return'Hello, World!'+os.environ["homeIp"] if __name__ == '__main__': # app.run(debug=True) # app.run() server=make_server('',9876,app) server.serve_forever() |
So, I set up APIs to get latest homeIP, also, I will write another client app to keep requesting the "/" route every 5 seconds or so, of course, there is an easier way:
open a web browser and a blank page, then in the console just execute
setInterval(function(){fetch(remoteAPIsetHomeIp),2000}, let it be there and run