Skip to content

Commit

Permalink
fix: [issue #9] static operator occur panic
Browse files Browse the repository at this point in the history
  • Loading branch information
tangx committed Dec 15, 2021
1 parent a010fad commit 1be6d62
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
8 changes: 3 additions & 5 deletions internal/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"log"
"time"

"github.com/go-jarvis/rum-gonic/internal/example/apis"
"github.com/go-jarvis/rum-gonic/internal/example/apis/index"
"github.com/go-jarvis/rum-gonic/internal/example/injector/redis"
"github.com/go-jarvis/rum-gonic/pkg/middlewares"

Expand All @@ -25,10 +23,10 @@ func main() {
// r.Register(auth.AdminUsers)

// 添加路由组 / 路由
r.Register(apis.RouterRoot)
r.Register(&index.Index{})
// r.Register(apis.RouterRoot)
// r.Register(&index.Index{})

r.Register(apis.RouterV0)
// r.Register(apis.RouterV0)

r.StaticFile("/user", "user.html")

Expand Down
36 changes: 28 additions & 8 deletions rum/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@ import (
"github.com/gin-gonic/gin"
)

func newStaticFile(method, path, filepath string) *StaticFile {
return &StaticFile{
method: method,
path: path,
filepath: filepath,
}
}

var _ OperatorDeepcopier = (*StaticFile)(nil)

type StaticFile struct {
method string
path string
filepath string
}

func (static *StaticFile) Deepcopy() Operator {
return &StaticFile{
method: static.method,
path: static.path,
filepath: static.filepath,
}
}

func (static *StaticFile) Path() string {
return static.path
}
Expand All @@ -29,14 +47,6 @@ func (static *StaticFile) Output(c *gin.Context) (interface{}, error) {
return nil, nil
}

func newStaticFile(method, path, filepath string) *StaticFile {
return &StaticFile{
method: method,
path: path,
filepath: filepath,
}
}

// StaticFile registers a single route in order to serve a single file of the local filesystem.
// router.StaticFile("favicon.ico", "./resources/favicon.ico")
func (r *RouterGroup) StaticFile(path, filepath string) {
Expand All @@ -47,12 +57,22 @@ func (r *RouterGroup) StaticFile(path, filepath string) {
}
}

var _ OperatorDeepcopier = (*StaticFS)(nil)

type StaticFS struct {
method string
path string
fs http.FileSystem
}

func (static *StaticFS) Deepcopy() Operator {
return &StaticFS{
method: static.method,
path: static.path,
fs: static.fs,
}
}

func (static *StaticFS) Path() string {
return static.path
}
Expand Down
4 changes: 4 additions & 0 deletions rum/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ type MethodOperator interface {

// DeepCopyOperator return a deepcopied operator
func DeepCopyOperator(op Operator) Operator {
if copier, ok := op.(OperatorDeepcopier); ok {
return copier.Deepcopy()
}

return NewOperatorFactory(op).New()
}
5 changes: 5 additions & 0 deletions rum/operator_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ type OperatorFactory struct {
Operator Operator
}

type OperatorDeepcopier interface {
Deepcopy() Operator
}

// New create a new operator
func (fact *OperatorFactory) New() Operator {

opc := reflect.New(fact.Type).Interface().(Operator)
return opc
}
1 change: 1 addition & 0 deletions userindex/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123123w
15 changes: 15 additions & 0 deletions userindex/user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<h3>Hello user</h3>
</body>

</html>

0 comments on commit 1be6d62

Please sign in to comment.