bfile/resource/main.go

74 lines
1.4 KiB
Go
Raw Normal View History

2023-11-05 04:13:02 +01:00
package main
import (
"log"
"io/ioutil"
)
import (
"github.com/BurntSushi/toml"
"github.com/gofiber/fiber/v2"
)
type Resource struct {
Url string `toml:"url"`
Mime string `toml:"mime"`
}
2023-11-05 04:36:24 +01:00
func (self Resource) Get() ([]byte, error) {
return ioutil.ReadFile(self.Url[7:])
}
2023-11-05 04:13:02 +01:00
type ResourceDConfig struct {
Enabled bool `toml:"enabled"`
ListenURL string `toml:"listen_url"`
}
type Config struct {
ResourceD ResourceDConfig `toml:"resourceD"`
Resource map[string]Resource `toml:"resource"`
}
func main() {
var conf Config
data, err := ioutil.ReadFile("resourced.toml")
if err != nil { panic(err) }
a, err := toml.Decode(string(data), &conf)
if err != nil { panic(err) }
_ = a
2023-11-05 04:36:24 +01:00
app := fiber.New(fiber.Config {
Prefork: true,
CaseSensitive: false,
StrictRouting: true,
ServerHeader: "",
AppName: "blek! File resourceD",
})
2023-11-05 04:13:02 +01:00
2023-11-05 04:36:24 +01:00
app.Use(func (c *fiber.Ctx) error {
2023-11-05 04:13:02 +01:00
if ! conf.ResourceD.Enabled {
2023-11-05 04:36:24 +01:00
return c.Status(fiber.StatusNotFound).SendString("ResourceD is disabled")
2023-11-05 04:13:02 +01:00
}
2023-11-05 04:36:24 +01:00
return c.Next()
})
app.Get("/:id", func (c *fiber.Ctx) error {
2023-11-05 04:13:02 +01:00
res, exists := conf.Resource[c.Params("id")]
if ! exists {
return c.Status(fiber.StatusNotFound).SendString("Resource not found")
}
2023-11-05 04:36:24 +01:00
data, err := res.Get()
if err != nil {
panic(err)
}
c.Response().Header.SetContentType(res.Mime)
c.Response().Header.SetContentLength(len(data))
return c.Send(data)
2023-11-05 04:13:02 +01:00
})
log.Fatal(app.Listen(conf.ResourceD.ListenURL))
}