-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (76 loc) · 2.41 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
87
88
89
90
91
92
93
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
"os"
"strings"
)
// Main Entry Point
func main() {
e := echo.New()
e.Use(middleware.CORS())
e.Static("/", "webcontent")
util := Utils{}
e.GET("/resolve_cf_slug/:slug", func(c echo.Context) error {
slug := c.Param("slug")
cfurl, err := util.GetCurseforgeIdFromSlug(slug, os.Getenv("CURSE_API_TOKEN"))
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, cfurl)
})
// Generic route to handle both .jar and .pom requests from gradle
e.GET("/unimaven/:platform/:slug/:fileid/:type", func(c echo.Context) error {
// Extract the required values from the URL
platform := c.Param("platform")
slug := c.Param("slug")
fileid := c.Param("fileid")
ftype := c.Param("type")
// Gradle requested the .pom file for the artifact, so we generate and return it
if strings.HasSuffix(ftype, ".pom") {
pom := generatePom(platform, slug, fileid)
return c.XMLBlob(http.StatusOK, []byte(pom))
}
// If the request is not for a .jar or .pom, we return a 404 not found
if !strings.HasSuffix(ftype, ".jar") {
return c.String(http.StatusNotFound, "Not Found")
}
// Handle CurseForge artifacts
if platform == "curseforge" {
cfurl, err := util.GetCurseforgeUrl(slug, fileid, os.Getenv("CURSE_API_TOKEN"))
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.Redirect(http.StatusFound, cfurl)
}
// Handle NightBloom artifacts
if platform == "nightbloom" {
nburl, err := util.GetNightBloomUrl(slug, fileid)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.Redirect(http.StatusFound, nburl)
}
// Handle Modrinth artifacts
if platform == "modrinth" {
mrurl, err := util.GetModrinthUrl(slug, fileid)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.Redirect(http.StatusFound, mrurl)
}
// Handle GitHub releases
if strings.HasPrefix(platform, "github") {
newPlatform := strings.Replace(platform, "github-", "", 1)
return c.Redirect(http.StatusFound, util.GetGitHubUrl(newPlatform, slug, fileid))
}
// Unsupported platform, so we return a 404
return c.String(http.StatusNotFound, "Not Found")
})
// Start the server
err := e.Start(":8080")
if err != nil {
return
}
}