package middleware

import (
	"github.com/labstack/echo/v4"
	//"time"
	"log"
	"os"
	//"http/net"
	//"pastebin_backend/app/controller"
)

func Auth(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		mylogger(c)
		c.Set("uid", 114514)
		return next(c)
	}
}

type logInfo struct {
	User_Agent string
	IP         string
	time       string
	path       string
}

/*
向文件或 stdout 输出详细的日志，记录用户的 User-Agent、IP 地址、访问时间、访问路径等信息
User-Agent,访问路径,暂且无法实现
*/
func mylogger(c echo.Context) {
	//c.GET("User-Agent")
	//userAgent := controller.Ctx.Request.Header.Get("User-Agent")
	ip := echo.ExtractIPDirect()(c.Request())

	//日志输出文件
	file, err := os.OpenFile("backend/files/sys.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalln("Faild to open error logger file:", err)
	}
	//自定义日志格式
	Info := log.New(file, "[Info]", log.Llongfile)
	Info.SetFlags(log.Ldate | log.Ltime)
	Info.Println(ip)
}
