-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (51 loc) · 1.76 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
package main
import (
"context"
"log"
"sendright/service/openAI"
"sendright/template/handler"
"sendright/template/logic"
"sendright/utility"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
_appError "sendright/appError"
_config "sendright/config"
)
func main() {
config := _config.GetInstance()
appErrors := _appError.NewAppError()
jsonWriter := utility.NewJsonWriter()
ConnectToMongo(config.MongoDbConnStr, config.MongoTimeout).Database(config.MongoDbDataBase)
app := fiber.New()
app.Use(cors.New(cors.Config{
AllowHeaders: "Origin,Content-Type,Accept,Content-Length,Accept-Language,Accept-Encoding,Connection,Access-Control-Allow-Origin",
AllowOrigins: "*",
AllowCredentials: true,
AllowMethods: "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS",
}))
router := app.Group("api/v1")
metaRouter := router.Group("metadata")
metaLogic := logic.NewMetaLogic(appErrors)
handler.NewMetaHandler(metaRouter, metaLogic, jsonWriter)
openAIService := openAI.NewOpenAIService(config.OpenAISecret)
templateLogic := logic.NewTemplateLogic(openAIService, appErrors)
templateRouter := router.Group("template")
handler.NewTemplateHandler(templateRouter, templateLogic, appErrors, jsonWriter)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})
app.Listen(config.ServiceAddress)
}
func ConnectToMongo(connStr string, mongoTimeout int64) *mongo.Client {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(mongoTimeout))
defer cancel()
clientOptions := options.Client().ApplyURI(connStr)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
return client
}