-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathmain.go
More file actions
559 lines (490 loc) · 16.4 KB
/
Copy pathmain.go
File metadata and controls
559 lines (490 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Package main implements the gateway HTTP server used by MicroAI-Paygate.
// It provides request handlers, middleware, and configuration helpers
// for timeouts and rate limiting.
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"os/signal"
"syscall"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/joho/godotenv"
)
type PaymentContext struct {
Recipient string `json:"recipient"`
Token string `json:"token"`
Amount string `json:"amount"`
Nonce string `json:"nonce"`
ChainID int `json:"chainId"`
}
type VerifyRequest struct {
Context PaymentContext `json:"context"`
Signature string `json:"signature"`
}
type VerifyResponse struct {
IsValid bool `json:"is_valid"`
RecoveredAddress string `json:"recovered_address"`
Error string `json:"error"`
}
type SummarizeRequest struct {
Text string `json:"text"`
}
func validateConfig() error {
required := []string{
"OPENROUTER_API_KEY",
}
var missing []string
for _, key := range required {
if os.Getenv(key) == "" {
missing = append(missing, key)
}
}
if len(missing) > 0 {
return fmt.Errorf("missing required environment variables: %v", missing)
}
return nil
}
func main() {
// Try loading .env from current directory first, then fallback to parent
err := godotenv.Load(".env")
if err != nil {
// fallback to parent
err = godotenv.Load("../.env")
if err != nil {
log.Println("Warning: Error loading .env file")
}
}
if err := validateConfig(); err != nil {
fmt.Println("[Error] Missing required environment variables:")
fmt.Println(" -", err.Error())
fmt.Println()
fmt.Println("Copy .env.example to .env and fill in the required values.")
fmt.Println("See README.md for more configuration details.")
os.Exit(1)
}
fmt.Println("[OK] Configuration validated")
if port := os.Getenv("PORT"); port != "" {
fmt.Printf(" - Port: %s\n", port)
}
if model := os.Getenv("MODEL"); model != "" {
fmt.Printf(" - Model: %s\n", model)
}
if verifier := os.Getenv("VERIFIER_URL"); verifier != "" {
fmt.Printf(" - Verifier: %s\n", verifier)
}
if chainID := os.Getenv("CHAIN_ID"); chainID != "" {
fmt.Printf(" - Chain ID: %s\n", chainID)
}
if os.Getenv("PORT") == "" {
fmt.Println("[WARN] PORT not set, using default: 3000")
}
if os.Getenv("MODEL") == "" {
fmt.Println("[WARN] MODEL not set, using default model")
}
if os.Getenv("VERIFIER_URL") == "" {
fmt.Println("[WARN] VERIFIER_URL not set, using default verifier")
}
if os.Getenv("CHAIN_ID") == "" {
fmt.Println("[WARN] CHAIN_ID not set, using default: 8453(base)")
}
r := gin.Default()
r.StaticFile("/openapi.yaml", "openapi.yaml")
r.GET("/docs", func(c *gin.Context) {
c.Header("Content-Type", "text/html")
c.String(200, `
<!DOCTYPE html>
<html>
<head>
<title>MicroAI Paygate Docs</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: '/openapi.yaml',
dom_id: '#swagger-ui'
});
</script>
</body>
</html>
`)
})
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3001"},
AllowMethods: []string{"GET", "POST", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "X-402-Signature", "X-402-Nonce"},
ExposeHeaders: []string{"Content-Length", "X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset", "Retry-After"},
AllowCredentials: true,
}))
// Initialize rate limiters if enabled
if getRateLimitEnabled() {
limiters := initRateLimiters()
r.Use(RateLimitMiddleware(limiters))
log.Println("Rate limiting enabled")
}
// Global request timeout middleware (default: 60s).
// Note: route-specific timeouts (e.g. for AI endpoints) may shorten this
// deadline; the middleware implementation always uses the earliest
// deadline when nested timeouts are present to avoid surprising behavior.
r.Use(RequestTimeoutMiddleware(getRequestTimeout()))
// Health check with shorter timeout (2s)
r.GET("/healthz", RequestTimeoutMiddleware(getHealthCheckTimeout()), handleHealth)
// AI endpoints with AI-specific timeout (30s)
aiGroup := r.Group("/api/ai")
aiGroup.Use(RequestTimeoutMiddleware(getAITimeout()))
aiGroup.POST("/summarize", handleSummarize)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
addr := ":" + port
srv := &http.Server{
Addr: addr,
Handler: r,
}
go func() {
log.Printf("[INFO] Gateway listening on %s", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("[FATAL] listen error: %v", err)
}
}()
// ---- Graceful shutdown ----
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("[INFO] Shutdown signal received, draining connections...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[ERROR] Server forced to shutdown: %v", err)
} else {
log.Println("[OK] Server shutdown completed")
}
}
// handleSummarize handles POST /api/ai/summarize requests. It validates
// payment headers, calls the verifier service to validate the signature, and
// forwards the text to the AI service. The handler respects context timeouts
// applied by middleware and returns appropriate HTTP errors (402, 403, 504,
// 500) to the client.
func handleSummarize(c *gin.Context) {
signature := c.GetHeader("X-402-Signature")
nonce := c.GetHeader("X-402-Nonce")
// 1. Payment Required
if signature == "" || nonce == "" {
paymentContext := createPaymentContext()
c.JSON(402, gin.H{
"error": "Payment Required",
"message": "Please sign the payment context",
"paymentContext": paymentContext,
})
return
}
// 2. Verify Payment (Call Rust Service)
paymentCtx := PaymentContext{
Recipient: getRecipientAddress(),
Token: "USDC",
Amount: getPaymentAmount(),
Nonce: nonce,
ChainID: getChainID(),
}
verifyReq := VerifyRequest{
Context: paymentCtx,
Signature: signature,
}
verifyBody, err := json.Marshal(verifyReq)
if err != nil {
log.Printf("error marshaling verification request: %v", err)
c.JSON(500, gin.H{"error": "Failed to create verification request"})
return
}
verifierURL := os.Getenv("VERIFIER_URL")
if verifierURL == "" {
verifierURL = "http://127.0.0.1:3002"
}
// Call verifier with its own timeout
verifierCtx, verifierCancel := context.WithTimeout(c.Request.Context(), getVerifierTimeout())
defer verifierCancel()
vreq, err := http.NewRequestWithContext(verifierCtx, "POST", verifierURL+"/verify", bytes.NewBuffer(verifyBody))
if err != nil {
// If the request cannot be created, return 500
c.JSON(500, gin.H{"error": "Invalid verifier request", "details": err.Error()})
return
}
vreq.Header.Set("Content-Type", "application/json")
// Use http.DefaultClient and rely on verifierCtx for timeouts/cancellation.
resp, err := http.DefaultClient.Do(vreq)
if err != nil {
// If the verifier or parent context timed out, return Gateway Timeout
if errors.Is(err, context.DeadlineExceeded) || verifierCtx.Err() == context.DeadlineExceeded || c.Request.Context().Err() == context.DeadlineExceeded {
c.JSON(504, gin.H{"error": "Gateway Timeout", "message": "Verifier request timed out"})
return
}
c.JSON(500, gin.H{"error": "Verification service unavailable"})
return
}
defer resp.Body.Close()
var verifyResp VerifyResponse
if err := json.NewDecoder(resp.Body).Decode(&verifyResp); err != nil {
c.JSON(500, gin.H{"error": "Failed to decode verification response"})
return
}
if !verifyResp.IsValid {
c.JSON(403, gin.H{"error": "Invalid Signature", "details": verifyResp.Error})
return
}
// 3. Call AI Service
var req SummarizeRequest
if err := c.BindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": "Invalid request body"})
return
}
summary, err := callOpenRouter(c.Request.Context(), req.Text)
if err != nil {
// If the error was due to a timeout, return 504
if errors.Is(err, context.DeadlineExceeded) || c.Request.Context().Err() == context.DeadlineExceeded {
c.JSON(504, gin.H{"error": "Gateway Timeout", "message": "AI request timed out"})
return
}
c.JSON(500, gin.H{"error": "AI Service Failed", "details": err.Error()})
return
}
c.JSON(200, gin.H{"result": summary})
}
// createPaymentContext constructs a PaymentContext prefilled with the recipient address (from RECIPIENT_ADDRESS or a fallback), the USDC token, amount "0.001", a newly generated UUID nonce, and chain ID 8453.
func createPaymentContext() PaymentContext {
return PaymentContext{
Recipient: getRecipientAddress(),
Token: "USDC",
Amount: getPaymentAmount(),
Nonce: uuid.New().String(),
ChainID: getChainID(),
}
}
// getRecipientAddress retrieves the recipient address from the RECIPIENT_ADDRESS environment variable.
// If RECIPIENT_ADDRESS is unset, it logs a warning and returns the default address "0x2cAF48b4BA1C58721a85dFADa5aC01C2DFa62219".
func getRecipientAddress() string {
addr := os.Getenv("RECIPIENT_ADDRESS")
if addr == "" {
log.Println("Warning: RECIPIENT_ADDRESS not set, using default")
return "0x2cAF48b4BA1C58721a85dFADa5aC01C2DFa62219"
}
return addr
}
// getPaymentAmount returns the payment amount from the PAYMENT_AMOUNT environment variable.
// If unset, it defaults to "0.001".
func getPaymentAmount() string {
amount := os.Getenv("PAYMENT_AMOUNT")
if amount == "" {
return "0.001"
}
return amount
}
// getChainID returns the blockchain chain ID from the CHAIN_ID environment variable.
// If unset or invalid, it defaults to 8453 (Base).
func getChainID() int {
chainIDStr := os.Getenv("CHAIN_ID")
if chainIDStr == "" {
return 8453
}
chainID, err := strconv.Atoi(chainIDStr)
if err != nil {
log.Printf("Warning: Invalid CHAIN_ID '%s', using default 8453", chainIDStr)
return 8453
}
return chainID
}
// callOpenRouter sends the given text to the OpenRouter chat completions API
// requesting a two-sentence summary and returns the generated summary.
// It reads OPENROUTER_API_KEY for authorization and OPENROUTER_MODEL to select
// the model (defaults to "z-ai/glm-4.5-air:free" if unset).
func callOpenRouter(ctx context.Context, text string) (string, error) {
apiKey := os.Getenv("OPENROUTER_API_KEY")
model := os.Getenv("OPENROUTER_MODEL")
if model == "" {
model = "z-ai/glm-4.5-air:free"
}
prompt := fmt.Sprintf("Summarize this text in 2 sentences: %s", text)
reqBody, _ := json.Marshal(map[string]interface{}{
"model": model,
"messages": []map[string]string{
{"role": "user", "content": prompt},
},
})
openRouterURL := os.Getenv("OPENROUTER_URL")
if openRouterURL == "" {
openRouterURL = "https://openrouter.ai/api/v1/chat/completions"
}
req, err := http.NewRequestWithContext(ctx, "POST", openRouterURL, bytes.NewBuffer(reqBody))
if err != nil {
return "", fmt.Errorf("failed to create OpenRouter request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Use http.DefaultClient and rely on ctx for cancellation/timeouts.
resp, err := http.DefaultClient.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) || ctx.Err() == context.DeadlineExceeded {
return "", context.DeadlineExceeded
}
return "", err
}
defer resp.Body.Close()
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", fmt.Errorf("failed to decode AI response: %w", err)
}
choices, ok := result["choices"].([]interface{})
if !ok || len(choices) == 0 {
return "", fmt.Errorf("invalid response from AI provider: no choices")
}
choice, ok := choices[0].(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid response from AI provider: malformed choice")
}
message, ok := choice["message"].(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid response from AI provider: malformed message")
}
content, ok := message["content"].(string)
if !ok {
return "", fmt.Errorf("invalid response from AI provider: missing content")
}
return content, nil
}
func handleHealth(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok", "service": "gateway"})
}
// Rate Limiting Functions
// initRateLimiters creates rate limiters for each tier
func initRateLimiters() map[string]RateLimiter {
cleanupInterval := getEnvAsInt("RATE_LIMIT_CLEANUP_INTERVAL", 300)
cleanupTTL := time.Duration(cleanupInterval) * time.Second
return map[string]RateLimiter{
"anonymous": NewTokenBucket(
getEnvAsInt("RATE_LIMIT_ANONYMOUS_RPM", 10),
getEnvAsInt("RATE_LIMIT_ANONYMOUS_BURST", 5),
cleanupTTL,
),
"standard": NewTokenBucket(
getEnvAsInt("RATE_LIMIT_STANDARD_RPM", 60),
getEnvAsInt("RATE_LIMIT_STANDARD_BURST", 20),
cleanupTTL,
),
"verified": NewTokenBucket(
getEnvAsInt("RATE_LIMIT_VERIFIED_RPM", 120),
getEnvAsInt("RATE_LIMIT_VERIFIED_BURST", 50),
cleanupTTL,
),
}
}
// RateLimitMiddleware applies rate limiting to requests
func RateLimitMiddleware(limiters map[string]RateLimiter) gin.HandlerFunc {
return func(c *gin.Context) {
// Determine rate limit key and tier
key := getRateLimitKey(c)
tier := selectRateLimitTier(c)
limiter := limiters[tier]
// Check if request is allowed
if !limiter.Allow(key) {
retryAfter := calculateRetryAfter(limiter, key)
c.Header("Retry-After", strconv.Itoa(retryAfter))
c.Header("X-RateLimit-Limit", strconv.Itoa(getLimitForTier(tier)))
c.Header("X-RateLimit-Remaining", "0")
c.Header("X-RateLimit-Reset", strconv.FormatInt(limiter.GetResetTime(key), 10))
c.JSON(429, gin.H{
"error": "Too Many Requests",
"message": "Rate limit exceeded. Please retry later.",
"retry_after": retryAfter,
})
c.Abort()
return
}
// Add rate limit headers to successful responses
c.Header("X-RateLimit-Limit", strconv.Itoa(getLimitForTier(tier)))
c.Header("X-RateLimit-Remaining", strconv.Itoa(limiter.GetRemaining(key)))
c.Header("X-RateLimit-Reset", strconv.FormatInt(limiter.GetResetTime(key), 10))
c.Next()
}
}
// getRateLimitKey determines the key for rate limiting (nonce/wallet > IP)
func getRateLimitKey(c *gin.Context) string {
signature := c.GetHeader("X-402-Signature")
nonce := c.GetHeader("X-402-Nonce")
// Only use nonce-based key if BOTH signature and nonce are present
// This prevents attackers from bypassing IP rate limits with fake nonces
if signature != "" && nonce != "" {
hash := sha256.Sum256([]byte(nonce))
// Use 32 hex chars (128 bits) for better collision resistance
return "nonce:" + hex.EncodeToString(hash[:])[:32]
}
return "ip:" + c.ClientIP()
}
// selectRateLimitTier determines which tier to apply based on request
func selectRateLimitTier(c *gin.Context) string {
// Check if request has signature (authenticated)
signature := c.GetHeader("X-402-Signature")
nonce := c.GetHeader("X-402-Nonce")
if signature != "" && nonce != "" {
// Future: Check if user is verified/premium
// For now, all signed requests get standard tier
return "standard"
}
// Unsigned requests get anonymous tier
return "anonymous"
}
// calculateRetryAfter calculates seconds until rate limit resets
func calculateRetryAfter(limiter RateLimiter, key string) int {
resetTime := limiter.GetResetTime(key)
now := time.Now().Unix()
retryAfter := int(resetTime - now)
if retryAfter < 1 {
return 1
}
return retryAfter
}
// getLimitForTier returns the RPM limit for a given tier
func getLimitForTier(tier string) int {
switch tier {
case "anonymous":
return getEnvAsInt("RATE_LIMIT_ANONYMOUS_RPM", 10)
case "standard":
return getEnvAsInt("RATE_LIMIT_STANDARD_RPM", 60)
case "verified":
return getEnvAsInt("RATE_LIMIT_VERIFIED_RPM", 120)
default:
return 10
}
}
// getRateLimitEnabled checks if rate limiting is enabled
func getRateLimitEnabled() bool {
enabled := strings.ToLower(os.Getenv("RATE_LIMIT_ENABLED"))
return enabled == "true" || enabled == "1"
}
// getEnvAsInt retrieves an environment variable as an integer with a default value
func getEnvAsInt(key string, defaultValue int) int {
valStr := os.Getenv(key)
if valStr == "" {
return defaultValue
}
val, err := strconv.Atoi(valStr)
if err != nil {
log.Printf("Warning: Invalid value for %s: %s, using default %d", key, valStr, defaultValue)
return defaultValue
}
return val
}