package main

import (
	"crawler/config"
	"crawler/middleware"
	"crawler/route"
	"crawler/service"
	"crawler/utility"
	"log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

/*
@author 김국현
@date 2022.12.25
*/
func main() {
	utility.SetEnviron()
	utility.SetDebug()

	// MySQL DB 연결
	db := new(service.DB)
	service.DB_MOVIEW = db.Connection(config.DB_MOVIEW)
	service.DB_CRAWLER = db.Connection(config.DB_CRAWLER)
	service.DB_PLAYR = db.Connection(config.DB_PLAYR)

	app := gin.Default()
	app.Use(middleware.Access())          // 접속기록
	app.Use(middleware.GinBodyResponse()) // request 요청 로그

	// Routing
	route.SetRoute(app)

	var port = config.Env.APP.Port

	server := &http.Server{
		Addr:           ":" + port,
		Handler:        app,
		ReadTimeout:    time.Duration(config.Env.APP.ReadTimeout) * time.Second,
		WriteTimeout:   time.Duration(config.Env.APP.WriteTimeout) * time.Second,
		MaxHeaderBytes: config.Env.APP.MaxHeaderBytes << 20,
	}

	// 실행 로그 채널 생성
	log.Println("Server is starting on port:", port)
	service.InitLogWorker(service.DB_CRAWLER.SQLDB)
	defer service.StopLogWorker()

	log.Println("Server was started successfully !!")
	log.Println(server.ListenAndServe())
}
