|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | 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" |
10 | 9 | ) |
11 | 10 |
|
12 | 11 | // Estrutura do pacote vindo da sua UI |
13 | 12 | 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"` |
17 | 16 | } |
18 | 17 |
|
19 | 18 | // Estrutura da resposta do backend privado |
20 | 19 | 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"` |
26 | 25 | } |
27 | 26 |
|
28 | 27 | 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 | + } |
34 | 33 |
|
35 | | - mux := http.NewServeMux() |
| 34 | + mux := http.NewServeMux() |
36 | 35 |
|
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 | + } |
43 | 42 |
|
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 | + } |
50 | 49 |
|
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() |
62 | 61 |
|
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 | + } |
69 | 68 |
|
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 | + }) |
74 | 73 |
|
75 | | - // Libera CORS para frontend local ou remoto |
76 | | - handler := corsMiddleware(mux) |
| 74 | + // Libera CORS para frontend local ou remoto |
| 75 | + handler := corsMiddleware(mux) |
77 | 76 |
|
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) |
80 | 79 | } |
81 | 80 |
|
82 | 81 | 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") |
87 | 86 |
|
88 | | - if r.Method == http.MethodOptions { |
89 | | - return |
90 | | - } |
| 87 | + if r.Method == http.MethodOptions { |
| 88 | + return |
| 89 | + } |
91 | 90 |
|
92 | | - next.ServeHTTP(w, r) |
93 | | - }) |
| 91 | + next.ServeHTTP(w, r) |
| 92 | + }) |
94 | 93 | } |
0 commit comments