|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "io" |
6 | | - "log" |
7 | | - "net/http" |
8 | | - "os" |
| 4 | + "io" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "bytes" |
9 | 9 | ) |
10 | 10 |
|
11 | | -// Estrutura do pacote vindo da sua UI |
| 11 | +// Estruturas opcionais (mantidas para clareza) |
12 | 12 | type OracleRequest struct { |
13 | | - Event string `json:"event"` |
14 | | - Payload string `json:"payload"` |
15 | | - Device string `json:"device"` |
| 13 | + Event string `json:"event"` |
| 14 | + Payload string `json:"payload"` |
| 15 | + Device string `json:"device"` |
16 | 16 | } |
17 | 17 |
|
18 | | -// Estrutura da resposta do backend privado |
19 | 18 | type OracleResponse struct { |
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"` |
| 19 | + Hash string `json:"hash"` |
| 20 | + Timestamp string `json:"timestamp"` |
| 21 | + LedgerLine string `json:"ledger_line"` |
| 22 | + Proof string `json:"proof"` |
| 23 | + Status string `json:"status"` |
25 | 24 | } |
26 | 25 |
|
27 | 26 | func main() { |
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 | | - } |
33 | | - |
34 | | - mux := http.NewServeMux() |
35 | | - |
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 | | - } |
42 | | - |
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 | | - } |
49 | | - |
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() |
61 | | - |
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 | | - } |
68 | | - |
69 | | - // Replicar resposta para a UI |
70 | | - w.Header().Set("Content-Type", "application/json") |
71 | | - w.Write(privateResp) |
72 | | - }) |
73 | | - |
74 | | - // Libera CORS para frontend local ou remoto |
75 | | - handler := corsMiddleware(mux) |
76 | | - |
77 | | - log.Println("🌐 Gateway Go rodando em http://localhost:7070") |
78 | | - http.ListenAndServe(":7070", handler) |
| 27 | + // URL do backend privado (NÃO aparece no GitHub) |
| 28 | + backendURL := os.Getenv("PRIVATE_ORACLE_URL") |
| 29 | + if backendURL == "" { |
| 30 | + log.Fatal("Erro: a variável PRIVATE_ORACLE_URL não está definida") |
| 31 | + } |
| 32 | + |
| 33 | + mux := http.NewServeMux() |
| 34 | + |
| 35 | + // Endpoint chamado pela UI → repassa para o backend /mel |
| 36 | + mux.HandleFunc("/oracle", func(w http.ResponseWriter, r *http.Request) { |
| 37 | + if r.Method != http.MethodPost { |
| 38 | + http.Error(w, "Método inválido", http.StatusMethodNotAllowed) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // Ler JSON vindo da UI |
| 43 | + body, err := io.ReadAll(r.Body) |
| 44 | + if err != nil { |
| 45 | + http.Error(w, "Erro ao ler body", http.StatusBadRequest) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Encaminhar para o backend PRIVADO → /mel |
| 50 | + resp, err := http.Post( |
| 51 | + backendURL+"/mel", |
| 52 | + "application/json", |
| 53 | + bytes.NewReader(body), |
| 54 | + ) |
| 55 | + if err != nil { |
| 56 | + http.Error(w, "Erro ao contactar backend privado", http.StatusBadGateway) |
| 57 | + return |
| 58 | + } |
| 59 | + defer resp.Body.Close() |
| 60 | + |
| 61 | + // Ler resposta do backend |
| 62 | + privateResp, err := io.ReadAll(resp.Body) |
| 63 | + if err != nil { |
| 64 | + http.Error(w, "Erro ao ler resposta backend privado", http.StatusBadGateway) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // Retornar para a UI |
| 69 | + w.Header().Set("Content-Type", "application/json") |
| 70 | + w.Write(privateResp) |
| 71 | + }) |
| 72 | + |
| 73 | + // CORS |
| 74 | + handler := corsMiddleware(mux) |
| 75 | + |
| 76 | + log.Println("🌐 Gateway Go rodando em http://localhost:7070") |
| 77 | + http.ListenAndServe(":7070", handler) |
79 | 78 | } |
80 | 79 |
|
| 80 | +// Middleware simples de CORS |
81 | 81 | func corsMiddleware(next http.Handler) http.Handler { |
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") |
| 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") |
86 | 86 |
|
87 | | - if r.Method == http.MethodOptions { |
88 | | - return |
89 | | - } |
| 87 | + if r.Method == http.MethodOptions { |
| 88 | + return |
| 89 | + } |
90 | 90 |
|
91 | | - next.ServeHTTP(w, r) |
92 | | - }) |
| 91 | + next.ServeHTTP(w, r) |
| 92 | + }) |
93 | 93 | } |
| 94 | + |
0 commit comments