Beego: Open second port for admin APIs

Created on 7 Aug 2013  ·  7Comments  ·  Source: astaxie/beego

In our Java app, we use Dropwizard.
It has a great feature where it opens up 2 separate HTTP ports:

  • one for public APIs
  • one for admin APIs (closed off the firewall, so no one from outside can reach it).

On the admin port it has things such as:

a) health check API for integration with an operations monitoring system like Nagios:

http://dropwizard.codahale.com/manual/core/#health-checks

b) admin tasks (e.g. run garbage collection, refresh in-memory caches, run DB migration, etc):

http://dropwizard.codahale.com/manual/core/#tasks

It would be great if beego also supported opening up 2 separate ports by default in a similar fashion and offered out of the box support for admin APIs.

Also, check out Dropwizard's use of YAML for configuration, I think it is much nicer than the .ini format. You can see in there the options for public and admin ports:

http://dropwizard.codahale.com/manual/core/#configuration-defaults

Most helpful comment

All 7 comments

these will make the framework more complex. I want this framework much simpleness.

but the admin port is a good idea. I will add this feature in next version for modules. like monitoring,tasks.

now beego's admin already support admin port which is 8088,you can open http://localhost:8088 to manager the application's info.

  • Now admin module support QPS statistics like this:
| requestUrl                                        | method     | times            | used             | max used         | min used         | avg used         |
| /api/user                                         | POST       |  2               | 122.00us         | 120.00us         | 2.00us           | 61.00us          |
| /api/user                                         | GET        |  1               | 13.00us          | 13.00us          | 13.00us          | 13.00us          |
| /api/user                                         | DELETE     |  1               | 1.40us           | 1.40us           | 1.40us           | 1.40us           |
| /api/admin                                        | POST       |  1               | 14.00us          | 14.00us          | 14.00us          | 14.00us          |
| /api/user/astaxie                                 | POST       |  1               | 12.00us          | 12.00us          | 12.00us          | 12.00us          |
| /api/user/xiemengjun                              | POST       |  1               | 13.00us          | 13.00us          | 13.00us          | 13.00us          |
  • profile throw the http,you can get the goroutine info,mem,cpu,gc and so on:
goroutine 4 [running]:
runtime/pprof.writeGoroutineStacks(0xc2000a5420, 0xc200000008, 0x1, 0x860000c2000a6c40)
    /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:511 +0x7a
runtime/pprof.writeGoroutine(0xc2000a5420, 0xc200000008, 0x2, 0x8687298b92b07716, 0xa957, ...)
    /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:500 +0x3a
runtime/pprof.(*Profile).WriteTo(0x3b7540, 0xc2000a5420, 0xc200000008, 0x2, 0x1, ...)
    /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:229 +0xb2
_/Users/astaxie/github/beego/admin.ProcessInput(0x28a430, 0x10, 0xc2000a5420, 0xc200000008)
    /Users/astaxie/github/beego/admin/profile.go:26 +0x254
_/Users/astaxie/github/beego/admin.TestProcessInput(0xc2000bd000)
    /Users/astaxie/github/beego/admin/profile_test.go:9 +0x58
testing.tRunner(0xc2000bd000, 0x3b7500)
    /Users/astaxie/go/src/pkg/testing/testing.go:353 +0x8a
created by testing.RunTests
    /Users/astaxie/go/src/pkg/testing/testing.go:433 +0x86b

goroutine 1 [chan receive]:
testing.RunTests(0x2bb7f8, 0x3b7500, 0x2, 0x2, 0x1, ...)
    /Users/astaxie/go/src/pkg/testing/testing.go:434 +0x88e
testing.Main(0x2bb7f8, 0x3b7500, 0x2, 0x2, 0x3bf780, ...)
    /Users/astaxie/go/src/pkg/testing/testing.go:365 +0x8a
main.main()
    _/Users/astaxie/github/beego/admin/_test/_testmain.go:45 +0x9a

