From 3624cfe5682f131e9682132982174eed521ade27 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 5 Nov 2023 13:13:02 +1000 Subject: [PATCH] init repo --- resource/.gitignore | 4 +++ resource/README.md | 15 +++++++++ resource/go.mod | 19 +++++++++++ resource/go.sum | 29 +++++++++++++++++ resource/main.go | 56 +++++++++++++++++++++++++++++++++ resource/resourced.toml.example | 31 ++++++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 resource/.gitignore create mode 100644 resource/README.md create mode 100644 resource/go.mod create mode 100644 resource/go.sum create mode 100644 resource/main.go create mode 100644 resource/resourced.toml.example diff --git a/resource/.gitignore b/resource/.gitignore new file mode 100644 index 0000000..4bf8d84 --- /dev/null +++ b/resource/.gitignore @@ -0,0 +1,4 @@ +resourced.toml +resourced +.DS_Store +.env diff --git a/resource/README.md b/resource/README.md new file mode 100644 index 0000000..09823a3 --- /dev/null +++ b/resource/README.md @@ -0,0 +1,15 @@ +# resourceD +A simple microservice for serving resources + +## Building +```sh +go build . +``` + +## Running +Either run it the normal way via the provided docker compose file, or `go run .`. + +Also dont forget to create `resourced.toml` + +## Configuration +The file `resourced.toml.example` serves as a both an example and reference, since it has a lot of comments. \ No newline at end of file diff --git a/resource/go.mod b/resource/go.mod new file mode 100644 index 0000000..0ee4b39 --- /dev/null +++ b/resource/go.mod @@ -0,0 +1,19 @@ +module blek/resourced + +go 1.21.3 + +require ( + github.com/BurntSushi/toml v1.3.2 // indirect + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/gofiber/fiber/v2 v2.50.0 // indirect + github.com/google/uuid v1.3.1 // indirect + github.com/klauspost/compress v1.16.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.50.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.13.0 // indirect +) diff --git a/resource/go.sum b/resource/go.sum new file mode 100644 index 0000000..76c49cb --- /dev/null +++ b/resource/go.sum @@ -0,0 +1,29 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw= +github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M= +github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/resource/main.go b/resource/main.go new file mode 100644 index 0000000..5bf03de --- /dev/null +++ b/resource/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "log" + "io/ioutil" +) + +import ( + "github.com/BurntSushi/toml" + "github.com/gofiber/fiber/v2" +) + +type Resource struct { + Url string `toml:"url"` + Mime string `toml:"mime"` +} + +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 + + app := fiber.New() + + app.Get("/:id", func (c *fiber.Ctx) error { + + if ! conf.ResourceD.Enabled { + return c.Status(fiber.StatusNotFound).SendString("Resource not found") + } + + res, exists := conf.Resource[c.Params("id")] + if ! exists { + return c.Status(fiber.StatusNotFound).SendString("Resource not found") + } + + return c.SendString(fmt.Sprintf("Id: %s\nUrl: %s\nType: %s", c.Params("id"), res.Url, res.Mime)) + }) + + log.Fatal(app.Listen(conf.ResourceD.ListenURL)) +} \ No newline at end of file diff --git a/resource/resourced.toml.example b/resource/resourced.toml.example new file mode 100644 index 0000000..7cf863b --- /dev/null +++ b/resource/resourced.toml.example @@ -0,0 +1,31 @@ + +# The resourceD config +[ResourceD] + +# Whether to enable the resourceD. +# If this is false, resourceD will start but respond to +# all requests with 404 +# It is false by default because resourceD is not required in a default installation. +Enabled=true + +# URL to listen on +ListenUrl="0.0.0.0:80" + +# Resource ID must be like a java package name +# At least one X.X. is required +# +# Examples: +# org.university.logo +# dev.indie_guy.logo +# com.pany.logo +# Test your names here: https://regex101.com/r/wQdOup/2 +# +[Resource."com.example.logo"] +# Can also be an external link +# If an external link is specified, +# the resource will be returned as a 302 redirect to the link +Url="file:///some/where" + +# File type, as according to this: +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types +mime="image/jpg"