-
Notifications
You must be signed in to change notification settings - Fork 22
/
appstore.go
173 lines (152 loc) · 5.71 KB
/
appstore.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package v1
import (
"io/ioutil"
"strconv"
"strings"
"github.com/IceWhaleTech/CasaOS-AppManagement/model"
modelCommon "github.com/IceWhaleTech/CasaOS-Common/model"
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
"github.com/IceWhaleTech/CasaOS-Common/utils/file"
"github.com/IceWhaleTech/CasaOS-Common/utils/port"
"github.com/samber/lo"
"github.com/IceWhaleTech/CasaOS-AppManagement/service"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// @Summary 获取远程列表
// @Produce application/json
// @Accept application/json
// @Tags app
// @Param index query int false "页码"
// @Param size query int false "每页数量"
// @Param category_id query int false "分类id"
// @Param type query string false "rank,new"
// @Param key query string false "search key"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/list [get]
func AppList(c *gin.Context) {
index := c.DefaultQuery("index", "1")
size := c.DefaultQuery("size", "10000")
t := c.DefaultQuery("type", "rank")
categoryID := c.DefaultQuery("category_id", "0")
key := c.DefaultQuery("key", "")
if len(index) == 0 || len(size) == 0 || len(t) == 0 || len(categoryID) == 0 {
c.JSON(common_err.CLIENT_ERROR, &modelCommon.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
return
}
serverAppLists, err := service.MyService.AppStore().GetServerList(index, size, t, categoryID, key)
if err != nil {
c.JSON(common_err.SERVICE_ERROR, &modelCommon.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
return
}
myAppList, _ := service.MyService.Docker().GetContainerAppList(nil, nil, nil)
data := make(map[string]interface{}, 3)
data["recommend"] = updateState(&serverAppLists.Recommend, myAppList)
data["list"] = updateState(&serverAppLists.List, myAppList)
data["community"] = updateState(&serverAppLists.Community, myAppList)
c.JSON(common_err.SUCCESS, &modelCommon.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
}
// @Summary 应用详情
// @Produce application/json
// @Accept application/json
// @Tags app
// @Param id path int true "id"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/appinfo/{id} [get]
func AppInfo(c *gin.Context) {
id := c.Param("id")
language := c.GetHeader("Language")
info, err := service.MyService.AppStore().GetServerAppInfo(id, "", language)
if err != nil {
c.JSON(common_err.SERVICE_ERROR, &modelCommon.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
return
}
if info.NetworkModel != "host" {
for i := 0; i < len(info.Ports); i++ {
protocol := strings.ToLower(info.Ports[i].Protocol)
if p, _ := strconv.Atoi(info.Ports[i].ContainerPort); port.IsPortAvailable(p, protocol) {
info.Ports[i].CommendPort = strconv.Itoa(p)
} else {
if protocol == "tcp" {
if p, err := port.GetAvailablePort("tcp"); err == nil {
info.Ports[i].CommendPort = strconv.Itoa(p)
}
} else if protocol == "upd" {
if p, err := port.GetAvailablePort("udp"); err == nil {
info.Ports[i].CommendPort = strconv.Itoa(p)
}
}
}
if info.Ports[i].Type == 0 {
info.PortMap = info.Ports[i].CommendPort
}
}
} else {
for i := 0; i < len(info.Ports); i++ {
if info.Ports[i].Type == 0 {
info.PortMap = info.Ports[i].ContainerPort
break
}
}
}
for i := 0; i < len(info.Devices); i++ {
if !file.CheckNotExist(info.Devices[i].ContainerPath) {
info.Devices[i].Path = info.Devices[i].ContainerPath
}
}
myAppList, _ := service.MyService.Docker().GetContainerAppList(nil, &strings.Split(info.Image, ":")[0], nil)
if len(*myAppList) > 0 {
info.State = model.StateEnumInstalled
}
info.Image += ":" + info.ImageVersion
c.JSON(common_err.SUCCESS, &modelCommon.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: info})
}
// @Summary 获取远程分类列表
// @Produce application/json
// @Accept application/json
// @Tags app
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/category [get]
func CategoryList(c *gin.Context) {
list, err := service.MyService.AppStore().GetServerCategoryList()
if err != nil {
c.JSON(common_err.SERVICE_ERROR, &modelCommon.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
return
}
var count uint
for _, category := range list {
count += category.Count
}
rear := append([]model.CategoryList{}, list[0:]...)
list = append(list[:0], model.CategoryList{Count: count, Name: "All", Font: "apps"})
list = append(list, rear...)
c.JSON(common_err.SUCCESS, &modelCommon.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: list})
}
// @Summary 分享该应用配置
// @Produce application/json
// @Accept application/json
// @Tags app
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/share [post]
func ShareAppFile(c *gin.Context) {
str, _ := ioutil.ReadAll(c.Request.Body)
content := service.MyService.AppStore().ShareAppFile(str)
c.JSON(common_err.SUCCESS, jsoniter.RawMessage(content))
}
func updateState(serverAppList *[]model.ServerAppList, myAppList *[]model.MyAppList) []model.ServerAppList {
result := make([]model.ServerAppList, len(*serverAppList))
for i, serverApp := range *serverAppList {
if lo.ContainsBy(*myAppList, func(app model.MyAppList) bool {
return serverApp.Image == strings.Split(app.Image, ":")[0]
}) {
serverApp.State = model.StateEnumInstalled
}
result[i] = serverApp
}
return result
}