任务三

This commit is contained in:
Yvan
2026-01-07 19:10:02 +08:00
parent 3c0f3b7165
commit be3b6641a6
50 changed files with 2916 additions and 1167 deletions
+13
View File
@@ -120,3 +120,16 @@ func (j *JWT) CreateTokenByOldToken(oldToken string, oldClaims CustomClaims) (st
}
return newToken, &oldClaims, nil
}
// NewJWTFromConfig 从配置创建JWT实例(内部使用)
func NewJWTFromConfig(signingKey, issuer, expiresTime, bufferTime string) *JWT {
expires, _ := time.ParseDuration(expiresTime)
if expires == 0 {
expires = 7 * 24 * time.Hour // 默认7天
}
buffer, _ := time.ParseDuration(bufferTime)
if buffer == 0 {
buffer = 24 * time.Hour // 默认1天
}
return NewJWT(signingKey, issuer, expires, buffer)
}
+15
View File
@@ -0,0 +1,15 @@
package jwt
import (
"kra/internal/conf"
"github.com/google/wire"
)
// NewJWTFromConf 从conf.JWT配置创建JWT实例(用于wire注入)
func NewJWTFromConf(c *conf.JWT) *JWT {
return NewJWTFromConfig(c.SigningKey, c.Issuer, c.ExpiresTime, c.BufferTime)
}
// ProviderSet is jwt providers for wire.
var ProviderSet = wire.NewSet(NewJWTFromConf)
+47
View File
@@ -0,0 +1,47 @@
package response
import (
"encoding/json"
"net/http"
"github.com/go-kratos/kratos/v2/errors"
)
// Response 统一响应格式
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
// Encoder 响应编码器,包装响应为 {code, msg, data} 格式
func Encoder(w http.ResponseWriter, r *http.Request, v interface{}) error {
if v == nil {
v = struct{}{}
}
reply := Response{
Code: 0,
Msg: "success",
Data: v,
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(reply)
}
// ErrorEncoder 错误编码器,统一错误响应格式
func ErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
se := errors.FromError(err)
code := int(se.Code)
if code >= 400 && code < 500 {
code = 7
} else if code >= 500 {
code = 7
}
reply := Response{
Code: code,
Msg: se.Message,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(int(se.Code))
json.NewEncoder(w).Encode(reply)
}
+142
View File
@@ -0,0 +1,142 @@
package server
import (
"runtime"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
)
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
// Server 服务器信息
type Server struct {
Os Os `json:"os"`
Cpu Cpu `json:"cpu"`
Ram Ram `json:"ram"`
Disk []Disk `json:"disk"`
}
// Os 操作系统信息
type Os struct {
GOOS string `json:"goos"`
NumCPU int `json:"numCpu"`
Compiler string `json:"compiler"`
GoVersion string `json:"goVersion"`
NumGoroutine int `json:"numGoroutine"`
}
// Cpu CPU信息
type Cpu struct {
Cpus []float64 `json:"cpus"`
Cores int `json:"cores"`
}
// Ram 内存信息
type Ram struct {
UsedMB int `json:"usedMb"`
TotalMB int `json:"totalMb"`
UsedPercent int `json:"usedPercent"`
}
// Disk 磁盘信息
type Disk struct {
MountPoint string `json:"mountPoint"`
UsedMB int `json:"usedMb"`
UsedGB int `json:"usedGb"`
TotalMB int `json:"totalMb"`
TotalGB int `json:"totalGb"`
UsedPercent int `json:"usedPercent"`
}
// InitOS 获取操作系统信息
func InitOS() Os {
return Os{
GOOS: runtime.GOOS,
NumCPU: runtime.NumCPU(),
Compiler: runtime.Compiler,
GoVersion: runtime.Version(),
NumGoroutine: runtime.NumGoroutine(),
}
}
// InitCPU 获取CPU信息
func InitCPU() (Cpu, error) {
var c Cpu
cores, err := cpu.Counts(false)
if err != nil {
return c, err
}
c.Cores = cores
cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true)
if err != nil {
return c, err
}
c.Cpus = cpus
return c, nil
}
// InitRAM 获取内存信息
func InitRAM() (Ram, error) {
var r Ram
u, err := mem.VirtualMemory()
if err != nil {
return r, err
}
r.UsedMB = int(u.Used) / MB
r.TotalMB = int(u.Total) / MB
r.UsedPercent = int(u.UsedPercent)
return r, nil
}
// InitDisk 获取磁盘信息
func InitDisk(mountPoints []string) ([]Disk, error) {
var d []Disk
for _, mp := range mountPoints {
u, err := disk.Usage(mp)
if err != nil {
return d, err
}
d = append(d, Disk{
MountPoint: mp,
UsedMB: int(u.Used) / MB,
UsedGB: int(u.Used) / GB,
TotalMB: int(u.Total) / MB,
TotalGB: int(u.Total) / GB,
UsedPercent: int(u.UsedPercent),
})
}
return d, nil
}
// GetServerInfo 获取服务器完整信息
func GetServerInfo(diskMountPoints []string) (*Server, error) {
var s Server
var err error
s.Os = InitOS()
if s.Cpu, err = InitCPU(); err != nil {
return &s, err
}
if s.Ram, err = InitRAM(); err != nil {
return &s, err
}
if len(diskMountPoints) > 0 {
if s.Disk, err = InitDisk(diskMountPoints); err != nil {
return &s, err
}
}
return &s, nil
}