proxied resources

This commit is contained in:
blek 2023-11-05 14:09:30 +10:00
parent a9df435ebe
commit 318d7788a8
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 33 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strings"
"net/http"
"io/ioutil"
)
@ -15,6 +16,7 @@ import (
type Resource struct {
Url string `toml:"url"`
Proxied bool `toml:"proxied",default:false`
Mime string `toml:"mime"`
}
func (self Resource) Get() ([]byte, error) {
@ -72,13 +74,30 @@ func main() {
})
app.Get("/:id", func (c *fiber.Ctx) error {
res, exists := conf.Resource[c.Params("id")]
if ! exists {
return c.Status(fiber.StatusNotFound).SendString("Resource not found")
}
if ! strings.HasPrefix(res.Url, "file://") {
if res.Proxied {
resp, err := http.Get(res.Url)
if err != nil {
log.Fatalln(err)
} else {
c.Response().Header.SetContentType(res.Mime)
// c.Response().Header.SetContentLength(resp.ContentLength)
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return c.Status(500).SendString("Internal error")
}
return c.Send(buf)
}
}
c.Location(res.Url)
c.Status(302)
return nil

View File

@ -19,7 +19,6 @@ ListenUrl="0.0.0.0:80"
# dev.indie_guy.logo
# com.pany.logo
# Test your names here: https://regex101.com/r/wQdOup/2
#
[Resource."com.example.logo"]
# Can also be an external link
# If an external link is specified,
@ -29,3 +28,14 @@ Url="file:///some/where"
# File type, as according to this:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
mime="image/jpg"
# (if url is an http(s) url))
# Whether to proxy the resource.
# true - Send the resource like a regular file
# false - Send a 302 HTTP redirect to the URL (default)
#
# It is not recommended to set this to `true`
# unless you are referring to a resource that is
# available only in the local network
#
# proxied=true