Added css minify

This commit is contained in:
2026-06-13 04:00:52 +08:00
parent 55541a5930
commit 803bd80557
10 changed files with 259 additions and 142 deletions
+45 -24
View File
@@ -8,7 +8,9 @@ import (
"time"
"github.com/tgckpg/resolver-go/internal/closure"
"github.com/tgckpg/resolver-go/internal/compilecache"
"github.com/tgckpg/resolver-go/internal/generated"
"github.com/tgckpg/resolver-go/internal/minifier/css"
"github.com/tgckpg/resolver-go/internal/resolver"
)
@@ -23,8 +25,13 @@ func main() {
}
h := handler{
r: r,
closure: closure.NewCompileCache(2),
r: r,
jsCache: compilecache.New(
closure.NewCompiler(), 2, 128,
),
cssCache: compilecache.New(
css.NewEsbuildCompiler(), 2, 128,
),
}
http.HandleFunc("/", h.index)
log.Printf("botan-api listening on %s", *addr)
@@ -32,8 +39,9 @@ func main() {
}
type handler struct {
r *resolver.Resolver
closure *closure.CompileCache
r *resolver.Resolver
jsCache *compilecache.Cache
cssCache *compilecache.Cache
}
func (h handler) index(w http.ResponseWriter, req *http.Request) {
@@ -71,7 +79,8 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
}
if outMode == resolver.ModeJS {
if compiled, ok := h.closure.Get(res.Hash); ok {
state, compiled, err := h.jsCache.Get(res.Hash)
if state == compilecache.Ready {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("X-Botan-Compiled", "hit")
elapsed := time.Since(timerStart)
@@ -86,31 +95,43 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
return
}
h.closure.Enqueue(closure.CompileJob{
Hash: res.Hash,
Mode: "js",
ExternSources: jsExterns,
JSSources: []closure.SourceInput{
{
Name: "botanjs-" + res.Hash + ".js",
Source: string(res.Content),
h.jsCache.Enqueue(compilecache.Job{
Hash: res.Hash,
Mode: "js",
Payload: closure.CompilePayload{
ExternSources: jsExterns,
JSSources: []closure.SourceInput{
{
Name: "botanjs-" + res.Hash + ".js",
Source: string(res.Content),
},
},
Defines: map[string]any{
"DEBUG": false,
},
},
Defines: map[string]any{
"DEBUG": false,
})
} else if outMode == resolver.ModeCSS {
state, compiled, _ := h.cssCache.Get(res.Hash)
if state == compilecache.Ready {
w.Header().Set("Content-Type", "text/css")
w.Header().Set("X-Botan-Compiled", "hit")
elapsed := time.Since(timerStart)
w.Header().Set("X-Botan-Resolve-Time", elapsed.String())
w.Write(compiled)
return
}
h.cssCache.Enqueue(compilecache.Job{
Hash: res.Hash,
Mode: "css",
Payload: css.CompilePayload{
Name: "style.css",
Source: string(res.Content),
},
})
}
// Compatibility flags:
// rjs/rcss/ojs/ocss => content
// hjs/hcss => hash filename only
// js/css => currently content; cluster compiler can switch this later.
if strings.HasPrefix(modeText, "h") {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = w.Write([]byte(res.Hash))
return
}
w.Header().Set("Content-Type", res.ContentType)
w.Header().Set("X-Botan-Compiled", "miss")