webservice/js_library.go

36 lines
739 B
Go

package webservice
import (
// Standard
"embed"
"fmt"
"net/http"
"regexp"
)
var (
//go:embed static/js
embedded embed.FS
pathMatcher *regexp.Regexp = regexp.MustCompile("^/_js/v[0-9]+/(.*)$")
)
func (service *Service) staticJSHandler(w http.ResponseWriter, r *http.Request) {
var path string
if comp := pathMatcher.FindStringSubmatch(r.URL.Path); comp != nil {
path = fmt.Sprintf("static/js/%s", comp[1])
} else {
service.logger.Debug("webservice", "request", r.URL.Path)
return
}
service.logger.Debug("webservice", "request", r.URL.Path, "path", path)
w.Header().Add("Content-Type", "text/javascript")
contents, err := embedded.ReadFile(path)
if err != nil {
fmt.Println(err)
return
}
w.Write(contents)
}