Skip to content

Commit

Permalink
Merge pull request #77 from RealFax/dev
Browse files Browse the repository at this point in the history
merge. add jetbrains logo to docs
  • Loading branch information
PotatoCloud authored Apr 5, 2024
2 parents d9aff9e + a929a96 commit c9e74df
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ admin = "123456"
- google uuid [(BSD-3-Clause License)](https://github.com/google/uuid/blob/master/LICENSE)
- grpc [(Apache License 2.0)](https://github.com/grpc/grpc-go/blob/master/LICENSE)
- protobuf [(BSD-3-Clause License)](https://github.com/protocolbuffers/protobuf-go/blob/master/LICENSE)

## Acknowledgments

<a href="https://jb.gg/OpenSourceSupport"><img width="100px" src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png"/></a>
4 changes: 4 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ admin = "123456"
- google uuid [(BSD-3-Clause License)](https://github.com/google/uuid/blob/master/LICENSE)
- grpc [(Apache License 2.0)](https://github.com/grpc/grpc-go/blob/master/LICENSE)
- protobuf [(BSD-3-Clause License)](https://github.com/protocolbuffers/protobuf-go/blob/master/LICENSE)

## 致谢

<a href="https://jb.gg/OpenSourceSupport"><img width="100px" src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png"/></a>
8 changes: 6 additions & 2 deletions internal/rqd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/x509/pkix"
"github.com/RealFax/RedQueen/internal/rqd/config"
"github.com/RealFax/RedQueen/internal/rqd/store"
"github.com/RealFax/RedQueen/internal/version"
"github.com/RealFax/RedQueen/pkg/dlocker"
"github.com/RealFax/RedQueen/pkg/expr"
"github.com/RealFax/RedQueen/pkg/grpcutil"
Expand Down Expand Up @@ -173,7 +174,7 @@ func (s *Server) registerHttpServer() {
router.NotFound = httputil.WrapE(func(w http.ResponseWriter, r *http.Request) error {
return httputil.NewStatus(http.StatusNotFound, 0, "Not Found")
})
router.PanicHandler = func(w http.ResponseWriter, _ *http.Request, _ interface{}) {
router.PanicHandler = func(w http.ResponseWriter, _ *http.Request, _ any) {
httputil.Any(http.StatusServiceUnavailable, 0).Message("Service Unavailable").Ok(w)
}

Expand All @@ -190,7 +191,10 @@ func (s *Server) registerHttpServer() {
router.Handler(http.MethodGet, "/action/:bucket/scan", httputil.WrapE(httpHandlers.PrefixScan))
router.Handler(http.MethodPut, "/action/:bucket/try", httputil.WrapE(httpHandlers.TrySet))

s.httpServer.Handler = router
s.httpServer.Handler = httputil.UseMiddleware(router, func(w http.ResponseWriter, r *http.Request) bool {
w.Header().Add("Server", version.String())
return true
})

if s.cfg.BasicAuth != nil && len(s.cfg.BasicAuth) != 0 {
// use basic-auth
Expand Down
18 changes: 18 additions & 0 deletions pkg/httputil/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ func WrapE(fc func(w http.ResponseWriter, r *http.Request) error) http.HandlerFu
}
}

type Middleware struct {
handler http.Handler
fc func(w http.ResponseWriter, r *http.Request) bool
}

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if m.fc(w, r) {
m.handler.ServeHTTP(w, r)
}
}

func UseMiddleware(next http.Handler, fc func(w http.ResponseWriter, r *http.Request) bool) http.Handler {
return &Middleware{
handler: next,
fc: fc,
}
}

type BasicAuthFunc func(username, password string) bool
type basicAuth struct {
next http.Handler
Expand Down

0 comments on commit c9e74df

Please sign in to comment.