This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (67 loc) · 1.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/recover"
"io"
"log"
"os"
"strings"
)
var ResourcePath = map[string]string{}
var ResourceData = map[string][]byte{}
var ResourceHash = map[string]string{}
func init() {
open, err := os.Open("resources.json")
if err != nil {
log.Fatalf("failed to open resource: %s", err)
}
data, err := io.ReadAll(open)
if err != nil {
log.Fatalf("failed to read resource: %s", err)
}
err = json.Unmarshal(data, &ResourcePath)
if err != nil {
log.Fatalf("failed to unmarshal json data: %s", err)
}
for k, v := range ResourcePath {
open, err = os.Open("Resources/" + v)
if err != nil {
log.Fatalf("failed to open resource: %s", err)
}
data, err = io.ReadAll(open)
if err != nil {
log.Fatalf("failed to read resource: %s", err)
}
ResourceData[k] = data
ResourceHash[k] = fmt.Sprintf("%x", sha256.Sum256(data))
}
}
func main() {
app := fiber.New(fiber.Config{
Prefork: true,
})
app.Use(recover.New())
app.Use(compress.New(compress.Config{
Level: compress.LevelBestSpeed,
}))
app.Get("/:type/:hash", downloadResource)
log.Fatal(app.Listen(":3001"))
}
func downloadResource(c *fiber.Ctx) error {
c.Set("surrogate-key", "mod-resource")
resourceType := strings.ToLower(c.Params("type"))
resourceData := ResourceData[resourceType]
if resourceData == nil {
return c.SendStatus(404)
}
resourceHash := strings.ToLower(c.Params("hash"))
if resourceHash == ResourceHash[resourceType] {
return c.SendStatus(204)
}
c.Set("content-type", "application/octet-stream")
return c.Send(resourceData)
}