From 6f9c8aca96d791c4d75e4850c786bae1ee00ffc5 Mon Sep 17 00:00:00 2001 From: Garionion Date: Fri, 1 Apr 2022 19:43:29 +0200 Subject: [PATCH] change default json encoder, small change to region count --- go.mod | 3 +++ go.sum | 7 +++++++ main.go | 31 +++++++++++++++++-------------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index b167bdb..bdbe7f5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( code.gitea.io/sdk/gitea v0.15.1 github.com/gofiber/fiber/v2 v2.30.0 + github.com/json-iterator/go v1.1.12 github.com/kpango/glg v1.6.10 ) @@ -15,6 +16,8 @@ require ( github.com/hashicorp/go-version v1.4.0 // indirect github.com/klauspost/compress v1.15.1 // indirect github.com/kpango/fastime v1.1.4 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/stretchr/testify v1.7.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.34.0 // indirect diff --git a/go.sum b/go.sum index 5cb360e..2eaff1e 100644 --- a/go.sum +++ b/go.sum @@ -10,9 +10,12 @@ github.com/goccy/go-json v0.9.6 h1:5/4CtRQdtsX0sal8fdVhTaiMN01Ri8BExZZ8iRmHQ6E= github.com/goccy/go-json v0.9.6/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.30.0 h1:R928kgJICQkcfIzAjMIQ+U0uOpa0+vTCZLLODeo4M14= github.com/gofiber/fiber/v2 v2.30.0/go.mod h1:1Ega6O199a3Y7yDGuM9FyXDPYQfv+7/y48wl6WCwUF4= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4= github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.1 h1:y9FcTHGyrebwfP0ZZqFiaxTaiDnUrGkJkI+f583BL1A= github.com/klauspost/compress v1.15.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= @@ -20,6 +23,10 @@ github.com/kpango/fastime v1.1.4 h1:pus9JgJBg/8Jie3ozayA4yNIV67BUPhbq0wMZY3CtYo= github.com/kpango/fastime v1.1.4/go.mod h1:tTNDbIo5qL6D7g5vh2YbkyUbOVP2kD/we3rSjN22PMY= github.com/kpango/glg v1.6.10 h1:ykvO7lKmfYp9d3jBvIO7UFV5mqp2FYPezC0rAyN1IqA= github.com/kpango/glg v1.6.10/go.mod h1:Er7L/mFi0MY+Nse2a85sc3kU80cR3Vi3iBKax56r5ng= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= diff --git a/main.go b/main.go index 257b000..a7e18a2 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,13 @@ import ( "fmt" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/recover" + jsoniter "github.com/json-iterator/go" "github.com/kpango/glg" "sync" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Channel int const ( @@ -65,22 +68,24 @@ func getAttachementURL(attachement Attachement) (string, error) { } func (r *RegionCounter) countRegion(region string) { - glg.Infof("Region %s", region) - glg.Debugf("%+v", r.RegionCount) + glg.Debugf("Region %s", region) + r.Lock() + defer r.Unlock() if region == "" { return } - _, ok := r.RegionCount[region] + val, ok := r.RegionCount[region] if !ok { - glg.Info("New region") r.RegionCount[region] = 1 - glg.Debugf("%+v", r.RegionCount) return } - r.Lock() - defer r.Unlock() - r.RegionCount[region]++ - glg.Debugf("%+v", r.RegionCount) + r.RegionCount[region] = val + 1 +} + +func (r *RegionCounter) getRegionCount() map[string]int { + r.RLock() + defer r.RUnlock() + return r.RegionCount } func main() { @@ -92,6 +97,8 @@ func main() { GETOnly: true, ServerHeader: "Fiber", AppName: "Gitea Attachement Proxy", + JSONEncoder: jsoniter.Marshal, + JSONDecoder: jsoniter.Unmarshal, }) app.Use(recover.New()) @@ -160,11 +167,7 @@ func main() { }) app.Get("/regions", func(c *fiber.Ctx) error { - regionCounter.RLock() - defer regionCounter.RUnlock() - c.JSON(regionCounter.RegionCount) - glg.Debugf("%+v", regionCounter.RegionCount) - return nil + return c.JSON(regionCounter.getRegionCount()) }) app.Listen(":8080")