Add resource service #19

Merged
blek merged 13 commits from resource-service into 0.2-dev 2023-11-11 09:09:48 +01:00
1 changed files with 25 additions and 0 deletions
Showing only changes of commit a9df435ebe - Show all commits

View File

@ -2,6 +2,9 @@ package main
import ( import (
"log" "log"
"fmt"
"regexp"
"strings"
"io/ioutil" "io/ioutil"
) )
@ -28,6 +31,19 @@ type Config struct {
Resource map[string]Resource `toml:"resource"` Resource map[string]Resource `toml:"resource"`
} }
func (self Config) Validate() int {
re, err := regexp.Compile(`^(file|http(s|))://`)
if err != nil { panic(err) }
for key, res := range self.Resource {
if ! re.MatchString(res.Url) {
panic(fmt.Sprintf("Resource %s has invalid URL: %s\nOnly file://, http:// and https:// URLs are allowed", key, res.Url))
}
}
return 0
}
func main() { func main() {
var conf Config var conf Config
@ -38,6 +54,8 @@ func main() {
if err != nil { panic(err) } if err != nil { panic(err) }
_ = a _ = a
conf.Validate()
app := fiber.New(fiber.Config { app := fiber.New(fiber.Config {
Prefork: true, Prefork: true,
CaseSensitive: false, CaseSensitive: false,
@ -60,6 +78,12 @@ func main() {
return c.Status(fiber.StatusNotFound).SendString("Resource not found") return c.Status(fiber.StatusNotFound).SendString("Resource not found")
} }
if ! strings.HasPrefix(res.Url, "file://") {
c.Location(res.Url)
c.Status(302)
return nil
}
data, err := res.Get() data, err := res.Get()
if err != nil { if err != nil {
panic(err) panic(err)
@ -67,6 +91,7 @@ func main() {
c.Response().Header.SetContentType(res.Mime) c.Response().Header.SetContentType(res.Mime)
c.Response().Header.SetContentLength(len(data)) c.Response().Header.SetContentLength(len(data))
return c.Send(data) return c.Send(data)
}) })