Skip to content

Commit

Permalink
add static
Browse files Browse the repository at this point in the history
  • Loading branch information
gorilla-go committed Jan 29, 2024
1 parent 6464fc6 commit 5f43dbd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k *Kernel) Handle(w http.ResponseWriter, req *http.Request) {
}
}

if cusMiddleware != nil && len(cusMiddleware) > 0 {
if cusMiddleware != nil {
k.middleware = cusMiddleware
}

Expand Down
55 changes: 53 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package pig
import (
"fmt"
"github.com/gorilla-go/pig/foundation"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
Expand All @@ -13,6 +16,7 @@ type Router struct {
regRouteMap *foundation.LinkedHashMap[string, *foundation.LinkedHashMap[string, func(*Context)]]
missRoute func(*Context)
middlewareMap map[string][]IMiddleware
static map[string]string
}

type RouterParams foundation.ReqParams
Expand All @@ -26,6 +30,7 @@ func NewRouter() *Router {
M: make(map[string]*foundation.LinkedHashMap[string, func(*Context)]),
},
middlewareMap: make(map[string][]IMiddleware),
static: make(map[string]string),
}
}

Expand Down Expand Up @@ -70,6 +75,14 @@ func (r *Router) ReqUniPath(path, method string) string {
return fmt.Sprintf("%s://%s", method, path)
}

func (r *Router) Static(path string, realPath string) {
if r.group != "" {
panic("group router nonsupport static files.")
}

r.static[path] = realPath
}

func (r *Router) GET(path string, f func(*Context), middleware ...IMiddleware) {
r.addRoute("GET", path, f, middleware)
}
Expand Down Expand Up @@ -145,6 +158,39 @@ func (r *Router) Route(path string, requestMethod string) (func(*Context), Route
routerParams := make(RouterParams)
middlewares := make([]IMiddleware, 0)

if requestMethod == "GET" && len(r.static) > 0 {
for uriPrefix, realPath := range r.static {
if strings.HasPrefix(path, uriPrefix) {
file := fmt.Sprintf(
"%s/%s",
filepath.Dir(realPath),
strings.TrimPrefix(path, uriPrefix),
)
_, err := os.Stat(file)
if err == nil {
return func(context *Context) {
fi := NewFile(file)
f, err := os.Open(fi.FilePath)
if err != nil {
panic(err)
}
defer func() {
err := f.Close()
if err != nil {
panic(err)
}
}()
context.Response().Raw().Header().Set("Content-Type", fi.ContentType)
_, err = io.Copy(context.Response().Raw(), f)
if err != nil {
panic(err)
}
}, routerParams, middlewares
}
}
}
}

r.regRouteMap.ForEach(func(uri string, methodMap *foundation.LinkedHashMap[string, func(*Context)]) bool {
originUri := uri
uri = strings.Trim(uri, "/")
Expand Down Expand Up @@ -232,7 +278,12 @@ func (r *Router) Route(path string, requestMethod string) (func(*Context), Route
})

if r.missRoute != nil && fn == nil {
return r.missRoute, nil, middlewares
return r.missRoute, nil, nil
}
return fn, routerParams, middlewares

if len(middlewares) > 0 {
return fn, routerParams, middlewares
}

return fn, routerParams, nil
}
25 changes: 16 additions & 9 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@ package main
import (
"fmt"
"github.com/gorilla-go/pig"
"github.com/gorilla-go/pig/di"
"os"
)

type HttpErrorHandler struct {
type m struct {
}

func (h *HttpErrorHandler) Handle(a any, context *pig.Context) {
fmt.Println(a)
func (m *m) Handle(context *pig.Context, f func(*pig.Context)) {
fmt.Println("ok2")
f(context)
}

type ErrorCollection struct {
type m2 struct {
}

func (m *ErrorCollection) Handle(context *pig.Context, f func(*pig.Context)) {
di.ProvideValue[pig.IHttpErrorHandler](context.Container(), &HttpErrorHandler{})
func (m *m2) Handle(context *pig.Context, f func(*pig.Context)) {
fmt.Println("ok3")
f(context)
}

func main() {
r := pig.NewRouter()
r.GET("/", func(context *pig.Context) {
panic("target error")
})
}, &m2{})

getwd, err := os.Getwd()
if err != nil {
return
}
r.Static("/static/", getwd+"/test/")

pig.New().Use(&ErrorCollection{}).Router(r).Run(8081)
pig.New().Use(&m{}).Router(r).Run(8081)
}

0 comments on commit 5f43dbd

Please sign in to comment.