Compare commits

..

2 Commits

2 changed files with 78 additions and 1 deletions

View File

@ -14,6 +14,8 @@ tags:
description: API for serving data
- name: System API
description: API for serving system data
- name: Internal API
description: API for microservices. Shouldn't be accessible via WAN
paths:
/{id}:
@ -45,4 +47,47 @@ paths:
schema:
type: boolean
example: true
/internal/check_resource:
post:
summary: Check if a resource(s) exist
description: |-
This method takes an array of resources and reports which ones exist.
See responses for more info.
tags:
- Internal API
requestBody:
description: |-
An array of strings, where each string is a resource ID
Let's assume that we want to check if two resources exist: `dev.blek.file.logo` and `org.nonexistant.resource`.
content:
application/json:
schema:
type: array
items:
type: string
example:
- dev.blek.file.logo
- org.nonexistant.resource
responses:
200:
description: |-
As in the request body example, let's assume that this method checked if these two resources exist:
- `dev.blek.file.logo`
- `org.nonexistant.resource`
As we can see, this returned only `dev.blek.file.logo` because its the only resource of the provided ones that exist.
content:
application/json:
schema:
type: object
additionalProperties:
type: object
properties:
type:
type: string
example:
dev.blek.file.logo:
type: image/png

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")]