-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
589 lines (503 loc) · 15.6 KB
/
main.go
File metadata and controls
589 lines (503 loc) · 15.6 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/joho/godotenv"
)
// Command-line flags.
var (
httpAddr = flag.String("addr", "localhost:8080", "Listen address")
)
func main() {
flag.Parse()
// Initialize Gin router
r := gin.Default()
// Prevent caching globally so sensitive tokens/pages aren't stored
r.Use(func(c *gin.Context) {
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Header("Pragma", "no-cache")
c.Header("Expires", "0")
c.Next()
})
// Serve static files
r.Static("/static", "./static")
// Serve favicon from static directory
r.StaticFile("/favicon.ico", "./static/favicon.ico")
// Load HTML templates from the templates directory
r.LoadHTMLGlob("templates/*")
// Define routes
r.GET("/", HomeHandler)
r.POST("/request-login", RequestLoginHandler)
r.POST("/login", LoginHandler)
r.GET("/dashboard", DashboardHandler)
r.GET("/api-docs", ApiDocsHandler)
// Internal API routes (for dashboard)
api := r.Group("/api", AuthMiddleware())
{
api.GET("/local-data", LocalDataHandler)
api.GET("/credits/:licenseId", GetCreditsHandler)
api.POST("/check-credits", CheckCreditsHandler)
api.POST("/report-usage", ReportUsageHandler)
api.POST("/update-credits", UpdateCreditsHandler)
api.DELETE("/delete-credits/:licenseId", DeleteCreditsHandler)
// New API entries
api.POST("/create-user", CreateUserHandler)
api.POST("/delete-user", DeleteUserHandler)
// Model access management
api.PUT("/users/:licenseId/models", UpdateModelAccessHandler)
api.GET("/users/:licenseId/models", GetModelAccessHandler)
// Agent management
api.POST("/agents", CreateAgentHandler)
api.DELETE("/agents/:agentName", DeleteAgentHandler)
}
host := os.Getenv("SERVER_HOST")
port := os.Getenv("SERVER_PORT")
if port == "" {
port = "8080"
}
if host == "" {
host = "0.0.0.0" // Bind to all interfaces (for IPv4 access)
}
tlsCert := os.Getenv("TLS_CERT")
tlsKey := os.Getenv("TLS_KEY")
useTLS := tlsCert != "" && tlsKey != ""
addr := host + ":" + port
scheme := "http"
if useTLS {
scheme = "https"
}
// Start the Ollama reverse proxy on a separate port (non-blocking).
go StartOllamaProxy()
// Print a clean, informative startup message
log.Printf("=====================================================")
log.Printf("Easy AI API Gateway")
log.Printf("-----------------------------------------------------")
log.Printf("Local Access: %s://localhost:%s", scheme, port)
log.Printf("Network Access: %s://%s:%s", scheme, "your-ip-address", port)
if useTLS {
log.Printf("TLS Cert: %s", tlsCert)
log.Printf("TLS Key: %s", tlsKey)
}
log.Printf("=====================================================")
if useTLS {
if err := r.RunTLS(addr, tlsCert, tlsKey); err != nil {
log.Fatalf("could not start TLS server: %v", err)
}
} else {
if err := r.Run(addr); err != nil {
log.Fatalf("could not start server: %v", err)
}
}
}
func isDevMode() bool {
val := os.Getenv("DEV_MODE")
return val == "true" || val == "1"
}
func HomeHandler(c *gin.Context) {
if isDevMode() {
c.Redirect(http.StatusSeeOther, "/dashboard")
return
}
c.HTML(http.StatusOK, "login.html", nil)
}
func RequestLoginHandler(c *gin.Context) {
adminEmail := os.Getenv("ADMIN_EMAIL")
if adminEmail == "" {
log.Println("ADMIN_EMAIL not set")
c.String(http.StatusInternalServerError, "Server configuration error")
return
}
token, err := generateToken()
if err != nil {
log.Println("Error generating token:", err)
c.String(http.StatusInternalServerError, "Internal Server Error")
return
}
storeToken(adminEmail, token)
err = sendLoginToken(adminEmail, token)
if err != nil {
log.Println("Error sending email:", err)
c.String(http.StatusInternalServerError, "Failed to send login email")
return
}
c.String(http.StatusOK, "Login token sent to admin email. Please check your inbox.")
}
func LoginHandler(c *gin.Context) {
token := c.PostForm("token")
adminEmail := os.Getenv("ADMIN_EMAIL")
if isValidToken(adminEmail, token) {
// In a real app, set a secure session cookie here
// For simplicity, we just redirect. You MUST implement proper sessions for a real app.
invalidateToken(adminEmail)
c.Redirect(http.StatusSeeOther, "/dashboard")
return
}
c.String(http.StatusUnauthorized, "Invalid or expired token")
}
func DashboardHandler(c *gin.Context) {
// Real app needs session validation here!
primeKey := os.Getenv("PRIME_KEY")
c.HTML(http.StatusOK, "dashboard.html", gin.H{
"PrimeKey": primeKey,
})
}
func ApiDocsHandler(c *gin.Context) {
primeKey := os.Getenv("PRIME_KEY")
c.HTML(http.StatusOK, "api-docs.html", gin.H{
"PrimeKey": primeKey,
})
}
type UserCredits struct {
LicenseID string `json:"licenseId"`
Email string `json:"userEmail"`
Balance int `json:"monthlyToken"`
CreditsTopup int `json:"topUpToken"`
TokensUsed int `json:"usedToken"`
LastUpdated int64 `json:"lastUpdated"`
Application string `json:"application"`
ModelAccessList []string `json:"modelAccessList,omitempty"`
}
// UnmarshalJSON supports both old and new JSON field names for backward compatibility
// with existing encrypted cache files.
func (u *UserCredits) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
getString := func(keys ...string) string {
for _, k := range keys {
if v, ok := raw[k]; ok {
var s string
if json.Unmarshal(v, &s) == nil {
return s
}
}
}
return ""
}
getInt := func(keys ...string) int {
for _, k := range keys {
if v, ok := raw[k]; ok {
var n int
if json.Unmarshal(v, &n) == nil {
return n
}
}
}
return 0
}
getInt64 := func(keys ...string) int64 {
for _, k := range keys {
if v, ok := raw[k]; ok {
var n int64
if json.Unmarshal(v, &n) == nil {
return n
}
}
}
return 0
}
getStringSlice := func(keys ...string) []string {
for _, k := range keys {
if v, ok := raw[k]; ok {
var s []string
if json.Unmarshal(v, &s) == nil {
return s
}
}
}
return nil
}
u.LicenseID = getString("licenseId")
u.Email = getString("userEmail", "email")
u.Balance = getInt("monthlyToken", "balance")
u.CreditsTopup = getInt("topUpToken", "creditsTopup")
u.TokensUsed = getInt("usedToken", "tokensUsed")
u.LastUpdated = getInt64("lastUpdated")
u.Application = getString("application")
u.ModelAccessList = getStringSlice("modelAccessList")
return nil
}
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
apiKey := c.GetHeader("X-API-Key")
primeKey := os.Getenv("PRIME_KEY")
if apiKey == "" {
apiKey = c.Query("api_key") // fallback to query param if needed
}
if apiKey != primeKey {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized: Invalid API Key"})
c.Abort()
return
}
c.Next()
}
}
func LocalDataHandler(c *gin.Context) {
// For the dashboard to list everything, we only return the local creditsStore
// No more Firebase connectivity.
creditsStoreMu.RLock()
users := make(map[string]*UserCredits, len(creditsStore))
for k, v := range creditsStore {
users[k] = v
}
creditsStoreMu.RUnlock()
c.JSON(http.StatusOK, gin.H{"users": users})
}
var creditsStore = make(map[string]*UserCredits)
var creditsStoreMu sync.RWMutex
func init() {
// Load .env.local early so GOOGLE_APPLICATION_CREDENTIALS (and other vars) are available
if err := godotenv.Load(".env.local"); err != nil {
log.Println("init: .env.local not found, relying on environment variables.")
}
initFirestore()
if err := loadFromFirestore(creditsStore); err != nil {
log.Println("Error loading from Firestore, starting fresh:", err)
}
if err := loadAgentsFromFirestore(); err != nil {
log.Println("Error loading agents from Firestore:", err)
}
}
func GetCreditsHandler(c *gin.Context) {
licenseId := c.Param("licenseId")
creditsStoreMu.RLock()
credits, ok := creditsStore[licenseId]
creditsStoreMu.RUnlock()
if !ok {
c.JSON(http.StatusNotFound, gin.H{"error": "License ID not found"})
return
}
c.JSON(http.StatusOK, gin.H{
"licenseId": credits.LicenseID,
"userEmail": credits.Email,
"monthlyToken": credits.Balance,
"topUpToken": credits.CreditsTopup,
"usedToken": credits.TokensUsed,
"availableToken": credits.Balance + credits.CreditsTopup - credits.TokensUsed,
"lastUpdated": credits.LastUpdated,
"application": credits.Application,
})
}
func CheckCreditsHandler(c *gin.Context) {
var req struct {
LicenseID string `json:"licenseId"`
EstimatedTokens int `json:"estimatedTokens"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
creditsStoreMu.RLock()
credits, ok := creditsStore[req.LicenseID]
creditsStoreMu.RUnlock()
if !ok {
// Default
credits = &UserCredits{Balance: 1000000}
}
available := credits.Balance + credits.CreditsTopup - credits.TokensUsed
allowed := available >= req.EstimatedTokens
c.JSON(http.StatusOK, gin.H{
"allowed": allowed,
"availableToken": available,
})
}
func ReportUsageHandler(c *gin.Context) {
var req struct {
LicenseID string `json:"licenseId"`
PromptTokens int `json:"promptTokens"`
CandidateTokens int `json:"candidateTokens"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
totalTokens := req.PromptTokens + req.CandidateTokens
log.Printf("Reporting local usage for %s: %d tokens", req.LicenseID, totalTokens)
creditsStoreMu.Lock()
credits, ok := creditsStore[req.LicenseID]
if !ok {
credits = &UserCredits{LicenseID: req.LicenseID, Balance: 1000000}
creditsStore[req.LicenseID] = credits
}
credits.TokensUsed += totalTokens
credits.LastUpdated = time.Now().UnixMilli()
creditsStoreMu.Unlock()
if err := saveUserToFirestore(credits); err != nil {
log.Println("Error saving to Firestore:", err)
}
c.JSON(http.StatusOK, gin.H{"status": "success", "tokensUsed": totalTokens, "totalTokensUsed": credits.TokensUsed})
}
func UpdateCreditsHandler(c *gin.Context) {
var req struct {
LicenseID string `json:"licenseId"`
Email string `json:"email"`
Credits int `json:"credits"`
CreditsTopup int `json:"creditsTopup"`
Application string `json:"application"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
isNew := false
creditsStoreMu.Lock()
credits, ok := creditsStore[req.LicenseID]
if !ok {
credits = &UserCredits{LicenseID: req.LicenseID}
creditsStore[req.LicenseID] = credits
isNew = true
}
credits.Email = req.Email
credits.Balance = req.Credits
credits.CreditsTopup = req.CreditsTopup
credits.Application = req.Application
credits.LastUpdated = time.Now().UnixMilli()
creditsStoreMu.Unlock()
if err := saveUserToFirestore(credits); err != nil {
log.Println("Error saving to Firestore after update:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save credits"})
return
}
if isNew {
sendNewClientEmail(credits.Email, credits.LicenseID, fmt.Sprintf("%d", credits.Balance), credits.Application)
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
func CreateUserHandler(c *gin.Context) {
var req struct {
LicenseID string `json:"licenseId"`
Email string `json:"email" binding:"required"`
Credits int `json:"credits"`
CreditsTopup int `json:"creditsTopup"`
Application string `json:"application"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()})
return
}
licenseId := req.LicenseID
if licenseId == "" {
licenseId = uuid.New().String()
}
credits := &UserCredits{
LicenseID: licenseId,
Email: req.Email,
Balance: req.Credits,
CreditsTopup: req.CreditsTopup,
TokensUsed: 0,
LastUpdated: time.Now().UnixMilli(),
Application: req.Application,
}
if credits.Balance == 0 {
credits.Balance = 1000000 // Default
}
creditsStoreMu.Lock()
creditsStore[licenseId] = credits
creditsStoreMu.Unlock()
if err := saveUserToFirestore(credits); err != nil {
log.Println("Error saving to Firestore after creation:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save new user"})
return
}
// Send welcome email to the user with their License ID and details
sendNewClientEmail(credits.Email, credits.LicenseID, fmt.Sprintf("%d", credits.Balance), credits.Application)
c.JSON(http.StatusOK, gin.H{"status": "success", "licenseId": licenseId})
}
func DeleteUserHandler(c *gin.Context) {
var req struct {
LicenseID string `json:"licenseId" binding:"required"`
Email string `json:"email" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
creditsStoreMu.Lock()
credits, ok := creditsStore[req.LicenseID]
if !ok || credits.Email != req.Email {
creditsStoreMu.Unlock()
c.JSON(http.StatusNotFound, gin.H{"error": "User not found with matching License ID and Email"})
return
}
delete(creditsStore, req.LicenseID)
creditsStoreMu.Unlock()
if err := deleteUserFromFirestore(req.LicenseID); err != nil {
log.Println("Error deleting from Firestore:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
// UpdateModelAccessHandler handles PUT /api/users/:licenseId/models.
// It updates the user's ModelAccessList in creditsStore and persists to Firestore.
func UpdateModelAccessHandler(c *gin.Context) {
licenseId := c.Param("licenseId")
var req struct {
Models []string `json:"models"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
return
}
creditsStoreMu.Lock()
user, ok := creditsStore[licenseId]
if !ok {
creditsStoreMu.Unlock()
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
return
}
user.ModelAccessList = req.Models
creditsStoreMu.Unlock()
if err := saveUserToFirestore(user); err != nil {
log.Println("Error saving model access to Firestore:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to persist changes"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
// GetModelAccessHandler handles GET /api/users/:licenseId/models.
// It returns the user's current ModelAccessList as a JSON array.
func GetModelAccessHandler(c *gin.Context) {
licenseId := c.Param("licenseId")
creditsStoreMu.RLock()
user, ok := creditsStore[licenseId]
creditsStoreMu.RUnlock()
if !ok {
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
return
}
models := user.ModelAccessList
if models == nil {
models = []string{}
}
c.JSON(http.StatusOK, gin.H{"models": models})
}
func DeleteCreditsHandler(c *gin.Context) {
licenseId := c.Param("licenseId")
creditsStoreMu.Lock()
if _, ok := creditsStore[licenseId]; !ok {
creditsStoreMu.Unlock()
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
}
delete(creditsStore, licenseId)
creditsStoreMu.Unlock()
if err := deleteUserFromFirestore(licenseId); err != nil {
log.Println("Error deleting from Firestore:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}