Compare commits
2 Commits
d018c0c2ba
...
47472eb1c6
Author | SHA1 | Date |
---|---|---|
blek | 47472eb1c6 | |
blek | 0793079cad |
|
@ -14,6 +14,8 @@ tags:
|
||||||
description: API for serving data
|
description: API for serving data
|
||||||
- name: System API
|
- name: System API
|
||||||
description: API for serving system data
|
description: API for serving system data
|
||||||
|
- name: Internal API
|
||||||
|
description: API for microservices. Shouldn't be accessible via WAN
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
/{id}:
|
/{id}:
|
||||||
|
@ -45,4 +47,47 @@ paths:
|
||||||
schema:
|
schema:
|
||||||
type: boolean
|
type: boolean
|
||||||
example: true
|
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
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"net/http"
|
"net/http"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -20,6 +21,9 @@ type Resource struct {
|
||||||
Proxied bool `toml:"proxied",default:false`
|
Proxied bool `toml:"proxied",default:false`
|
||||||
Mime string `toml:"mime"`
|
Mime string `toml:"mime"`
|
||||||
}
|
}
|
||||||
|
type CheckResourceType struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
func (self Resource) Get() ([]byte, error) {
|
func (self Resource) Get() ([]byte, error) {
|
||||||
return ioutil.ReadFile(self.Url[7:])
|
return ioutil.ReadFile(self.Url[7:])
|
||||||
}
|
}
|
||||||
|
@ -115,6 +119,34 @@ func main() {
|
||||||
}
|
}
|
||||||
return c.Next()
|
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 {
|
app.Get("/:id", func (c *fiber.Ctx) error {
|
||||||
res, exists := conf.Resource[c.Params("id")]
|
res, exists := conf.Resource[c.Params("id")]
|
||||||
|
|
Loading…
Reference in New Issue