Skip to content

Commit

Permalink
add Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Jul 30, 2018
1 parent 4f684e0 commit 003ec77
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
40 changes: 40 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,46 @@ func New() *App {
return &App{}
}

// Clone clones app
func (app *App) Clone() *App {
x := &App{
Addr: app.Addr,
ReadTimeout: app.ReadTimeout,
ReadHeaderTimeout: app.ReadHeaderTimeout,
WriteTimeout: app.WriteTimeout,
IdleTimeout: app.IdleTimeout,
MaxHeaderBytes: app.MaxHeaderBytes,
TLSNextProto: cloneTLSNextProto(app.TLSNextProto),
ConnState: app.ConnState,
ErrorLog: app.ErrorLog,
handler: app.handler,
routes: cloneRoutes(app.routes),
globals: cloneGlobals(app.globals),
beforeRender: app.beforeRender,
template: cloneTmpl(app.template),
templateFuncs: cloneFuncMaps(app.templateFuncs),
gracefulShutdown: &*app.gracefulShutdown,
certFile: app.certFile,
keyFile: app.keyFile,
}
if app.TLSConfig != nil {
x.TLSConfig = app.TLSConfig.Clone()
}
return x
}

func cloneTLSNextProto(xs map[string]func(*http.Server, *tls.Conn, http.Handler)) map[string]func(*http.Server, *tls.Conn, http.Handler) {
if xs == nil {
return nil
}

rs := make(map[string]func(*http.Server, *tls.Conn, http.Handler))
for k, v := range xs {
rs[k] = v
}
return rs
}

// Address sets server address
func (app *App) Address(addr string) *App {
app.Addr = addr
Expand Down
14 changes: 14 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package hime

// Globals is the global const map
type Globals map[interface{}]interface{}

func cloneGlobals(xs Globals) Globals {
if xs == nil {
return nil
}
rs := make(Globals)
for k, v := range xs {
rs[k] = v
}
return rs
}

// Globals registers global constants
func (app *App) Globals(globals Globals) *App {
if app.globals == nil {
Expand Down
6 changes: 0 additions & 6 deletions hime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import (
"net/http"
)

// Routes is the map for route name => path
type Routes map[string]string

// Globals is the global const map
type Globals map[interface{}]interface{}

// Handler is the hime handler
type Handler func(*Context) Result

Expand Down
14 changes: 14 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package hime

// Routes is the map for route name => path
type Routes map[string]string

func cloneRoutes(xs Routes) Routes {
if xs == nil {
return nil
}
rs := make(Routes)
for k, v := range xs {
rs[k] = v
}
return rs
}

// Routes registers route name and path
func (app *App) Routes(routes Routes) *App {
if app.routes == nil {
Expand Down
24 changes: 24 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,27 @@ func joinTemplateDir(dir string, filenames ...string) []string {
}
return xs
}

func cloneFuncMaps(xs []template.FuncMap) []template.FuncMap {
if xs == nil {
return nil
}

rs := make([]template.FuncMap, len(xs))
for i := range xs {
rs[i] = xs[i]
}
return rs
}

func cloneTmpl(xs map[string]*tmpl) map[string]*tmpl {
if xs == nil {
return nil
}

rs := make(map[string]*tmpl)
for k, v := range xs {
rs[k] = v
}
return rs
}

0 comments on commit 003ec77

Please sign in to comment.