Compare commits

..

3 Commits

Author SHA1 Message Date
Yavolte fb585fabe6 use jwt claims 2025-06-30 11:25:55 +08:00
Yavolte 29d609ce0a fix cache bugs 2025-06-30 11:11:34 +08:00
Yavolte b0d6e3423d add gzip support 2025-06-24 15:51:18 +08:00
5 changed files with 159 additions and 8 deletions

View File

@ -57,9 +57,16 @@ func WithAllow(paths ...string) Option {
} }
} }
func WithClaims(claims reflect.Type) Option { func WithClaims(claims any) Option {
return func(o *options) { return func(o *options) {
o.claims = claims if tv, ok := claims.(reflect.Type); ok {
o.claims = tv
} else {
o.claims = reflect.TypeOf(claims)
if o.claims.Kind() == reflect.Ptr {
o.claims = o.claims.Elem()
}
}
} }
} }

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"time" "time"
"git.nobla.cn/golang/aeus"
) )
type redisCache struct { type redisCache struct {
@ -57,7 +59,12 @@ func (c *redisCache) String() string {
} }
func NewCache(opts ...Option) *redisCache { func NewCache(opts ...Option) *redisCache {
return &redisCache{ cache := &redisCache{
opts: newOptions(opts...), opts: newOptions(opts...),
} }
app := aeus.FromContext(cache.opts.context)
if app != nil {
cache.opts.prefix = app.Name() + ":" + cache.opts.prefix
}
return cache
} }

View File

@ -1,13 +1,16 @@
package redis package redis
import ( import (
"context"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
type ( type (
options struct { options struct {
client *redis.Client context context.Context
prefix string client *redis.Client
prefix string
} }
Option func(*options) Option func(*options)
@ -19,6 +22,12 @@ func WithClient(client *redis.Client) Option {
} }
} }
func WithContext(ctx context.Context) Option {
return func(o *options) {
o.context = ctx
}
}
func WithPrefix(prefix string) Option { func WithPrefix(prefix string) Option {
return func(o *options) { return func(o *options) {
o.prefix = prefix o.prefix = prefix

View File

@ -3,13 +3,17 @@ package http
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"net" "net"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath"
"slices"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -106,14 +110,61 @@ func (s *Server) Webroot(prefix string, fs http.FileSystem) {
s.fs.SetIndexFile("/index.html") s.fs.SetIndexFile("/index.html")
} }
func (s *Server) shouldCompress(req *http.Request) bool {
if !strings.Contains(req.Header.Get(headerAcceptEncoding), "gzip") ||
strings.Contains(req.Header.Get("Connection"), "Upgrade") {
return false
}
// Check if the request path is excluded from compression
extension := filepath.Ext(req.URL.Path)
if slices.Contains(assetsExtensions, extension) {
return true
}
return false
}
func (s *Server) staticHandle(ctx *gin.Context, fp http.File) {
uri := path.Clean(ctx.Request.URL.Path)
fi, err := fp.Stat()
if err != nil {
return
}
if !fi.IsDir() {
//https://github.com/gin-contrib/gzip
if s.shouldCompress(ctx.Request) && fi.Size() > 8192 {
gzWriter := newGzipWriter()
gzWriter.Reset(ctx.Writer)
ctx.Header(headerContentEncoding, "gzip")
ctx.Writer.Header().Add(headerVary, headerAcceptEncoding)
originalEtag := ctx.GetHeader("ETag")
if originalEtag != "" && !strings.HasPrefix(originalEtag, "W/") {
ctx.Header("ETag", "W/"+originalEtag)
}
ctx.Writer = &gzipWriter{ctx.Writer, gzWriter}
defer func() {
if ctx.Writer.Size() < 0 {
gzWriter.Reset(io.Discard)
}
gzWriter.Close()
if ctx.Writer.Size() > -1 {
ctx.Header("Content-Length", strconv.Itoa(ctx.Writer.Size()))
}
putGzipWriter(gzWriter)
}()
}
}
http.ServeContent(ctx.Writer, ctx.Request, path.Base(uri), s.fs.modtime, fp)
ctx.Abort()
return
}
func (s *Server) notFoundHandle(ctx *gin.Context) { func (s *Server) notFoundHandle(ctx *gin.Context) {
if s.fs != nil && ctx.Request.Method == http.MethodGet { if s.fs != nil && ctx.Request.Method == http.MethodGet {
uri := path.Clean(ctx.Request.URL.Path) uri := path.Clean(ctx.Request.URL.Path)
if fp, err := s.fs.Open(uri); err == nil { if fp, err := s.fs.Open(uri); err == nil {
http.ServeContent(ctx.Writer, ctx.Request, path.Base(uri), s.fs.modtime, fp) s.staticHandle(ctx, fp)
fp.Close() fp.Close()
ctx.Abort()
return
} }
} }
ctx.JSON(http.StatusNotFound, newResponse(errors.NotFound, "Not Found", nil)) ctx.JSON(http.StatusNotFound, newResponse(errors.NotFound, "Not Found", nil))

View File

@ -1,8 +1,14 @@
package http package http
import ( import (
"bufio"
"compress/gzip"
"context" "context"
"errors"
"io"
"net"
"net/http" "net/http"
"sync"
"git.nobla.cn/golang/aeus/pkg/logger" "git.nobla.cn/golang/aeus/pkg/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -41,6 +47,77 @@ type (
} }
) )
const (
headerAcceptEncoding = "Accept-Encoding"
headerContentEncoding = "Content-Encoding"
headerVary = "Vary"
)
var (
gzPool sync.Pool
assetsExtensions = []string{".css", ".js", ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".woff", ".woff2", ".ttf", ".eot", ".otf"}
)
type gzipWriter struct {
gin.ResponseWriter
writer *gzip.Writer
}
func (g *gzipWriter) WriteString(s string) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write([]byte(s))
}
func (g *gzipWriter) Write(data []byte) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write(data)
}
func (g *gzipWriter) Flush() {
_ = g.writer.Flush()
g.ResponseWriter.Flush()
}
// Fix: https://github.com/mholt/caddy/issues/38
func (g *gzipWriter) WriteHeader(code int) {
g.Header().Del("Content-Length")
g.ResponseWriter.WriteHeader(code)
}
var _ http.Hijacker = (*gzipWriter)(nil)
// Hijack allows the caller to take over the connection from the HTTP server.
// After a call to Hijack, the HTTP server library will not do anything else with the connection.
// It becomes the caller's responsibility to manage and close the connection.
//
// It returns the underlying net.Conn, a buffered reader/writer for the connection, and an error
// if the ResponseWriter does not support the Hijacker interface.
func (g *gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := g.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}
func newGzipWriter() (writer *gzip.Writer) {
v := gzPool.Get()
if v == nil {
writer, _ = gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
} else {
if w, ok := v.(*gzip.Writer); ok {
return w
} else {
writer, _ = gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
}
}
return
}
func putGzipWriter(writer *gzip.Writer) {
gzPool.Put(writer)
}
func WithNetwork(network string) Option { func WithNetwork(network string) Option {
return func(o *options) { return func(o *options) {
o.network = network o.network = network