forked from Botanical/BotanJS
Rename the components
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
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
|
||||
}
|
||||
|
||||
log.Println(res.JSFiles)
|
||||
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user