Skip to content

Commit

Permalink
update tao to v0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
huija committed Sep 22, 2022
1 parent 76cbebb commit 085a682
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 109 deletions.
36 changes: 33 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/taouniverse/tao"
"net/http"
"net/http/pprof"
)

// ConfigKey for this repo
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
29 changes: 29 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
14 changes: 14 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -37,6 +40,6 @@ func TestTao(t *testing.T) {
})
})

err := tao.Run(nil, nil)
err = tao.Run(nil, nil)
assert.Nil(t, err)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
57 changes: 0 additions & 57 deletions init.go

This file was deleted.

45 changes: 0 additions & 45 deletions middlewares.go

This file was deleted.

File renamed without changes.

0 comments on commit 085a682

Please sign in to comment.