diff --git a/resource/main.go b/resource/main.go index 0c349b6..c0a4002 100644 --- a/resource/main.go +++ b/resource/main.go @@ -5,6 +5,7 @@ import ( "fmt" "regexp" "strings" + "net/http" "io/ioutil" ) @@ -14,8 +15,9 @@ import ( ) type Resource struct { - Url string `toml:"url"` - Mime string `toml:"mime"` + Url string `toml:"url"` + Proxied bool `toml:"proxied",default:false` + Mime string `toml:"mime"` } func (self Resource) Get() ([]byte, error) { return ioutil.ReadFile(self.Url[7:]) @@ -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 diff --git a/resource/resourced.toml.example b/resource/resourced.toml.example index 7cf863b..e8fca0e 100644 --- a/resource/resourced.toml.example +++ b/resource/resourced.toml.example @@ -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 \ No newline at end of file