init
This commit is contained in:
parent
1d302b2433
commit
c6e76da127
8
config/config.go
Normal file
8
config/config.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
const (
|
||||||
|
//日志文件路径
|
||||||
|
LogFilePath = "testfb.log"
|
||||||
|
//地址+端口
|
||||||
|
Port = ":3030"
|
||||||
|
)
|
||||||
19
go.mod
Normal file
19
go.mod
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
module testfb
|
||||||
|
|
||||||
|
go 1.22.6
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||||
|
github.com/gofiber/fiber/v2 v2.52.5 // indirect
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||||
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
|
github.com/klauspost/compress v1.17.0 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||||
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
github.com/valyala/fasthttp v1.51.0 // indirect
|
||||||
|
github.com/valyala/tcplisten v1.0.0 // indirect
|
||||||
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
)
|
||||||
29
go.sum
Normal file
29
go.sum
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
||||||
|
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
|
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
|
||||||
|
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||||
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
|
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
|
||||||
|
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||||
|
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
|
||||||
|
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
|
||||||
|
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
|
||||||
|
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
35
main.go
Normal file
35
main.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"testfb/config"
|
||||||
|
"testfb/routes"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
//创建日志文件
|
||||||
|
f, err := os.OpenFile(config.LogFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("日志文件创建失败:", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
app := fiber.New()
|
||||||
|
|
||||||
|
app.Use(logger.New(logger.Config{
|
||||||
|
Output: f,
|
||||||
|
}))
|
||||||
|
|
||||||
|
//注册路由
|
||||||
|
routes.SetupRoutes(app)
|
||||||
|
// 启动服务
|
||||||
|
log.Println("服务启动成功,监听端口:", config.Port)
|
||||||
|
if err := app.Listen(config.Port); err != nil {
|
||||||
|
log.Fatalln("服务启动失败:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
1
middleware/logger.go
Normal file
1
middleware/logger.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package middleware
|
||||||
6
models/user.go
Normal file
6
models/user.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Username string `json:"username" validate:"required,alphanum,min=3,max=20"`
|
||||||
|
Password string `json:"password" validate:"required,min=8,max=20"`
|
||||||
|
}
|
||||||
53
routes/auth.go
Normal file
53
routes/auth.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testfb/utils"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 注册路由
|
||||||
|
func SetupRoutes(app *fiber.App) {
|
||||||
|
app.Post("/login", login)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理登录
|
||||||
|
func login(c *fiber.Ctx) error {
|
||||||
|
//请求参数是JSON格式,解析JSON数据
|
||||||
|
var data map[string]string
|
||||||
|
if err := c.BodyParser(&data); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"message": "Invalid request",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
//把JSON数据中的用户名和密码取出来
|
||||||
|
if data["username"] == "" || data["password"] == "" {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"message": "Invalid request",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
username := data["username"]
|
||||||
|
password := data["password"]
|
||||||
|
log.Printf("Login request for user: %s", username)
|
||||||
|
log.Printf("Password: %s", password)
|
||||||
|
//验证用户名和密码
|
||||||
|
if utils.ValiddateUser(username, password) {
|
||||||
|
token, err := utils.GenerateJWT(username)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"message": "Cannot get token, Internal server error",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
log.Println("Token generated for user:", username)
|
||||||
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"token": token,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
log.Printf("Invalid username or password for user: %s", username)
|
||||||
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
|
"message": "Invalid username or password",
|
||||||
|
})
|
||||||
|
}
|
||||||
18
testfb.log
Normal file
18
testfb.log
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
09:42:05 | 404 | 107.091µs | 127.0.0.1 | POST | / | Cannot POST /
|
||||||
|
09:43:05 | 404 | 9.871µs | 127.0.0.1 | POST | / | Cannot POST /
|
||||||
|
09:43:20 | 404 | 9.475µs | 127.0.0.1 | POST | / | Cannot POST /
|
||||||
|
09:43:33 | 404 | 9.491µs | 127.0.0.1 | POST | / | Cannot POST /
|
||||||
|
09:43:49 | 400 | 207.706µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:43:55 | 401 | 80.159µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:45:52 | 401 | 65.554µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:47:38 | 401 | 155.702µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:47:48 | 400 | 90.98µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:48:00 | 400 | 64.331µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:48:06 | 401 | 81.624µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:52:52 | 401 | 188.948µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:54:47 | 200 | 242.308µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:55:40 | 400 | 72.042µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:55:47 | 200 | 126.783µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:55:52 | 200 | 82.231µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:55:56 | 401 | 83.999µs | 127.0.0.1 | POST | /login | -
|
||||||
|
09:56:01 | 200 | 71.148µs | 127.0.0.1 | POST | /login | -
|
||||||
26
utils/jwt.go
Normal file
26
utils/jwt.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 密钥 (签发JWT)
|
||||||
|
var jwtSecret = []byte("mysecretkey")
|
||||||
|
|
||||||
|
// 模拟用户验证
|
||||||
|
func ValiddateUser(username, password string) bool {
|
||||||
|
return username == "admin" && password == "123456"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成JWT
|
||||||
|
func GenerateJWT(username string) (string, error) {
|
||||||
|
// 设置过期时间
|
||||||
|
claims := jwt.MapClaims{
|
||||||
|
"username": username,
|
||||||
|
"exp": time.Now().Add(time.Hour * 72).Unix(),
|
||||||
|
}
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
return token.SignedString(jwtSecret)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user