110 lines
2.7 KiB
Go
110 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"wish-list-api/api/routes"
|
|
_ "wish-list-api/docs"
|
|
"wish-list-api/pkg/auth"
|
|
"wish-list-api/pkg/user"
|
|
wishlist "wish-list-api/pkg/wish-list"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/fiber/v2/middleware/redirect"
|
|
|
|
swagger "github.com/swaggo/fiber-swagger"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// @title Wish List API
|
|
// @version 1.0
|
|
// @description API-сервер для приложения списка желаний
|
|
// @securityDefinitions.apikey BearerAuth
|
|
// @in header
|
|
// @name Authorization
|
|
// @license.name Apache 2.0
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
// @host localhost:8080
|
|
// @BasePath /api
|
|
func main() {
|
|
db, cancel, err := databaseConnection()
|
|
defer cancel()
|
|
|
|
if err != nil {
|
|
log.Fatal("Database Connection Error $s", err)
|
|
}
|
|
|
|
fmt.Println("Database connection success!")
|
|
|
|
userCollection := db.Collection("users")
|
|
userRepo := user.NewRepo(userCollection)
|
|
userService := user.NewService(userRepo)
|
|
|
|
wishListCollection := db.Collection("wish_lists")
|
|
wishListItemCollection := db.Collection("wish_list_items")
|
|
wishListRepo := wishlist.NewRepo(wishListCollection, wishListItemCollection)
|
|
wishListService := wishlist.NewService(wishListRepo)
|
|
|
|
authService := auth.NewService(auth.ServiceConfig{
|
|
UserService: userService,
|
|
})
|
|
|
|
telegramAuthService := auth.NewTelegramAuthService(auth.TelegramAuthConfig{
|
|
UserService: userService,
|
|
AuthService: authService,
|
|
BotToken: os.Getenv("TELEGRAM_BOT_TOKEN"),
|
|
TelegramAppUrl: os.Getenv("TELEGRAM_APP_URL"),
|
|
})
|
|
|
|
app := fiber.New()
|
|
app.Use(cors.New())
|
|
app.Use(logger.New())
|
|
|
|
app.Use(redirect.New(redirect.Config{
|
|
Rules: map[string]string{
|
|
"/": "/docs/index.html",
|
|
},
|
|
StatusCode: 301,
|
|
}))
|
|
|
|
app.Get("/docs/*", swagger.FiberWrapHandler())
|
|
|
|
api := app.Group("/api")
|
|
|
|
routes.AuthRouter(api, authService, telegramAuthService)
|
|
routes.UserRouter(api, userService, authService)
|
|
routes.WishListRouter(api, wishListService, authService)
|
|
|
|
log.Fatal(app.Listen(":8080"))
|
|
}
|
|
|
|
func databaseConnection() (*mongo.Database, context.CancelFunc, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
mongoURI := os.Getenv("MONGODB_URI")
|
|
|
|
fmt.Println("MongoDB URI: ", mongoURI)
|
|
|
|
if mongoURI == "" {
|
|
mongoURI = "mongodb://mongo_user:mongo_password@localhost:27017/admin"
|
|
}
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI).
|
|
SetServerSelectionTimeout(5*time.Second))
|
|
|
|
if err != nil {
|
|
cancel()
|
|
return nil, nil, err
|
|
}
|
|
|
|
db := client.Database("books")
|
|
return db, cancel, nil
|
|
}
|