-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (52 loc) · 1.47 KB
/
main.go
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
package main
import (
"context"
"log"
"time"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"autogpt-api/handlers"
)
var userCollection *mongo.Collection
func main() {
// MongoDB config
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
mongodbURI := os.Getenv("MONGODB_URI")
clientOptions := options.Client().ApplyURI(mongodbURI)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
userCollection = client.Database("autogpt").Collection("users")
// Init new fiber custom app
app := fiber.New(fiber.Config{
Prefork: true,
CaseSensitive: true,
ServerHeader: "Fiber",
AppName: "autoGPT API v1.1.0",
})
// Middleware to log requests
app.Use(logger.New())
// Pass userCollection to handlers
handlers.InitHandlers(userCollection)
// Routes
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello World")
})
app.Get("/brain", handlers.OpenAIBrain)
app.Post("/openai", handlers.OpenAIHandler)
app.Post("/google", handlers.GoogleHandler)
app.Post("/anthropic", handlers.AnthropicHandler)
app.Get("/whisper", handlers.WhisperHandler)
// Init server
port := "8080"
err = app.Listen(":" + port)
if err != nil {
log.Fatal("Can't connect to port", port, ": ", err)
}
}