forked from Botanical/BotanJS
Removed old impl
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tgckpg/botanres-go/internal/generated"
|
||||
"github.com/tgckpg/botanres-go/internal/resolver"
|
||||
"github.com/tgckpg/resolver-go/internal/closure"
|
||||
"github.com/tgckpg/resolver-go/internal/generated"
|
||||
"github.com/tgckpg/resolver-go/internal/resolver"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -20,13 +23,24 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
h := handler{r: r}
|
||||
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 }
|
||||
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, "/")
|
||||
@@ -61,7 +75,46 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(res.JSFiles)
|
||||
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
|
||||
@@ -73,5 +126,7 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", res.ContentType)
|
||||
w.Header().Set("X-Botan-Compiled", "miss")
|
||||
|
||||
_, _ = w.Write(res.Content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user