|
| 1 | +package openrouter |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "uni-token-service/store" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | +) |
| 13 | + |
| 14 | +type session struct { |
| 15 | + Key string `json:"key"` |
| 16 | + UserId string `json:"userId"` |
| 17 | +} |
| 18 | + |
| 19 | +func loadSession() (session, error) { |
| 20 | + var s session |
| 21 | + data, err := store.Providers.Get("openRouter") |
| 22 | + if err != nil { |
| 23 | + return session{}, err |
| 24 | + } |
| 25 | + json.Unmarshal(data, &s) |
| 26 | + return s, nil |
| 27 | +} |
| 28 | + |
| 29 | +func saveSession(s session) error { |
| 30 | + jsonData, err := json.Marshal(s) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + store.Providers.Put("openRouter", jsonData) |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func SetupAPI(routes gin.IRoutes) { |
| 39 | + routes.POST("/authed", handleAuthed) |
| 40 | + routes.GET("/status", handleGetStatus) |
| 41 | + routes.GET("/key", handleGetKey) |
| 42 | + routes.POST("/logout", handleLogout) |
| 43 | +} |
| 44 | + |
| 45 | +func handleAuthed(c *gin.Context) { |
| 46 | + var req struct { |
| 47 | + Key string `json:"key"` |
| 48 | + UserId string `json:"userId"` |
| 49 | + } |
| 50 | + if err := c.BindJSON(&req); err != nil { |
| 51 | + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) |
| 52 | + return |
| 53 | + } |
| 54 | + err := saveSession(session{ |
| 55 | + Key: req.Key, |
| 56 | + UserId: req.UserId, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) |
| 60 | + return |
| 61 | + } |
| 62 | + c.Status(http.StatusOK) |
| 63 | +} |
| 64 | + |
| 65 | +func handleGetStatus(c *gin.Context) { |
| 66 | + s, err := loadSession() |
| 67 | + if err != nil { |
| 68 | + c.JSON(http.StatusOK, gin.H{ |
| 69 | + "authed": false, |
| 70 | + }) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + req, err := http.NewRequest("GET", "https://openrouter.ai/api/v1/credits", nil) |
| 75 | + if err != nil { |
| 76 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"}) |
| 77 | + return |
| 78 | + } |
| 79 | + req.Header.Set("Authorization", "Bearer "+s.Key) |
| 80 | + client := &http.Client{Timeout: 10 * time.Second} |
| 81 | + resp, err := client.Do(req) |
| 82 | + if err != nil { |
| 83 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch credits"}) |
| 84 | + return |
| 85 | + } |
| 86 | + defer resp.Body.Close() |
| 87 | + body, err := io.ReadAll(resp.Body) |
| 88 | + if err != nil { |
| 89 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read response"}) |
| 90 | + return |
| 91 | + } |
| 92 | + if resp.StatusCode != http.StatusOK { |
| 93 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch credits"}) |
| 94 | + return |
| 95 | + } |
| 96 | + var creditsResp struct { |
| 97 | + Data struct { |
| 98 | + TotalCredits float64 `json:"total_credits"` |
| 99 | + TotalUsage float64 `json:"total_usage"` |
| 100 | + } `json:"data"` |
| 101 | + } |
| 102 | + if err := json.Unmarshal(body, &creditsResp); err != nil { |
| 103 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to parse credits"}) |
| 104 | + return |
| 105 | + } |
| 106 | + c.JSON(http.StatusOK, gin.H{ |
| 107 | + "userId": s.UserId, |
| 108 | + "credits": creditsResp.Data.TotalCredits, |
| 109 | + "usage": creditsResp.Data.TotalUsage, |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func handleGetKey(c *gin.Context) { |
| 114 | + s, err := loadSession() |
| 115 | + if err != nil { |
| 116 | + c.Status(http.StatusUnauthorized) |
| 117 | + return |
| 118 | + } |
| 119 | + c.JSON(http.StatusOK, gin.H{"key": s.Key}) |
| 120 | +} |
| 121 | + |
| 122 | +// handleLogout handles user logout request |
| 123 | +func handleLogout(c *gin.Context) { |
| 124 | + // Remove the stored session |
| 125 | + err := store.Providers.Delete("openRouter") |
| 126 | + if err != nil { |
| 127 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear session"}) |
| 128 | + return |
| 129 | + } |
| 130 | + c.Status(http.StatusOK) |
| 131 | +} |
0 commit comments