diff --git a/config.go b/config.go index 840f70a..bfa4293 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,8 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/taouniverse/tao" + "net/http" + "net/http/pprof" ) // ConfigKey for this repo @@ -59,9 +61,9 @@ var defaultGin = &Config{ RunAfters: []string{}, } -// Default config -func (g Config) Default() tao.Config { - return defaultGin +// Name of Config +func (g *Config) Name() string { + return ConfigKey } // ValidSelf with some default values @@ -130,3 +132,31 @@ func (g *Config) ToTask() tao.Task { func (g *Config) RunAfter() []string { return g.RunAfters } + +/** +middlewares +*/ + +// pprofOption for gin.Engine +func pprofOption(engin *gin.Engine, g *Config) { + pp := engin.Group(g.Pprof.Prefix) + + var pprofHandler = func(h http.HandlerFunc) gin.HandlerFunc { + return func(c *gin.Context) { + h.ServeHTTP(c.Writer, c.Request) + } + } + + pp.GET("/", pprofHandler(pprof.Index)) + pp.GET("/cmdline", pprofHandler(pprof.Cmdline)) + pp.GET("/profile", pprofHandler(pprof.Profile)) + pp.POST("/symbol", pprofHandler(pprof.Symbol)) + pp.GET("/symbol", pprofHandler(pprof.Symbol)) + pp.GET("/trace", pprofHandler(pprof.Trace)) + pp.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP)) + pp.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP)) + pp.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP)) + pp.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP)) + pp.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP)) + pp.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP)) +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..dff4a36 --- /dev/null +++ b/config_test.go @@ -0,0 +1,29 @@ +// Copyright 2022 huija +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gin + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestConfig(t *testing.T) { + g := new(Config) + g.ValidSelf() + assert.EqualValues(t, g, defaultGin) + + t.Log(g.RunAfter()) + t.Log(g.ToTask()) +} diff --git a/gin.go b/gin.go index cb9c10a..270b4d3 100644 --- a/gin.go +++ b/gin.go @@ -19,6 +19,20 @@ import ( "github.com/taouniverse/tao" ) +/** +import _ "github.com/taouniverse/tao-gin" +*/ + +// G config of gin +var G = new(Config) + +func init() { + err := tao.Register(ConfigKey, G, setup) + if err != nil { + panic(err.Error()) + } +} + // Engine of gin implements http.Handler var Engine *gin.Engine diff --git a/gin_test.go b/gin_test.go index fb816ee..48a3623 100644 --- a/gin_test.go +++ b/gin_test.go @@ -23,6 +23,9 @@ import ( ) func TestTao(t *testing.T) { + err := tao.SetConfigPath("./test.yaml") + assert.Nil(t, err) + group := Engine.Group("/") group.GET("/", func(context *gin.Context) { @@ -37,6 +40,6 @@ func TestTao(t *testing.T) { }) }) - err := tao.Run(nil, nil) + err = tao.Run(nil, nil) assert.Nil(t, err) } diff --git a/go.mod b/go.mod index 3b25464..983d954 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/gin-gonic/gin v1.7.7 github.com/stretchr/testify v1.7.0 - github.com/taouniverse/tao v0.1.2 + github.com/taouniverse/tao v0.1.5 ) require ( diff --git a/go.sum b/go.sum index d950b99..4f8372a 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/taouniverse/tao v0.1.2 h1:6Mb5arBm0wEd8X/mS6kE9FhAwwerWdvmY4JOGlyFiNA= -github.com/taouniverse/tao v0.1.2/go.mod h1:SBKOGf189LJQenjuqnp1Qsb6FYgoryRCjOIRtFuw3fU= +github.com/taouniverse/tao v0.1.4 h1:iNWHq7tL5aXoVW7ahFl+ddKB8uANYApwvFlxODig+es= +github.com/taouniverse/tao v0.1.4/go.mod h1:1YIQijX7HrVBicOsxJUcE/VzS1VLIq06UGzWzrTgyqg= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= diff --git a/init.go b/init.go deleted file mode 100644 index 3208bbd..0000000 --- a/init.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2021 -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gin - -import ( - "encoding/json" - "github.com/taouniverse/tao" -) - -/** -import _ "github.com/taouniverse/tao-gin" -*/ - -// G config of gin -var G = new(Config) - -func init() { - err := tao.Register(ConfigKey, func() error { - // 1. transfer config bytes to object - bytes, err := tao.GetConfigBytes(ConfigKey) - if err != nil { - G = G.Default().(*Config) - } else { - err = json.Unmarshal(bytes, &G) - if err != nil { - return err - } - } - - // gin config - G.ValidSelf() - - // 2. set object to tao - err = tao.SetConfig(ConfigKey, G) - if err != nil { - return err - } - - // gin setup - return setup() - }) - if err != nil { - panic(err.Error()) - } -} diff --git a/middlewares.go b/middlewares.go deleted file mode 100644 index ea58989..0000000 --- a/middlewares.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2022 huija -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gin - -import ( - "github.com/gin-gonic/gin" - "net/http" - "net/http/pprof" -) - -// pprofOption for gin.Engine -func pprofOption(engin *gin.Engine, g *Config) { - pp := engin.Group(g.Pprof.Prefix) - - var pprofHandler = func(h http.HandlerFunc) gin.HandlerFunc { - return func(c *gin.Context) { - h.ServeHTTP(c.Writer, c.Request) - } - } - - pp.GET("/", pprofHandler(pprof.Index)) - pp.GET("/cmdline", pprofHandler(pprof.Cmdline)) - pp.GET("/profile", pprofHandler(pprof.Profile)) - pp.POST("/symbol", pprofHandler(pprof.Symbol)) - pp.GET("/symbol", pprofHandler(pprof.Symbol)) - pp.GET("/trace", pprofHandler(pprof.Trace)) - pp.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP)) - pp.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP)) - pp.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP)) - pp.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP)) - pp.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP)) - pp.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP)) -} diff --git a/conf/config.yaml b/test.yaml similarity index 100% rename from conf/config.yaml rename to test.yaml