Skip to content

Commit 5d41646

Browse files
authored
Add files via upload
1 parent ed6e7ca commit 5d41646

File tree

1 file changed

+65
-66
lines changed

1 file changed

+65
-66
lines changed

oracle.go

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,93 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"io"
6-
"log"
7-
"net/http"
8-
"os"
9-
"bytes"
4+
"bytes"
5+
"io"
6+
"log"
7+
"net/http"
8+
"os"
109
)
1110

1211
// Estrutura do pacote vindo da sua UI
1312
type OracleRequest struct {
14-
Event string `json:"event"`
15-
Payload string `json:"payload"`
16-
Device string `json:"device"`
13+
Event string `json:"event"`
14+
Payload string `json:"payload"`
15+
Device string `json:"device"`
1716
}
1817

1918
// Estrutura da resposta do backend privado
2019
type OracleResponse struct {
21-
Hash string `json:"hash"`
22-
Timestamp string `json:"timestamp"`
23-
LedgerLine string `json:"ledger_line"`
24-
Proof string `json:"proof"`
25-
Status string `json:"status"`
20+
Hash string `json:"hash"`
21+
Timestamp string `json:"timestamp"`
22+
LedgerLine string `json:"ledger_line"`
23+
Proof string `json:"proof"`
24+
Status string `json:"status"`
2625
}
2726

2827
func main() {
29-
// URL do backend privado (NÃO aparece no GitHub)
30-
backendURL := os.Getenv("PRIVATE_ORACLE_URL")
31-
if backendURL == "" {
32-
log.Fatal("Erro: a variável PRIVATE_ORACLE_URL não está definida")
33-
}
28+
// URL do backend privado (NÃO aparece no GitHub)
29+
backendURL := os.Getenv("PRIVATE_ORACLE_URL")
30+
if backendURL == "" {
31+
log.Fatal("Erro: a variável PRIVATE_ORACLE_URL não está definida")
32+
}
3433

35-
mux := http.NewServeMux()
34+
mux := http.NewServeMux()
3635

37-
// Endpoint chamado pela UI
38-
mux.HandleFunc("/oracle", func(w http.ResponseWriter, r *http.Request) {
39-
if r.Method != http.MethodPost {
40-
http.Error(w, "Método inválido", http.StatusMethodNotAllowed)
41-
return
42-
}
36+
// Endpoint chamado pela UI
37+
mux.HandleFunc("/oracle", func(w http.ResponseWriter, r *http.Request) {
38+
if r.Method != http.MethodPost {
39+
http.Error(w, "Método inválido", http.StatusMethodNotAllowed)
40+
return
41+
}
4342

44-
// Receber JSON da UI
45-
body, err := io.ReadAll(r.Body)
46-
if err != nil {
47-
http.Error(w, "Erro ao ler body", http.StatusBadRequest)
48-
return
49-
}
43+
// Receber JSON da UI
44+
body, err := io.ReadAll(r.Body)
45+
if err != nil {
46+
http.Error(w, "Erro ao ler body", http.StatusBadRequest)
47+
return
48+
}
5049

51-
// Encaminhar para o backend privado
52-
resp, err := http.Post(
53-
backendURL+"/oracle",
54-
"application/json",
55-
bytes.NewReader(body),
56-
)
57-
if err != nil {
58-
http.Error(w, "Erro ao contactar backend privado", http.StatusBadGateway)
59-
return
60-
}
61-
defer resp.Body.Close()
50+
// Encaminhar para o backend privado
51+
resp, err := http.Post(
52+
backendURL+"/oracle",
53+
"application/json",
54+
bytes.NewReader(body),
55+
)
56+
if err != nil {
57+
http.Error(w, "Erro ao contactar backend privado", http.StatusBadGateway)
58+
return
59+
}
60+
defer resp.Body.Close()
6261

63-
// Ler resposta do backend privado
64-
privateResp, err := io.ReadAll(resp.Body)
65-
if err != nil {
66-
http.Error(w, "Erro ao ler resposta backend privado", http.StatusBadGateway)
67-
return
68-
}
62+
// Ler resposta do backend privado
63+
privateResp, err := io.ReadAll(resp.Body)
64+
if err != nil {
65+
http.Error(w, "Erro ao ler resposta backend privado", http.StatusBadGateway)
66+
return
67+
}
6968

70-
// Replicar resposta para a UI
71-
w.Header().Set("Content-Type", "application/json")
72-
w.Write(privateResp)
73-
})
69+
// Replicar resposta para a UI
70+
w.Header().Set("Content-Type", "application/json")
71+
w.Write(privateResp)
72+
})
7473

75-
// Libera CORS para frontend local ou remoto
76-
handler := corsMiddleware(mux)
74+
// Libera CORS para frontend local ou remoto
75+
handler := corsMiddleware(mux)
7776

78-
log.Println("🌐 Gateway Go rodando em http://localhost:7070")
79-
http.ListenAndServe(":7070", handler)
77+
log.Println("🌐 Gateway Go rodando em http://localhost:7070")
78+
http.ListenAndServe(":7070", handler)
8079
}
8180

8281
func corsMiddleware(next http.Handler) http.Handler {
83-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84-
w.Header().Set("Access-Control-Allow-Origin", "*")
85-
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
86-
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
82+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
83+
w.Header().Set("Access-Control-Allow-Origin", "*")
84+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
85+
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
8786

88-
if r.Method == http.MethodOptions {
89-
return
90-
}
87+
if r.Method == http.MethodOptions {
88+
return
89+
}
9190

92-
next.ServeHTTP(w, r)
93-
})
91+
next.ServeHTTP(w, r)
92+
})
9493
}

0 commit comments

Comments
 (0)