jairsjunior / onboarding-demo

0 stars 0 forks source link

moisesjsalmeida - Onboarding Phase 2 #144

Open jairsjunior-onboarding[bot] opened 3 years ago

jairsjunior-onboarding[bot] commented 3 years ago

Welcome to the Onboarding Phase 2 - Campus Web Development - Step 1

jairsjunior-onboarding[bot] commented 3 years ago

First activity

Create a web service in Golang with a /hello API exposed to HTTP GET requests that returns the JSON: {"message": "word"}.

The service must be run in a Docker container.

Send me a screenshot of you source code, of you Dockerfile, and of a CURL GET command calling the /hello endpoint.

Hint: https://tour.golang.org/welcome/1

Challenge: Can you reduce your Golang service Docker image size to bellow 10MB?

moisesjsalmeida commented 3 years ago
// index.js

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ message: 'world' }));
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/hello`);
});

# Dockerfile

FROM node:14.16-alpine3.12

WORKDIR /usr/src/app
COPY . .
EXPOSE 3000
CMD npm start
moises in ~/projects/onboarding_node via ⬢ v14.16.1 took 3s
❯ curl -i localhost:3000/hello
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 07 Apr 2021 15:21:32 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 19

{"message":"world"}%
jairsjunior-onboarding[bot] commented 3 years ago

Done

moisesjsalmeida commented 3 years ago

next

relipe commented 3 years ago

Done

jairsjunior-onboarding[bot] commented 3 years ago

Done

jairsjunior-onboarding[bot] commented 3 years ago

Second activity

Evolve your Golang service to expose a /info API to HTTP GET requests, where the JSON content that gets returned is obtained from a MongoDB entry.

Send me a screenshot of your docker-compose.yml, of your source code and of your Browser network tab showing the request successfully being made.

moisesjsalmeida commented 3 years ago
services:
  node:
    build:
      context: .
      dockerfile: node.Dockerfile
    ports:
      - 3000:3000
    volumes:
      - ./app:/usr/src/app

  mongodb:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ./db_data:/data/db

info.js

const express = require('express');
const router = express.Router();

const Post = require('../models/Post');

router.get('/', async (req, res) => {
  try {
    const posts = await Post.find();
    res.json(posts);
  } catch (err) {
    res.json({ message: err });
  }
});

router.post('/', async (req, res) => {
  const post = new Post({
    title: req.body.title,
    description: req.body.description,
  });

  post
    .save()
    .then((data) => {
      res.status(200).json(data);
    })
    .catch((err) => {
      res.status(404).json({ message: err });
    });
});

module.exports = router;

index.js

require('dotenv/config');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || '0.0.0.0';

app.use(express.json());

//DB
const mongoose = require('mongoose');
mongoose.connect(process.env.DATABASE_CONNECTION, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

// ROUTES
const infoRoute = require('./routes/info');
app.use('/info', infoRoute);

app.get('/hello', (req, res) => res.json({ message: 'world' }));

app.listen(PORT, HOST);

image

jairsjunior-onboarding[bot] commented 3 years ago

Done

jairsjunior-onboarding[bot] commented 3 years ago

Third activity

Create a /metrics API to HTTP GET requests in the same service where the prometheus metrics uptime and invocation_count get exposed.

Add your service's /metrics endpoint to a Prometheus target list and show the data in a custom Grafana panel.

Send me a screenshot of your Grafana panel and of your metrics source code.

Hint: middleware