Refactor middleware to handle Sui files and fix potential errors

This commit is contained in:
Max 2023-12-10 12:38:55 +08:00
parent 0dde10caca
commit 07dfc90d2e
2 changed files with 18 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package service
import (
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
@ -47,6 +48,7 @@ func withStaticFileServer(c *gin.Context) {
if matches := rewrite.Pattern.FindStringSubmatch(c.Request.URL.Path); matches != nil {
log.Debug("Rewrite FindStringSubmatch: %s => %s", c.Request.URL.Path, rewrite.Replacement)
c.Request.URL.Path = rewrite.Pattern.ReplaceAllString(c.Request.URL.Path, rewrite.Replacement)
log.Debug("Rewrite FindStringSubmatch After: %s", c.Request.URL.Path)
break
}
}
@ -54,14 +56,23 @@ func withStaticFileServer(c *gin.Context) {
// Sui file server
if strings.HasSuffix(c.Request.URL.Path, ".sui") {
log.Debug("Sui File: %s", c.Request.URL.Path)
// Default index.sui
if filepath.Base(c.Request.URL.Path) == ".sui" {
c.Request.URL.Path = strings.TrimSuffix(c.Request.URL.Path, ".sui") + "index.sui"
}
r, code, err := api.NewRequestContext(c)
if err != nil {
log.Error("Sui Reqeust Error: %s", err.Error())
c.AbortWithError(code, err)
return
}
html, code, err := r.Render()
if err != nil {
log.Error("Sui Render Error: %s", err.Error())
c.AbortWithError(code, err)
return
}

View file

@ -137,8 +137,14 @@ func parserPath(c *gin.Context) (string, map[string]string, error) {
}
}
// No matchers
if matchers == nil {
return "", nil, fmt.Errorf("no route matchers")
if len(parts) < 1 {
return "", nil, fmt.Errorf("no route matchers")
}
fileParts = append(fileParts, parts...)
return filepath.Join(fileParts...) + ".sui", params, nil
}
// Match the page parts