add ability to view apps under root and specify custom appid in metadata
This commit is contained in:
parent
e1e93a9336
commit
024bda0c41
37
main.go
37
main.go
|
@ -77,10 +77,8 @@ func main() {
|
|||
// Serve static files
|
||||
r.Static("/apps/files", "./apps/files")
|
||||
|
||||
// Group routes for /apps
|
||||
apps := r.Group("/apps")
|
||||
{
|
||||
apps.GET("", func(c *gin.Context) {
|
||||
// Handler function for getting apps list
|
||||
getAppsHandler := func(c *gin.Context) {
|
||||
data, err := ioutil.ReadFile("apps.json")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, []App{})
|
||||
|
@ -90,7 +88,15 @@ func main() {
|
|||
var apps []App
|
||||
json.Unmarshal(data, &apps)
|
||||
c.JSON(http.StatusOK, apps)
|
||||
})
|
||||
}
|
||||
|
||||
// Serve apps list on root path
|
||||
r.GET("/", getAppsHandler)
|
||||
|
||||
// Group routes for /apps
|
||||
apps := r.Group("/apps")
|
||||
{
|
||||
apps.GET("", getAppsHandler)
|
||||
// Handle other methods for /apps
|
||||
apps.Handle("POST", "", methodNotAllowedHandler("GET"))
|
||||
apps.Handle("PUT", "", methodNotAllowedHandler("GET"))
|
||||
|
@ -109,17 +115,21 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
var metadata AppMetadata
|
||||
var metadata struct {
|
||||
AppMetadata
|
||||
AppID string `json:"appid,omitempty"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(metadataStr), &metadata); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid metadata"})
|
||||
return
|
||||
}
|
||||
|
||||
// Generate 12-digit app ID
|
||||
appID := metadata.AppID
|
||||
if appID == "" {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
appID := fmt.Sprintf("%012d", rand.Intn(1000000000000))
|
||||
appID = fmt.Sprintf("%012d", rand.Intn(1000000000000))
|
||||
}
|
||||
|
||||
// Create new app entry
|
||||
newApp := App{
|
||||
Name: metadata.Name,
|
||||
Ver: metadata.Ver,
|
||||
|
@ -129,7 +139,6 @@ func main() {
|
|||
Path: fmt.Sprintf("/apps/files/%s_%s%s", metadata.Name, metadata.Ver, filepath.Ext(file.Filename)),
|
||||
}
|
||||
|
||||
// Save file
|
||||
if err := os.MkdirAll("apps/files", 0755); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create directory"})
|
||||
return
|
||||
|
@ -140,7 +149,6 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
// Update apps.json
|
||||
var apps []App
|
||||
data, _ := ioutil.ReadFile("apps.json")
|
||||
json.Unmarshal(data, &apps)
|
||||
|
@ -160,7 +168,6 @@ func main() {
|
|||
editapp := r.Group("/editapp")
|
||||
{
|
||||
editapp.PUT("", authMiddleware(), func(c *gin.Context) {
|
||||
// Get metadata from form
|
||||
metadataStr := c.PostForm("metadata")
|
||||
if metadataStr == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Metadata is required"})
|
||||
|
@ -189,17 +196,13 @@ func main() {
|
|||
apps[i].Ver = updateData.App.Ver
|
||||
apps[i].Info = updateData.App.Info
|
||||
|
||||
// Handle file update if provided
|
||||
if file, err := c.FormFile("file"); err == nil {
|
||||
// Delete old file
|
||||
os.Remove(oldFilePath)
|
||||
|
||||
// Generate new path
|
||||
newPath := fmt.Sprintf("/apps/files/%s_%s%s",
|
||||
apps[i].Name, apps[i].Ver, filepath.Ext(file.Filename))
|
||||
apps[i].Path = newPath
|
||||
|
||||
// Save new file
|
||||
if err := c.SaveUploadedFile(file, "."+newPath); err != nil {
|
||||
c.JSON(http.StatusInternalServerError,
|
||||
gin.H{"error": "Failed to save new file"})
|
||||
|
@ -260,10 +263,8 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
// Delete the file
|
||||
os.Remove(filePath)
|
||||
|
||||
// Update apps.json
|
||||
appsJSON, _ := json.Marshal(apps)
|
||||
ioutil.WriteFile("apps.json", appsJSON, 0644)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "App deleted successfully"})
|
||||
|
|
Loading…
Reference in New Issue
Block a user