My company has started with a relatively small project using microservices. As one can imagine the hot topic of code sharing came up, and what to and not to share. One point of contention was sharing the actual express server listen() code in a lib and use in multiple services. I feel this is wrong as I believe this tightly couples the two services by sharing the init code. My colleague days that we are already coupled with express. Is this shared approach the right thing to do? In addition he has not segregated routes and controllers to their structure and yes the code is small but not sure how well one could find routes fast.
Below is what is being shared. I am interested in what people with more experience think about this approach.
let app = express();
let app_ready = false;
function check_alive(request, response, callback?:(req:any, resp:any) => void) {
console.log("alive state requested");
if (callback === undefined) {
// no alive_check callback provided
// use default
if (app_ready) {
response.status(200);
} else {
response.status(503);
}
response.json({ready: app_ready});
} else {
callback(request, response);
}
}
function check_ready(request, response, callback:(req:any, resp:any) => void) {
console.log("ready state requested");
callback(request, response);
}
exports.instance = app;
exports.init = (ready_check: (req:any,resp:any) => void, alive_check?:(req:any,resp:any) => void) => {
// basic routing
app.get("/liveness", (req, res) => {check_alive(req, res, alive_check);});
app.post("/liveness", (req, res) => {check_alive(req, res, alive_check);});
if (ready_check == undefined || ready_check == null) {
app.get("/readiness", (req, res) => {check_alive(req, res);});
app.post("/readiness", (req, res) => {check_alive(req, res);});
} else {
app.get("/readiness", (req, res) => {check_ready(req, res, ready_check);});
app.post("/readiness", (req, res) => {check_ready(req, res, ready_check);});
}
};
exports.listen = (port, callback) => {
return app.listen(port, () => {
app_ready = true;
callback();
});
}
exports.get = (url:string, callback:any) => {
app.get(url, callback);
}
exports.post = (url:string, callback:any) => {
app.post(url, callback);
}
EDIT: So while this code is technically correct, should the app initialization function be wrapped up in a shared lib? If no, why not and what should and shouldn't be shared?
I have read lots of article in medium, devops and this site saying as long as you can deploy, restart, etc independently then it is a microservice.
Thank you in advance for any insight.