goroutine 2 [syscall]:
heap profile: 1: 1048576 [1: 1048576] @ heap/1048576
1: 1048576 [1: 1048576] @ 0x1f79a 0x20c3e 0x20d08 0x1c815 0x1c6ea 0x95646 0x955aa 0x925b2 0x346b4 0x36d38 0x306aa 0x18cd0
#   0x20c3e cnew+0xee                           /Users/astaxie/go/src/pkg/runtime/zmalloc_darwin_amd64.c:655
#   0x20d08 runtime.cnewarray+0x38                      /Users/astaxie/go/src/pkg/runtime/zmalloc_darwin_amd64.c:673
#   0x1c815 makeslice1+0x45                         /Users/astaxie/go/src/pkg/runtime/slice.c:53
#   0x1c6ea runtime.makeslice+0x9a                      /Users/astaxie/go/src/pkg/runtime/slice.c:34
#   0x95646 runtime/pprof.writeGoroutineStacks+0x36             /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:509
#   0x955aa runtime/pprof.writeGoroutine+0x3a               /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:500
#   0x925b2 runtime/pprof.(*Profile).WriteTo+0xb2               /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:229
#   0x346b4 _/Users/astaxie/github/beego/admin.ProcessInput+0x254       /Users/astaxie/github/beego/admin/profile.go:26
#   0x36d38 _/Users/astaxie/github/beego/admin.TestProcessInput+0x58    /Users/astaxie/github/beego/admin/profile_test.go:9
#   0x306aa testing.tRunner+0x8a                        /Users/astaxie/go/src/pkg/testing/testing.go:353


# runtime.MemStats
# Alloc = 1804104
# TotalAlloc = 1830992
# Sys = 273791144
# Lookups = 40
# Mallocs = 579
# Frees = 73
# HeapAlloc = 1804104
# HeapSys = 2097152
# HeapIdle = 122880
# HeapInuse = 1974272
# HeapReleased = 0
# HeapObjects = 506
# Stack = 16384 / 131072
# MSpan = 5200 / 131072
# MCache = 2024 / 131072
# BuckHashSys = 1439992
# NextGC = 3545600
# PauseNs = [178998 166068 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
# NumGC = 2
# EnableGC = true
# DebugGC = false
threadcreate profile: total 4
1 @ 0x177a3 0x17b35 0x17e13 0x18037 0x19216 0x133c3 0x18cd0
#   0x17b35 newm+0x25           /Users/astaxie/go/src/pkg/runtime/proc.c:759
#   0x17e13 startm+0xb3         /Users/astaxie/go/src/pkg/runtime/proc.c:837
#   0x18037 handoffp+0x1a7          /Users/astaxie/go/src/pkg/runtime/proc.c:855
#   0x19216 runtime.entersyscallblock+0x136 /Users/astaxie/go/src/pkg/runtime/proc.c:1331
#   0x133c3 runtime.MHeap_Scavenger+0xd3    /Users/astaxie/go/src/pkg/runtime/mheap.c:453

1 @ 0x177a3 0x17b35 0x16c9e 0x18cd0
#   0x17b35 newm+0x25       /Users/astaxie/go/src/pkg/runtime/proc.c:759
#   0x16c9e runtime.main+0x2e   /Users/astaxie/go/src/pkg/runtime/proc.c:167

1 @ 0x177a3 0x178f4 0x17670 0x2475d
#   0x2475d _rt0_amd64+0x10d    /Users/astaxie/go/src/pkg/runtime/asm_amd64.s:91

1 @ 0x16bd2 0x2474a
#   0x2474a _rt0_amd64+0xfa /Users/astaxie/go/src/pkg/runtime/asm_amd64.s:81
  • support healthcheck you can writer the check:
type DatabaseCheck struct {
}

func (dc *DatabaseCheck) Check() error {
    if dc.isConnected() {
        return nil
    } else {
        return errors.New("can't connect database")
    }
}

admin.AddHealthCheck("database",&DatabaseCheck{})

throw the web browse http://localhost:8088/healthcheck you can get the info

* database: OK
  • support task like crontab now still in develop

good job!

很好!

excellent

excellent

Was this page helpful?
0 / 5 - 0 ratings