Files
AstroJS/resolver-go/cmd/botan-api/main.go
T
2026-06-12 04:51:38 +08:00

133 lines
3.0 KiB
Go

package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"log"
"net/http"
"strings"
"github.com/tgckpg/resolver-go/internal/closure"
"github.com/tgckpg/resolver-go/internal/generated"
"github.com/tgckpg/resolver-go/internal/resolver"
)
func main() {
addr := flag.String("addr", ":8080", "listen address")
src := flag.String("src", "./src", "BotanJS source root")
flag.Parse()
r, err := resolver.New(*src, generated.ClassMap)
if err != nil {
log.Fatal(err)
}
h := handler{
r: r,
closure: closure.NewCompileCache(2),
}
http.HandleFunc("/", h.index)
log.Printf("botan-api listening on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
type handler struct {
r *resolver.Resolver
closure *closure.CompileCache
}
func hashStrings(parts []string) string {
sum := md5.Sum([]byte(strings.Join(parts, "|")))
return hex.EncodeToString(sum[:])
}
func (h handler) index(w http.ResponseWriter, req *http.Request) {
path := strings.Trim(req.URL.Path, "/")
if path == "" {
_, _ = w.Write([]byte("BotanJS Go resolver API\n"))
return
}
parts := strings.SplitN(path, "/", 2)
modeText := parts[0]
code := ""
if len(parts) == 2 {
code = parts[1]
} else {
code = req.URL.Query().Get("p")
}
if code == "" {
http.Error(w, "missing request payload", http.StatusBadRequest)
return
}
outMode := resolver.ModeJS
if strings.HasSuffix(modeText, "css") {
outMode = resolver.ModeCSS
} else if !strings.HasSuffix(modeText, "js") {
http.Error(w, "invalid mode", http.StatusBadRequest)
return
}
res, err := h.r.ResolveRequest(code, outMode, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if outMode == resolver.ModeJS {
fileHashes := make([]string, 0, len(res.JSFiles))
for _, f := range res.JSFiles {
fileHashes = append(fileHashes, f.JSHash)
}
hash := hashStrings(fileHashes)
if compiled, ok := h.closure.Get(hash); ok {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("X-Botan-Compiled", "hit")
w.Write(compiled)
return
}
jsFiles := make([]string, 0, len(res.JSFiles))
for _, f := range res.JSFiles {
jsFiles = append(jsFiles, f.Src)
}
jsExterns, err := h.r.GetExterns(generated.Externs)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
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),
},
},
Defines: map[string]any{
"DEBUG": false,
},
})
}
// 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")
_, _ = w.Write(res.Content)
}