cds-web/main.go

120 lines
2.7 KiB
Go
Raw Normal View History

2022-10-09 02:15:25 +02:00
package main
import (
2022-10-16 03:16:51 +02:00
"cds/controllers"
2022-10-09 02:15:25 +02:00
"cds/dao"
"cds/models"
2022-10-21 03:12:22 +02:00
"cds/routes"
2022-10-09 02:15:25 +02:00
"fmt"
"os"
_ "cds/docs"
"github.com/gofiber/fiber/v2"
2022-10-21 03:12:22 +02:00
"github.com/gofiber/swagger"
2022-10-09 02:15:25 +02:00
"github.com/joho/godotenv"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/discord"
"github.com/markbates/goth/providers/steam"
"github.com/shareed2k/goth_fiber"
"go.mongodb.org/mongo-driver/mongo"
)
//const DISCORD_API = "https://discord.com/api/v10/"
func setupRoutes(app *fiber.App) {
2022-10-21 03:12:22 +02:00
//app.Get("/", index)
api := app.Group("/api")
routes.GroupRoute(api.Group("/groups"))
routes.UserRoute(api.Group("/users"))
2022-10-21 20:09:10 +02:00
routes.GameRoute(api.Group("/games"))
2022-10-21 03:12:22 +02:00
app.Get("/swagger/*", swagger.HandlerDefault)
2022-10-09 02:15:25 +02:00
app.Get("/login/:provider", goth_fiber.BeginAuthHandler)
2022-10-16 03:16:51 +02:00
app.Get("/auth/:provider", controllers.Auth)
app.Get("/check", controllers.CheckAuth)
//app.Get("/groups/:id", groups)
2022-10-09 02:15:25 +02:00
}
}
}
func index(c *fiber.Ctx) error {
token := c.Cookies("token", "")
var discord string = ""
var steam string = ""
var listGroups []models.Group
2022-10-09 02:15:25 +02:00
if token != "" {
user, _ := dao.GetById(fmt.Sprint(token))
if user != nil {
steam = user.SteamId
discord = user.DiscordName
listGroups, _ = dao.GetGroupByMember(fmt.Sprint(token))
2022-10-09 02:15:25 +02:00
}
}
return c.Render("index", fiber.Map{
"connected": token != "",
"urlDiscord": "/login/discord",
"urlSteam": "/login/steam",
"discordName": discord,
"steamName": steam,
"listGroups": listGroups,
})
}
func groups(c *fiber.Ctx) error {
groupId := c.Params("id")
group, err := dao.GetGroupById(groupId)
if err == mongo.ErrNoDocuments {
return c.SendStatus(fiber.StatusNotFound)
} else if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
token := c.Cookies("token", "")
joined, _ := dao.CheckMemberInGroup(fmt.Sprint(token), groupId)
game, _ := dao.GetGameById(group.Game.Hex())
return c.Render("group", fiber.Map{
"name": group.Name,
"game": game.Name,
"joined": joined,
2022-10-09 02:15:25 +02:00
})
}
// @title CDS API
// @version 1.0 (alpha)
// @description API du site web de CDS
// @contact.name La super équipe de dev
// @license.name AGPL 3.0
// @license.url https://www.gnu.org/licenses/agpl-3.0.html
// @host localhost:8080
// @BasePath /api
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
2022-10-21 03:12:22 +02:00
app := fiber.New()
2022-10-09 02:15:25 +02:00
goth.UseProviders(
discord.New(
os.Getenv("DISCORD_ID"),
os.Getenv("DISCORD_SECRET"),
"http://localhost:8080/auth/discord",
discord.ScopeIdentify, discord.ScopeGuilds),
steam.New(os.Getenv("STEAM_KEY"), "http://localhost:8080/auth/steam"),
)
setupRoutes(app)
2022-10-21 03:12:22 +02:00
app.Static("/", "./dist")
app.Static("/*", "./dist")
2022-10-09 02:15:25 +02:00
err = app.Listen(":8080")
if err != nil {
panic(err)
}
}