Skip to content

Commit

Permalink
add esm and unpkg proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 1, 2023
1 parent 9b644fd commit 5699171
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func main() {
{
packages.RegisterJsdelivr(app)
packages.RegisterFonts(app)
packages.RegisterEsm(app)
packages.RegisterUnpkg(app)
packages.RegisterCustom(app)
}

if err := app.Listen(fmt.Sprintf(":%d", viper.GetInt("port"))); err != nil {
Expand Down
27 changes: 27 additions & 0 deletions packages/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package packages

import (
"fmt"
"github.com/gofiber/fiber/v2"
"proxy/utils"
"strings"
)

var CustomReplacer = map[string]string{
"chatnio": "https://chatnio.net",
}

func RegisterCustom(app *fiber.App) {
for _, v := range CustomReplacer {
app.All(fmt.Sprintf("/%s/*", v), func(c *fiber.Ctx) error {
source := strings.TrimPrefix(c.Path(), fmt.Sprintf("/%s", v))
uri := fmt.Sprintf("%s%s", v, source)
data, err := utils.Get(uri, nil)
if err != nil {
return c.SendString(fmt.Sprintf("error during get data: %s", err.Error()))
}

return c.SendString(data)
})
}
}
23 changes: 23 additions & 0 deletions packages/esm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package packages

import (
"fmt"
"github.com/gofiber/fiber/v2"
"proxy/utils"
"strings"
)

func RegisterEsm(app *fiber.App) {
app.All("/esm/*", func(c *fiber.Ctx) error {
source := strings.TrimPrefix(c.Path(), "/esm")
uri := fmt.Sprintf("https://cdn.jsdelivr.net/npm%s", source)
data, err := utils.Get(uri, nil)
if err != nil {
return c.SendString(fmt.Sprintf("error during get data: %s", err.Error()))
}

data = strings.Replace(data, "https://esm.sh", "/esm", -1)

return c.SendString(data)
})
}
23 changes: 23 additions & 0 deletions packages/unpkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package packages

import (
"fmt"
"github.com/gofiber/fiber/v2"
"proxy/utils"
"strings"
)

func RegisterUnpkg(app *fiber.App) {
app.All("/unpkg/*", func(c *fiber.Ctx) error {
source := strings.TrimPrefix(c.Path(), "/unpkg")
uri := fmt.Sprintf("https://unpkg.com%s", source)
data, err := utils.Get(uri, nil)
if err != nil {
return c.SendString(fmt.Sprintf("error during get data: %s", err.Error()))
}

data = strings.Replace(data, "https://unpkg.com", "/unpkg", -1)

return c.SendString(data)
})
}

0 comments on commit 5699171

Please sign in to comment.