forked from Botanical/BotanJS
Deprecating old approach
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tgckpg/botanres-go/internal/generated"
|
||||
"github.com/tgckpg/botanres-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}
|
||||
http.HandleFunc("/", h.index)
|
||||
log.Printf("botan-api listening on %s", *addr)
|
||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||
}
|
||||
|
||||
type handler struct{ r *resolver.Resolver }
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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.Write(res.Content)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/tgckpg/botanres-go/internal/classmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
src := flag.String("src", "./src", "BotanJS source root")
|
||||
out := flag.String("out", "internal/generated/classmap_gen.go", "generated Go output")
|
||||
pkg := flag.String("pkg", "generated", "generated package name")
|
||||
flag.Parse()
|
||||
|
||||
m, err := classmap.Build(*src)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(dir(*out), 0o755); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
code := render(*pkg, m)
|
||||
fmted, err := format.Source([]byte(code))
|
||||
if err != nil {
|
||||
_, _ = os.Stderr.WriteString(code)
|
||||
fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(*out, fmted, 0o644); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func render(pkg string, m *classmap.Map) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("// Code generated by botan-gen; DO NOT EDIT.\n")
|
||||
b.WriteString("package " + pkg + "\n\n")
|
||||
b.WriteString("import \"github.com/tgckpg/botanres-go/internal/classmap\"\n\n")
|
||||
b.WriteString("var ClassMap = &classmap.Map{\nSymbols: map[string]classmap.Symbol{\n")
|
||||
for _, s := range classmap.SortedSymbols(m) {
|
||||
fmt.Fprintf(&b, "%q: {Name:%q, Kind:%q, Parent:%q, Imports:%#v, Resource: classmap.Resource{Src:%q, JSHash:%q, CSSHash:%q}},\n",
|
||||
s.Name, s.Name, string(s.Kind), s.Parent, s.Imports, s.Resource.Src, s.Resource.JSHash, s.Resource.CSSHash)
|
||||
}
|
||||
b.WriteString("},\nFiles: map[string]classmap.Resource{\n")
|
||||
for _, r := range classmap.SortedFiles(m) {
|
||||
fmt.Fprintf(&b, "%q: {Src:%q, JSHash:%q, CSSHash:%q},\n", r.Src, r.Src, r.JSHash, r.CSSHash)
|
||||
}
|
||||
b.WriteString("},\n}\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func dir(path string) string {
|
||||
idx := strings.LastIndexAny(path, `/\\`)
|
||||
if idx < 0 {
|
||||
return "."
|
||||
}
|
||||
return path[:idx]
|
||||
}
|
||||
|
||||
func fatal(err error) {
|
||||
fmt.Fprintln(os.Stderr, "botan-gen:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user