Skip to content

Commit

Permalink
refactor: adjust attr of elton, chagen less attr to be private
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 24, 2020
1 parent 54fda90 commit cc1597e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
55 changes: 33 additions & 22 deletions elton.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,8 @@ type (
}
// Elton web framework instance
Elton struct {
// status of elton
status int32
tree *node
// Server http server
Server *http.Server
// Routers all router infos
Routers []*RouterInfo
// Middlewares middleware function
Middlewares []Handler
// PreMiddlewares pre middleware function
PreMiddlewares []PreHandler
errorListeners []ErrorListener
traceListeners []TraceListener
// ErrorHandler set the function for error handler
ErrorHandler ErrorHandler
// NotFoundHandler set the function for not found handler
Expand All @@ -83,6 +72,19 @@ type (
EnableTrace bool
// SignedKeys signed keys
SignedKeys SignedKeysGenerator

// status of elton
status int32
// route tree
tree *node
// routers all router infos
routers []*RouterInfo
// middlewares middleware function
middlewares []Handler
// preMiddlewares pre middleware function
preMiddlewares []PreHandler
errorListeners []ErrorListener
traceListeners []TraceListener
// functionInfos the function address:name map
functionInfos map[uintptr]string
ctxPool sync.Pool
Expand Down Expand Up @@ -231,7 +233,7 @@ func (e *Elton) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
}
return
}
for _, preHandler := range e.PreMiddlewares {
for _, preHandler := range e.preMiddlewares {
preHandler(req)
}

Expand Down Expand Up @@ -263,23 +265,32 @@ func (e *Elton) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
}
}

// GetRouters get routers
func (e *Elton) GetRouters() []RouterInfo {
routers := make([]RouterInfo, len(e.routers))
for index, r := range e.routers {
routers[index] = *r
}
return routers
}

// Handle add http handle function
func (e *Elton) Handle(method, path string, handlerList ...Handler) *Elton {
for _, fn := range handlerList {
name := e.GetFunctionName(fn)
e.SetFunctionName(fn, name)
}

if e.Routers == nil {
e.Routers = make([]*RouterInfo, 0)
if e.routers == nil {
e.routers = make([]*RouterInfo, 0)
}
e.Routers = append(e.Routers, &RouterInfo{
e.routers = append(e.routers, &RouterInfo{
Method: method,
Path: path,
})
e.tree.InsertRoute(methodMap[method], path, func(c *Context) {
c.Route = path
mids := e.Middlewares
mids := e.middlewares
maxMid := len(mids)
maxNext := maxMid + len(handlerList)
index := -1
Expand Down Expand Up @@ -421,14 +432,14 @@ func (e *Elton) ALL(path string, handlerList ...Handler) *Elton {

// Use add middleware function handle
func (e *Elton) Use(handlerList ...Handler) *Elton {
if e.Middlewares == nil {
e.Middlewares = make([]Handler, 0)
if e.middlewares == nil {
e.middlewares = make([]Handler, 0)
}
for _, fn := range handlerList {
name := e.GetFunctionName(fn)
e.SetFunctionName(fn, name)
}
e.Middlewares = append(e.Middlewares, handlerList...)
e.middlewares = append(e.middlewares, handlerList...)
return e
}

Expand All @@ -440,10 +451,10 @@ func (e *Elton) UseWithName(handler Handler, name string) *Elton {

// Pre add pre middleware function handler
func (e *Elton) Pre(handlerList ...PreHandler) *Elton {
if e.PreMiddlewares == nil {
e.PreMiddlewares = make([]PreHandler, 0)
if e.preMiddlewares == nil {
e.preMiddlewares = make([]PreHandler, 0)
}
e.PreMiddlewares = append(e.PreMiddlewares, handlerList...)
e.preMiddlewares = append(e.preMiddlewares, handlerList...)
return e
}

Expand Down
6 changes: 3 additions & 3 deletions elton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ func TestHandle(t *testing.T) {
e.TRACE(path)
allMethods := "/all-methods"
e.ALL(allMethods)
for index, r := range e.Routers {
for index, r := range e.GetRouters() {
p := path
if index >= len(methods) {
p = allMethods
}
assert.Equal(p, r.Path)
}
assert.Equal(2*len(methods), len(e.Routers), "method handle add fail")
assert.Equal(2*len(methods), len(e.GetRouters()), "method handle add fail")
})
t.Run("group", func(t *testing.T) {
assert := assert.New(t)
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestHandle(t *testing.T) {

t.Run("get routers", func(t *testing.T) {
assert := assert.New(t)
assert.Equal(34, len(e.Routers), "router count fail")
assert.Equal(34, len(e.GetRouters()), "router count fail")
})

t.Run("response body reader", func(t *testing.T) {
Expand Down

0 comments on commit cc1597e

Please sign in to comment.