implement /resourced/internal/check_resource

This commit is contained in:
blek 2023-11-16 20:14:40 +10:00
parent 0793079cad
commit 47472eb1c6
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 32 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"strings"
"net/http"
"io/ioutil"
"encoding/json"
)
import (
@ -20,6 +21,9 @@ type Resource struct {
Proxied bool `toml:"proxied",default:false`
Mime string `toml:"mime"`
}
type CheckResourceType struct {
Type string `json:"type"`
}
func (self Resource) Get() ([]byte, error) {
return ioutil.ReadFile(self.Url[7:])
}
@ -115,6 +119,34 @@ func main() {
}
return c.Next()
})
app.Post("/internal/check_resource", func (c *fiber.Ctx) error {
resources := new([]string)
err := json.Unmarshal(c.Body(), &resources)
if err != nil {
log.Fatalln(err)
return c.Send([]byte("{ \"error\": \"500\"}"))
}
resources_exist := make(map[string]CheckResourceType)
for _, resource := range *resources {
if val, ok := conf.Resource[resource]; ok {
resources_exist[resource] = CheckResourceType {
Type: val.Mime,
}
}
}
marshaled, err := json.Marshal(resources_exist)
if err != nil {
log.Fatalln(err)
return c.Send([]byte("{ \"error\": \"500\"}"))
}
return c.Send([]byte(marshaled))
})
app.Get("/:id", func (c *fiber.Ctx) error {
res, exists := conf.Resource[c.Params("id")]