Last active
July 7, 2022 14:27
-
-
Save shyampurk/45cc6f30fe2fffdadf129a550d715781 to your computer and use it in GitHub Desktop.
Sample API Service for Reminder CRUD with Minikube Deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let express = require('express') | |
let bodyParser = require('body-parser') | |
let redis = require("redis") | |
let mongoose = require('mongoose') | |
const reminder = require('./reminder') | |
const redisUrl = 'redis://' + process.env.REDIS_MASTER_SERVICE_HOST | |
const mongouri = "<MONGODB_URL>" | |
let app = express() | |
let client = redis.createClient({url:redisUrl}) | |
client.on('connect', function () { | |
console.log("Redis server connection established") | |
}) | |
client.on('error', function (err) { | |
console.log("Redis server connection error!!") | |
}) | |
function main(){ | |
client.connect(); | |
app.use(express.urlencoded({ extended: false })) | |
app.use(bodyParser.json()) | |
mongoose.connect(mongouri , { | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}).then(result => { | |
console.log('MongoDB connection established') | |
app.post('/reminder/add',async (req,res) => { | |
await reminder.create(req.body) | |
.then(result=>{ | |
res.json({ | |
status:200, | |
message:"New reminder added", | |
data: result | |
}) | |
}).catch(error => { | |
res.json({ | |
status:500, | |
message:"Reminder creation failed" | |
}) | |
console.log(error) | |
}) | |
}) | |
app.get('/reminder/read/:id',async (req,res) => { | |
let cached = await client.get(`${req.params.id}`) | |
if(cached){ | |
console.log("the data is in cache",cached) | |
res.json({ | |
status:200, | |
message:"Reminder read successful", | |
data: JSON.parse(cached) | |
}) | |
} else{ | |
await reminder.findOne({ id: req.params.id }) | |
.then(result=>{ | |
if(result){ | |
client.set(`${req.params.id}`,JSON.stringify(result)) | |
res.json({ | |
status:200, | |
message:"Reminder read successful ", | |
data: result | |
}) | |
} | |
else { | |
res.json({ | |
status:404, | |
message:"Reminder not found" | |
}) | |
} | |
}).catch(error => { | |
res.json({ | |
status:500, | |
message:"Reminder read failed" | |
}) | |
}) | |
} | |
}) | |
app.delete('/reminder/delete/:id',async(req,res)=>{ | |
await reminder.findOne({ id: req.params.id }) | |
.then(result=>{ | |
if(result){ | |
reminder.deleteOne({id:req.params.id}) | |
.then(result=>{ | |
res.json({ | |
status:200, | |
message:"Reminder deleted", | |
}) | |
}) | |
.catch(error => { | |
res.json({ | |
status:500, | |
message:"Reminder deletion failed" | |
}) | |
console.log(error) | |
}) | |
} | |
else { | |
res.json({ | |
status:404, | |
message:"Reminder not found" | |
}) | |
} | |
}).catch(error => { | |
res.json({ | |
status:500, | |
message:"Reminder deletion failed" | |
}) | |
}) | |
}) | |
}).catch(err=>{ | |
res.json({ | |
status:500, | |
message:"Internal error" | |
}) | |
}) | |
} | |
app.listen(3000, function () { | |
console.log('Connected at port 3000') | |
main() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
creationTimestamp: null | |
labels: | |
app: caching-demo | |
name: caching-demo | |
spec: | |
replicas: 3 | |
selector: | |
matchLabels: | |
app: caching-demo | |
strategy: {} | |
template: | |
metadata: | |
creationTimestamp: null | |
labels: | |
app: caching-demo | |
spec: | |
initContainers: | |
- name: init-wait | |
image: alpine | |
command: ['sleep' , '20'] | |
containers: | |
- image: lightrun-k8-caching-demo | |
imagePullPolicy: Never | |
name: caching-demo | |
ports: | |
- containerPort: 3000 | |
resources: {} | |
status: {} | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
creationTimestamp: null | |
labels: | |
app: caching-demo | |
name: caching-demo | |
spec: | |
ports: | |
- nodePort: 31000 | |
port: 3000 | |
protocol: TCP | |
targetPort: 3000 | |
selector: | |
app: caching-demo | |
type: LoadBalancer | |
status: | |
loadBalancer: {} | |
--- | |
apiVersion: apps/v1 # API version | |
kind: Deployment | |
metadata: | |
name: redis-master # Unique name for the deployment | |
labels: | |
app: redis # Labels to be applied to this deployment | |
spec: | |
selector: | |
matchLabels: # This deployment applies to the Pods matching these labels | |
app: redis | |
role: master | |
tier: backend | |
replicas: 1 # Run a single pod in the deployment | |
template: # Template for the pods that will be created by this deployment | |
metadata: | |
labels: # Labels to be applied to the Pods in this deployment | |
app: redis | |
role: master | |
tier: backend | |
spec: # Spec for the container which will be run inside the Pod. | |
containers: | |
- name: master | |
image: redis | |
resources: | |
requests: | |
cpu: 100m | |
memory: 100Mi | |
ports: | |
- containerPort: 6379 | |
--- | |
apiVersion: v1 | |
kind: Service # Type of Kubernetes resource | |
metadata: | |
name: redis-master # Name of the Kubernetes resource | |
labels: # Labels that will be applied to this resource | |
app: redis | |
role: master | |
tier: backend | |
spec: | |
ports: | |
- port: 6379 # Map incoming connections on port 6379 to the target port 6379 of the Pod | |
targetPort: 6379 | |
selector: # Map any Pod with the specified labels to this service | |
app: redis | |
role: master | |
tier: backend | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:16 | |
#Create app directory | |
WORKDIR /usr/src/app | |
#Install any dependencies | |
COPY . . | |
RUN npm install | |
#Networking | |
EXPOSE 3000 | |
#Run the app | |
ENTRYPOINT ["node", "app.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"express": "^4.17.3", | |
"mongoose": "^6.3.1", | |
"redis": "^4.0.6" | |
}, | |
"name": "redisremainder", | |
"version": "1.0.0", | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"description": "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let db = require('mongoose') | |
const Schema = db.Schema | |
const reminder = new Schema({ | |
id: { | |
type: String, | |
required: true | |
}, | |
description : { | |
type: String, | |
required: true | |
}, | |
creatorName : { | |
type: String, | |
required: true | |
}, | |
remindDate : { | |
type: String, | |
required: true | |
}, | |
activeFlag : { | |
type: Boolean, | |
required: true | |
} | |
}, | |
{ | |
timestamps: true | |
}, | |
{ | |
"strict": "throw" | |
} | |
) | |
reminder.set('collection', 'reminder') | |
const reminderCollection = db.model('reminder', reminder) | |
module.exports = reminderCollection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment