Add resource service #19

Merged
blek merged 13 commits from resource-service into 0.2-dev 2023-11-11 09:09:48 +01:00
2 changed files with 33 additions and 4 deletions
Showing only changes of commit 318d7788a8 - Show all commits

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"net/http"
"io/ioutil" "io/ioutil"
) )
@ -14,8 +15,9 @@ import (
) )
type Resource struct { type Resource struct {
Url string `toml:"url"` Url string `toml:"url"`
Mime string `toml:"mime"` Proxied bool `toml:"proxied",default:false`
Mime string `toml:"mime"`
} }
func (self Resource) Get() ([]byte, error) { func (self Resource) Get() ([]byte, error) {
return ioutil.ReadFile(self.Url[7:]) return ioutil.ReadFile(self.Url[7:])
@ -72,13 +74,30 @@ func main() {
}) })
app.Get("/:id", func (c *fiber.Ctx) error { app.Get("/:id", func (c *fiber.Ctx) error {
res, exists := conf.Resource[c.Params("id")] res, exists := conf.Resource[c.Params("id")]
if ! exists { if ! exists {
return c.Status(fiber.StatusNotFound).SendString("Resource not found") return c.Status(fiber.StatusNotFound).SendString("Resource not found")
} }
if ! strings.HasPrefix(res.Url, "file://") { 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.Location(res.Url)
c.Status(302) c.Status(302)
return nil return nil

View File

@ -19,7 +19,6 @@ ListenUrl="0.0.0.0:80"
# dev.indie_guy.logo # dev.indie_guy.logo
# com.pany.logo # com.pany.logo
# Test your names here: https://regex101.com/r/wQdOup/2 # Test your names here: https://regex101.com/r/wQdOup/2
#
[Resource."com.example.logo"] [Resource."com.example.logo"]
# Can also be an external link # Can also be an external link
# If an external link is specified, # If an external link is specified,
@ -29,3 +28,14 @@ Url="file:///some/where"
# File type, as according to this: # File type, as according to this:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types # https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
mime="image/jpg" 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