Skip to content

Commit

Permalink
refactor: use fiber drop-in replace gin
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Sep 13, 2023
1 parent 07fcfc4 commit 4b032da
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 264 deletions.
5 changes: 3 additions & 2 deletions cache/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ var Collection *cache.Cache
// DataFilePath 是缓存记录文件地址
var DataFilePath = filepath.Join(util.MustGetExecDir(), "cache.data")

// init 用于初始化缓存驱动
func init() {
// LoadFromDisk 用于初始化缓存驱动
func LoadFromDisk() {
defer zap.L().Sync()

Collection = cache.New(5*time.Minute, 10*time.Minute)
zap.L().Debug("[cache] 加载缓存文件...")
if err := Collection.LoadFile(DataFilePath); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions consts/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package consts

// ContextKey is a type of context key
type ContextKey string

const (
// ContextKeyRequestID is a context key for request id
ContextKeyRequestID ContextKey = "request_id"
)
6 changes: 3 additions & 3 deletions controllers/v1/ping.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package v1

import (
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
"github.com/hitokoto-osc/Moe/util/web"
)

// Ping is a controller func that impl a Pong response,
// intended to notify the client that server is ok
func Ping(c *gin.Context) {
web.Success(c, map[string]interface{}{})
func Ping(c *fiber.Ctx) error {
return web.Success(c, map[string]interface{}{})
}
9 changes: 4 additions & 5 deletions controllers/v1/statistic.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package v1

import (
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
"github.com/hitokoto-osc/Moe/cache"
"github.com/hitokoto-osc/Moe/task/status/types"
"github.com/hitokoto-osc/Moe/util/web"
)

// Statistic 是用于返回统计分析结果的控制器
func Statistic(c *gin.Context) {
func Statistic(c *fiber.Ctx) error {
data, ok := cache.GetStatusData()
if !ok {
web.Fail(c, map[string]interface{}{}, -1)
return
return web.Fail(c, map[string]interface{}{}, -1)
}
if data.DownServer == nil {
data.DownServer = make([]types.DownServerData, 0)
}
web.Success(c, data)
return web.Success(c, data)
}
11 changes: 5 additions & 6 deletions flag/do.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package flag

import (
pflag "github.com/spf13/pflag"
)
import "github.com/spf13/pflag"

// Parse params
func Parse() {
func init() {
// Register Flag mapping
registerVersionFlag()
registerHelpFlag()
Expand All @@ -14,8 +11,10 @@ func Parse() {

// Parse Flag
pflag.Parse()
}

// Handle Flag event
// Do is a func will be called at init, registering the drivers of program
func Do() {
handleVersionFlag()
handleHelpFlag()
}
36 changes: 14 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.21

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/bytedance/sonic v1.10.1
github.com/cockroachdb/errors v1.11.1
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/requestid v0.0.6
github.com/gin-contrib/sessions v0.0.5
github.com/gin-gonic/gin v1.9.1
github.com/go-resty/resty/v2 v2.7.0
github.com/go-sql-driver/mysql v1.7.1
github.com/gofiber/contrib/fiberzap/v2 v2.1.0
github.com/gofiber/fiber/v2 v2.49.1
github.com/google/uuid v1.3.1
github.com/jmoiron/sqlx v1.3.5
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/spf13/pflag v1.0.5
Expand All @@ -20,56 +20,48 @@ require (
)

require (
github.com/gorilla/sessions v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/net v0.15.0 // indirect
)

require (
github.com/bytedance/sonic v1.10.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/getsentry/sentry-go v0.24.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.49.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 4b032da

Please sign in to comment.