forked from itsron143/ParticleGround-Portfolio
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtemplates.go
143 lines (121 loc) · 3.61 KB
/
templates.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/gorilla/mux"
)
type FileDetails struct {
Base FileInfo `json:"base"`
Mono FileInfo `json:"mono"`
}
type MirrorListResponse struct {
Version string `json:"version"`
Timestamp string `json:"timestamp"`
Mirrors []MirrorEntry `json:"mirrors"`
}
type MirrorEntry struct {
Name string `json:"name"`
Url string `json:"url"`
Checksum Checksum `json:"checksum"`
Filesize string `json:"filesize"`
}
type Release struct {
Name string `json:"name"`
ReleaseDate string `json:"release_date"`
ReleaseNotes string `json:"release_notes"`
}
type FileInfo struct {
Name string `json:"name"`
Filename string `json:"filename"`
Filesize string `json:"filesize"`
Checksum Checksum `json:"checksum"`
URL string `json:"url"`
Timestamp string `json:"timestamp"`
Mirrors []FileInfo `json:"mirrors"`
}
type Checksum struct {
SHA512 string `json:"512"`
SHA256 string `json:"256"`
}
// https://cdn.blazium.app/nightly/0.2.4/templates.json
func MirrorListHandler(w http.ResponseWriter, r *http.Request) {
// Extract the version from the URL
vars := mux.Vars(r)
version := vars["version"]
// Split the version string
versionParts := strings.Split(version, ".")
if len(versionParts) < 4 {
http.Error(w, "Invalid version format", http.StatusBadRequest)
return
}
// Determine base version and version type
baseVersion := strings.Join(versionParts[0:3], ".")
versionType := versionParts[3]
// Construct the details.json URL
url := fmt.Sprintf("https://cdn.blazium.app/%s/%s/templates.json", versionType, baseVersion)
// Make a HEAD request to check if details.json exists
resp, err := http.Head(url)
if err != nil || resp.StatusCode != http.StatusOK {
// If the file doesn't exist, return an empty response
emptyResponse := MirrorListResponse{Version: version}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(emptyResponse)
return
}
// Get the details.json file
resp, err = http.Get(url)
if err != nil || resp.StatusCode != http.StatusOK {
http.Error(w, "Failed to fetch details.json", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Failed to read details.json", http.StatusInternalServerError)
return
}
// Parse details.json
var details FileDetails
if err := json.Unmarshal(body, &details); err != nil {
http.Error(w, "Failed to parse details.json", http.StatusInternalServerError)
return
}
// Determine whether to use Mono or Base based on the version suffix
var fileInfo FileInfo
if len(versionParts) > 4 && versionParts[4] == "mono" {
fileInfo = details.Mono
} else {
fileInfo = details.Base
}
// Populate the MirrorListResponse
mirrorList := MirrorListResponse{
Version: version,
Timestamp: fileInfo.Timestamp,
}
// Add Base or Mono as the first MirrorEntry
baseOrMonoEntry := MirrorEntry{
Name: fileInfo.Name,
Url: fileInfo.URL,
Checksum: fileInfo.Checksum,
Filesize: fileInfo.Filesize,
}
mirrorList.Mirrors = append(mirrorList.Mirrors, baseOrMonoEntry)
// Add any additional mirror entries
if len(fileInfo.Mirrors) > 0 {
for _, mirror := range fileInfo.Mirrors {
mirrorEntry := MirrorEntry{
Name: mirror.Name,
Url: mirror.URL,
Checksum: mirror.Checksum,
Filesize: mirror.Filesize,
}
mirrorList.Mirrors = append(mirrorList.Mirrors, mirrorEntry)
}
}
// Return the response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mirrorList)
}