Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
up:
docker-compose up -d --build

down:
docker-compose down
32 changes: 27 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
6 changes: 3 additions & 3 deletions services/frontend/Dockerfile → frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:latest
WORKDIR /app
ADD . .
ADD . /app
RUN npm install -g serve
EXPOSE 5000
CMD "serve"
EXPOSE 5001
CMD ["serve", "-l", "5000"]
File renamed without changes.
90 changes: 45 additions & 45 deletions services/frontend/js/app.js → frontend/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -39,49 +39,49 @@ const writer = () => {
<button type="submit" class="btn btn-default">Enviar</button>
</form>
`
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")
})
}

Expand All @@ -92,48 +92,48 @@ const assembleStatus = (paragraph, service, status) => {
statusIcon = 'success'
statusBadge = 'success'
}
paragraph.innerHTML=
service + ' service status <span class="badge badge-'+statusBadge+'">'+status+'</span>'
paragraph.innerHTML =
service + ' service status <span class="badge badge-' + statusBadge + '">' + status + '</span>'
}

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,
mode: 'cors',
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 = `
<div class="alert alert-warning" role="alert">
Ocorreu um erro ao buscar a chave
</div>
`
})
})
})
.catch( error => {
.catch(error => {
console.log(error)
})
}
Expand All @@ -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 = () => {
Expand All @@ -154,7 +154,7 @@ const renderPage = () => {
root.appendChild(routes[page])
}

window.addEventListener('load', ()=> {
window.addEventListener('load', () => {
renderPage();
init();
});
File renamed without changes.
5 changes: 5 additions & 0 deletions reader/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM golang:1.16
WORKDIR /app
ADD . /app
EXPOSE 8080
CMD ["go", "run", "main.go"]
8 changes: 8 additions & 0 deletions reader/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 4 additions & 0 deletions reader/go.sum
Original file line number Diff line number Diff line change
@@ -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=
5 changes: 4 additions & 1 deletion services/reader/main.go → reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down
6 changes: 0 additions & 6 deletions services/reader/Dockerfile

This file was deleted.

6 changes: 0 additions & 6 deletions services/writer/Dockerfile

This file was deleted.

7 changes: 7 additions & 0 deletions writer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
File renamed without changes.
1 change: 1 addition & 0 deletions writer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
redis