Skip to content

Commit

Permalink
add request counter
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Apr 16, 2020
1 parent 26f16a5 commit 23d80ae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ysmood/digto
go 1.14

require (
github.com/gin-gonic/gin v1.6.0
github.com/gin-gonic/gin v1.6.2
github.com/go-acme/lego/v3 v3.5.0
github.com/stretchr/testify v1.5.1
github.com/ysmood/ddns v0.2.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/gin-gonic/gin v1.6.0 h1:Lb3veSYoGaNck69fV2+Vf2juLSsHpMTf3Vk5+X+EDJg=
github.com/gin-gonic/gin v1.6.0/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.6.2 h1:88crIK23zO6TqlQBt+f9FrPJNKm9ZEr7qjp9vl/d5TM=
github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-acme/lego/v3 v3.5.0 h1:/0+NJQK+hNwRznhCi+19lbEa4xufhe7wJZOVd5j486s=
github.com/go-acme/lego/v3 v3.5.0/go.mod h1:TXodhTGOiWEqXDdgrzBoCtJ5R4L9lfOE68CTM0KGkT0=
github.com/go-cmd/cmd v1.0.5/go.mod h1:y8q8qlK5wQibcw63djSl/ntiHUHXHGdCkPk0j4QeW4s=
Expand Down
39 changes: 38 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Context struct {
httpsListener net.Listener
timeout time.Duration
proxy *proxy
store *storer.Store
reqCounter *storer.Value

onError func(error)
}
Expand Down Expand Up @@ -54,6 +56,8 @@ func New(dbPath, dnsProvider, dnsConfig, host, caDirURL, httpAddr, httpsAddr str

gin.SetMode(gin.ReleaseMode)

reqCount := 0

return &Context{
host: host,
cert: cert,
Expand All @@ -62,6 +66,8 @@ func New(dbPath, dnsProvider, dnsConfig, host, caDirURL, httpAddr, httpsAddr str
httpsListener: httpsListener,
timeout: timeout,
proxy: newProxy(host),
store: store,
reqCounter: store.Value("reqCount", &reqCount),
onError: func(err error) {
log.Println(err)
},
Expand All @@ -79,7 +85,14 @@ func (ctx *Context) GetServer() *kit.ServerContext {
// Serve ...
func (ctx *Context) Serve() error {
ctx.engine.GET("/", ctx.homePage)
ctx.engine.NoRoute(ctx.proxy.handler)
ctx.engine.NoRoute(func(g *gin.Context) {
err := ctx.count()
if err != nil {
kit.Err(err)
}

ctx.proxy.handler(g)
})

go ctx.proxy.eventLoop()

Expand Down Expand Up @@ -117,6 +130,19 @@ func (ctx *Context) Serve() error {
return tlsSrv.ServeTLS(ctx.httpsListener, "", "")
}

func (ctx *Context) count() error {
return ctx.store.Update(func(txn storer.Txn) error {
var v int
t := ctx.reqCounter.Txn(txn)
err := t.Get(&v)
if err != nil {
return err
}
v++
return t.Set(&v)
})
}

func (ctx *Context) homePage(ginCtx kit.GinContext) {
if ginCtx.Request.Host != ctx.host || ginCtx.Request.URL.Path != "/" {
ctx.proxy.handler(ginCtx)
Expand All @@ -125,14 +151,25 @@ func (ctx *Context) homePage(ginCtx kit.GinContext) {

proxyStatus, _ := json.MarshalIndent(ctx.proxy.status, "", " ")

var count int
err := ctx.reqCounter.Get(&count)
if err != nil {
kit.Err(err)
}

params := []interface{}{
"version", Version,
"count", count,
"proxyStatus", string(proxyStatus),
}

ginCtx.String(http.StatusOK, kit.S(`
# Digto {{.version}}
## Request Count
{{.count}}
## Proxy Status
{{.proxyStatus}}
Expand Down
2 changes: 1 addition & 1 deletion server/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server

// Version ...
const Version = "v1.6.0"
const Version = "v1.6.1"

0 comments on commit 23d80ae

Please sign in to comment.