From 92e39f451e25378aa5d836f3e1c920915d90dc7b Mon Sep 17 00:00:00 2001 From: Wellington Mendes Date: Sat, 3 Sep 2022 23:14:47 -0300 Subject: [PATCH] [devops-bug-fix] see CHANGELOG for more details --- CHANGELOG.md | 47 ++++++++++ Makefile | 5 ++ docker-compose.yaml | 32 +++++-- {services/frontend => frontend}/Dockerfile | 6 +- .../frontend => frontend}/css/styles.css | 0 {services/frontend => frontend}/index.html | 0 {services/frontend => frontend}/js/app.js | 90 +++++++++---------- {services/frontend => frontend}/package.json | 0 reader/Dockerfile | 5 ++ reader/go.mod | 8 ++ reader/go.sum | 4 + {services/reader => reader}/main.go | 5 +- services/reader/Dockerfile | 6 -- services/writer/Dockerfile | 6 -- writer/Dockerfile | 7 ++ {services/writer => writer}/main.py | 0 writer/requirements.txt | 1 + 17 files changed, 156 insertions(+), 66 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 Makefile rename {services/frontend => frontend}/Dockerfile (52%) rename {services/frontend => frontend}/css/styles.css (100%) rename {services/frontend => frontend}/index.html (100%) rename {services/frontend => frontend}/js/app.js (60%) rename {services/frontend => frontend}/package.json (100%) create mode 100644 reader/Dockerfile create mode 100644 reader/go.mod create mode 100644 reader/go.sum rename {services/reader => reader}/main.go (82%) delete mode 100644 services/reader/Dockerfile delete mode 100644 services/writer/Dockerfile create mode 100644 writer/Dockerfile rename {services/writer => writer}/main.py (100%) create mode 100644 writer/requirements.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7711aee --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,47 @@ +## Changelog + +As alterações realizadas visam corrigir a estrutura do projeto, assim como definir uma arquitetura para o sistema. + +- `web` conecta-se com `reader` e `writer` pela rede `frontend`, não havendo conexão com o armazenamento (`redis`). + +- O back end, composto pelos serviços `reader` e `writer` se comunicam diretamente com o armazenamento (`redis`) para trocar informações. + +### root + +- Adicionado Makefile para subir e descer containers com `docker-compose`. + +- Adicionado CHANGELOG.md para visualizar alterações feitas no código. + +### docker-compose.yaml + +- Adicionada rede `frontend`. + +- Corrigido nome do serviço `redis`. + +- Alterado porta para serviço `web` de 5000 para 5001. + +- Corrigindo as portas dos serviços `reader` e `writer`. + +- Adicionando a porta utilizada no `redis`. + +- Adicionado `volumes` em todos os containers para facilitar desenvolvimento. + +- Adicionando `depends_on` para seguir um fluxo dependência na subida de containers. + +### services/frontend + +- Corrigindo `Dockerfile`. + +### services/reader + +- Corrigindo `Dockerfile`. + +- Adicionando arquivos `go.mod` e `go.sum` com as dependencias do projeto. + +- Corrigindo endpoint `/data` onde o `client.Get` passava parametros excedentes. Era necessário passar somente uma string que seria a key: `key := client.Get("SHAREDKEY")`. + +### services/writer + +- Corrigindo `Dockerfile`. + +- Adicionando `requirements.txt`. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4da6c03 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +up: + docker-compose up -d --build + +down: + docker-compose down \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index e196b36..dc1d83d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,24 +3,46 @@ services: web: build: services/frontend ports: - - "5000:5000" + - "5001:5000" + volumes: + - ./services/frontend/:/app networks: - frontend - - backend + depends_on: + - reader + - writer reader: build: services/reader ports: - - "8081:8081" + - "8080:8080" + volumes: + - ./services/reader/:/app networks: - frontend + - backend + depends_on: + - redis writer: build: services/writer ports: - - "8080:8080" + - "8081:8081" + volumes: + - ./services/writer/:/app networks: + - frontend - backend - reids: + depends_on: + - redis + redis: image: "redis:alpine" + command: redis-server --appendonly yes + ports: + - "6379:6379" + volumes: + - /tmp/redis.tmp:/data + networks: + - backend networks: backend: + frontend: diff --git a/services/frontend/Dockerfile b/frontend/Dockerfile similarity index 52% rename from services/frontend/Dockerfile rename to frontend/Dockerfile index 2e3c45c..68170e2 100644 --- a/services/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,6 +1,6 @@ FROM node:latest WORKDIR /app -ADD . . +ADD . /app RUN npm install -g serve -EXPOSE 5000 -CMD "serve" \ No newline at end of file +EXPOSE 5001 +CMD ["serve", "-l", "5000"] \ No newline at end of file diff --git a/services/frontend/css/styles.css b/frontend/css/styles.css similarity index 100% rename from services/frontend/css/styles.css rename to frontend/css/styles.css diff --git a/services/frontend/index.html b/frontend/index.html similarity index 100% rename from services/frontend/index.html rename to frontend/index.html diff --git a/services/frontend/js/app.js b/frontend/js/app.js similarity index 60% rename from services/frontend/js/app.js rename to frontend/js/app.js index f81dcaa..159b105 100644 --- a/services/frontend/js/app.js +++ b/frontend/js/app.js @@ -10,7 +10,7 @@ const home = () => { const writerp = container.appendChild(document.createElement('p')) readerhealth(readerp) writerhealth(writerp) - container.classList.add("mx-auto","align-middle","container") + container.classList.add("mx-auto", "align-middle", "container") return container; }; @@ -22,7 +22,7 @@ const reader = () => { ` const child = container.appendChild(document.createElement('p')) readData(child) - container.classList.add("mx-auto","align-middle","container") + container.classList.add("mx-auto", "align-middle", "container") return container; }; @@ -39,49 +39,49 @@ const writer = () => { ` - container.classList.add("mx-auto","align-middle","container") + container.classList.add("mx-auto", "align-middle", "container") const formsubmit = container.querySelector("#submitdata") - formsubmit.addEventListener('submit',writeData) + formsubmit.addEventListener('submit', writeData) return container; } const readerhealth = (readerparagraph) => { - const url='http://localhost:8080/health'; + const url = 'http://localhost:8080/health'; fetch(url, { - method: "GET", - mode: 'cors', - headers: { - 'Access-Control-Allow-Origin': '*' - }, - }).then( response => { - response.text().then(body => { - assembleStatus(readerparagraph, "Reader", body) - }).catch(err => { + method: "GET", + mode: 'cors', + headers: { + 'Access-Control-Allow-Origin': '*' + }, + }).then(response => { + response.text().then(body => { + assembleStatus(readerparagraph, "Reader", body) + }).catch(err => { + assembleStatus(readerparagraph, "Reader", "down") + }) + }) + .catch(error => { + console.log(error) assembleStatus(readerparagraph, "Reader", "down") }) - }) - .catch( error => { - console.log(error) - assembleStatus(readerparagraph,"Reader","down") - }) } - const writerhealth = (writerparagraph) => { - const url='http://localhost:8081/health'; +const writerhealth = (writerparagraph) => { + const url = 'http://localhost:8081/health'; fetch(url, { method: "GET", mode: 'cors', headers: { 'Access-Control-Allow-Origin': '*' }, - }).then( response => { + }).then(response => { response.text().then(body => { assembleStatus(writerparagraph, "Writer", body) }).catch(err => { assembleStatus(writerparagraph, "Writer", "down") - }) - }).catch( error => { - assembleStatus(writerparagraph,"Writer","down") + }) + }).catch(error => { + assembleStatus(writerparagraph, "Writer", "down") }) } @@ -92,14 +92,14 @@ const assembleStatus = (paragraph, service, status) => { statusIcon = 'success' statusBadge = 'success' } - paragraph.innerHTML= - service + ' service status '+status+'' + paragraph.innerHTML = + service + ' service status ' + status + '' } async function writeData(e) { e.preventDefault(); - const url='http://localhost:8081/write'; + const url = 'http://localhost:8081/write'; fetch(url, { method: "POST", body: e.target.elements.post.value, @@ -107,33 +107,33 @@ async function writeData(e) { headers: { 'Access-Control-Allow-Origin': '*' }, - }).then( response => { + }).then(response => { - }).catch( error => { + }).catch(error => { console.log(error) }) } const readData = (paragraph) => { - const url='http://localhost:8080/data'; + const url = 'http://localhost:8080/data'; fetch(url, { - method: "GET", - mode: 'cors', - headers: { - 'Access-Control-Allow-Origin': '*' - }, - }).then( response => { - response.text().then(body => { - paragraph.innerHTML = "Valor encontrado = "+body - }).catch(err => { - paragraph.innerHTML = ` + method: "GET", + mode: 'cors', + headers: { + 'Access-Control-Allow-Origin': '*' + }, + }).then(response => { + response.text().then(body => { + paragraph.innerHTML = "Valor encontrado = " + body + }).catch(err => { + paragraph.innerHTML = ` ` + }) }) - }) - .catch( error => { + .catch(error => { console.log(error) }) } @@ -145,7 +145,7 @@ const routes = { writer: writer(), } -const validateHash = (hash) => hash === "" ? 'home' : hash.replace('#', ''); +const validateHash = (hash) => hash === "" ? 'home' : hash.replace('#', ''); const init = () => window.addEventListener('hashchange', renderPage); const renderPage = () => { @@ -154,7 +154,7 @@ const renderPage = () => { root.appendChild(routes[page]) } -window.addEventListener('load', ()=> { +window.addEventListener('load', () => { renderPage(); init(); }); \ No newline at end of file diff --git a/services/frontend/package.json b/frontend/package.json similarity index 100% rename from services/frontend/package.json rename to frontend/package.json diff --git a/reader/Dockerfile b/reader/Dockerfile new file mode 100644 index 0000000..262bb73 --- /dev/null +++ b/reader/Dockerfile @@ -0,0 +1,5 @@ +FROM golang:1.16 +WORKDIR /app +ADD . /app +EXPOSE 8080 +CMD ["go", "run", "main.go"] \ No newline at end of file diff --git a/reader/go.mod b/reader/go.mod new file mode 100644 index 0000000..5176649 --- /dev/null +++ b/reader/go.mod @@ -0,0 +1,8 @@ +module reader + +go 1.16 + +require ( + github.com/go-redis/redis v6.15.9+incompatible // indirect + github.com/rs/cors v1.8.2 // indirect +) diff --git a/reader/go.sum b/reader/go.sum new file mode 100644 index 0000000..b1e3990 --- /dev/null +++ b/reader/go.sum @@ -0,0 +1,4 @@ +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U= +github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= diff --git a/services/reader/main.go b/reader/main.go similarity index 82% rename from services/reader/main.go rename to reader/main.go index cdb732d..1d79e0f 100644 --- a/services/reader/main.go +++ b/reader/main.go @@ -17,14 +17,17 @@ func main() { mux.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request){ if request.Method == "OPTIONS" { writer.WriteHeader(http.StatusOK) + fmt.Println("GET /health (down)") return } + fmt.Println("GET /health (up)") fmt.Fprintf(writer, "up") }) mux.HandleFunc("/data", func(writer http.ResponseWriter, request *http.Request) { client := redis.NewClient(&redis.Options{Addr: redis_host+":"+redis_port}) - key := client.Get(client.Context(),"SHAREDKEY") + key := client.Get("SHAREDKEY") + fmt.Println("GET /data (" + key.Val() + ")") fmt.Fprintf(writer, key.Val()) }) diff --git a/services/reader/Dockerfile b/services/reader/Dockerfile deleted file mode 100644 index 90d2094..0000000 --- a/services/reader/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM golang:1.16 -WORKDIR /go/src/github.com/PicPay/picpay-jr-devops-challenge/services/go -ADD . /go/src/github.com/PicPay/picpay-jr-devops-challenge/services/go - -EXPOSE 8080 -CMD ["go","run","main.go"] \ No newline at end of file diff --git a/services/writer/Dockerfile b/services/writer/Dockerfile deleted file mode 100644 index abc40b4..0000000 --- a/services/writer/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM python:3.9 -WORKDIR /app -ADD . /app -EXPOSE 8081 -CMD ["python"] -ENTRYPOINT ["main.py"] \ No newline at end of file diff --git a/writer/Dockerfile b/writer/Dockerfile new file mode 100644 index 0000000..039bf57 --- /dev/null +++ b/writer/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.9 +WORKDIR /app +ADD . /app +RUN pip install -r requirements.txt +EXPOSE 8081 +# CMD ["tail", "-f", "/dev/null"] +CMD ["python", "main.py"] \ No newline at end of file diff --git a/services/writer/main.py b/writer/main.py similarity index 100% rename from services/writer/main.py rename to writer/main.py diff --git a/writer/requirements.txt b/writer/requirements.txt new file mode 100644 index 0000000..74b362f --- /dev/null +++ b/writer/requirements.txt @@ -0,0 +1 @@ +redis \ No newline at end of file