Skip to content

Commit

Permalink
Update chain.js handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nidorx committed May 7, 2023
1 parent c1612b9 commit 6b8ff6f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
35 changes: 20 additions & 15 deletions socket/client-js-config.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
package socket

import (
"bytes"
"embed"
"github.com/rs/zerolog/log"
"github.com/syntax-framework/chain"
"net/http"
"path"
"strconv"
"time"
)

var (
//go:embed client/chain.js
clientJsFS embed.FS
clientJsContent []byte
clientJsEtag string
clientJsModTime, _ = time.Parse(time.DateTime, "2023-05-07 00:00:00")
configuredRouterClient = map[*chain.Router]bool{}
)

func init() {
var err error
if clientJsContent, err = clientJsFS.ReadFile("client/chain.js"); err != nil {
log.Panic().Err(err).Caller(1).
Stack().
Msg(_l("cannot load client/chain.js"))
if content, err := clientJsFS.ReadFile("client/chain.js"); err != nil {
log.Panic().Err(err).Caller(1).Stack().Msg(_l("cannot load client/chain.js"))
} else {
clientJsContent = content
clientJsEtag = chain.HashCrc32(clientJsContent)
}
}

func ClientJsAddHandler(router *chain.Router) {
if _, exist := configuredRouterClient[router]; exist {
// ClientJsHandler add "/chain.js" endpoint
func ClientJsHandler(r *chain.Router, route string) {
if _, exist := configuredRouterClient[r]; exist {
// @TODO: Permitir saber quando a instancia é destruida r.OnDestroy(func() { })
return
}

router.GET("/chain.js", func(ctx *chain.Context) {
ctx.SetHeader("Content-Type", "application/javascript")
jsPath := path.Join(route, "/chain.js")
r.GET(jsPath, func(ctx *chain.Context) {
ctx.SetHeader("Content-Type", "text/javascript; charset=utf-8")
ctx.SetHeader("Content-Length", strconv.Itoa(len(clientJsContent)))
// Etag
// Last-Modified
if _, err := ctx.Write(clientJsContent); err != nil {
log.Error().Stack().Err(err).Caller(1).
Msg(_l("it was not possible to deliver /chain.js"))
}
ctx.SetHeader("ETag", clientJsEtag)
http.ServeContent(ctx.Writer, ctx.Request, "/chain.js", clientJsModTime, bytes.NewReader(clientJsContent))
})
}
2 changes: 1 addition & 1 deletion socket/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Handler struct {

func (h *Handler) Configure(router *chain.Router, endpoint string) {

ClientJsAddHandler(router)
ClientJsHandler(router, endpoint)

if h.Options == nil {
h.Options = map[string]any{}
Expand Down
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package chain

import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"hash/crc32"
)

type Serializer interface {
Expand Down Expand Up @@ -31,3 +33,11 @@ func HashMD5(text string) string {
h.Write([]byte(text))
return hex.EncodeToString(h.Sum(nil))
}

var crc32iSCSI = crc32.MakeTable(crc32.Castagnoli)

func HashCrc32(content []byte) string {
h := crc32.New(crc32iSCSI)
h.Write(content)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

0 comments on commit 6b8ff6f

Please sign in to comment.