package webserver

import (
	"time"

	"git.lxtend.com/lixiangwuxian/qqbot/handler/restart"
	"git.lxtend.com/lixiangwuxian/qqbot/handler/ticket"
	"git.lxtend.com/lixiangwuxian/qqbot/health"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func StartRouter() {
	ginServer := gin.New()
	ginServer.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"https://www.lxtend.com", "https://git.lxtend.com"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Accept", "Authorization", "X-CSRF-Token"},
		ExposeHeaders:    []string{"Content-Length", "X-CSRF-Token"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	healthEngine := ginServer.Group("/health")
	healthEngine.GET("/ping", health.HealthHandler)
	ticketEngine := ginServer.Group("/ticket")
	ticketEngine.GET("", ticket.TicketHandler)
	gitEngine := ginServer.Group("/git")
	gitEngine.GET("/pull", restart.PullCodeHandler)
	gitEngine.GET("/build", restart.BuildBotHandler)
	gitEngine.GET("/restart", restart.RestartBotHandler)
	gitEngine.GET("/all", restart.AllInOneHandler)
	go ginServer.Run(":3434")
}
