Commit b772f6d4 authored by chenhan wang's avatar chenhan wang
Browse files

增加了IP和时间的日记记录

parent 28f2e8e9
......@@ -3,8 +3,8 @@ package controller
import (
"net"
"net/http"
"pastebin_backend/app/response"
//"pastebin_backend/model"
"backend/app/response"
"backend/model"
//"error"
"time"
......
......@@ -2,33 +2,44 @@ 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 {
//c.Bind("User")
/*
cookie, _ := c.Cookie("user")
if cookie == nil {
err := controller.AskPasswd(c)
if err != nil {
return nil
}
}*/
//c.Set("uid", cookie.Value)
mylogger(c)
c.Set("uid", 114514)
return next(c)
}
}
type logInfo struct {
User_Agent string
IP string
time string
path string
}
/*
func CookieAuth(c echo.Context) error{
cookie, err := c.Cookie()
向文件或 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 {
return err
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)
}
......@@ -4,7 +4,6 @@ go 1.19
require (
github.com/go-playground/validator/v10 v10.11.1
github.com/google/uuid v1.1.2
github.com/labstack/echo/v4 v4.9.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.14.0
......
......@@ -123,7 +123,6 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment