Browse Source

[Mod] update publish api

XuPeng 1 year ago
parent
commit
9547c461bb
6 changed files with 408 additions and 8 deletions
  1. 74 0
      api/publish.js
  2. 5 4
      nginx.conf
  3. 2 0
      package.json
  4. 4 1
      route.js
  5. 2 1
      server.js
  6. 321 2
      yarn.lock

+ 74 - 0
api/publish.js

@@ -0,0 +1,74 @@
+const micro = require('micro')
+const path = require('path')
+const low = require('lowdb')
+const FileSync = require('lowdb/adapters/FileSync')
+const { send, json } = micro
+
+const dbPath = path.join(__dirname, '../static', 'db.json')
+const adapter = new FileSync(dbPath)
+const db = low(adapter)
+// Set some defaults (required if your JSON file is empty)
+db.defaults({ finogeeks: {
+  test: {
+    key: 'test',
+    name: 'Finchat(开发版)',
+    icon: 'https://app.finogeeks.club/uploads/pkg/icon/2370/AppIcon60x60_3x.png',
+    versions: []
+  },
+  inner: {
+    key: 'inner',
+    name: 'Finchat(正式版)',
+    icon: 'https://app.finogeeks.club/uploads/pkg/icon/2370/AppIcon60x60_3x.png',
+    versions: []
+  },
+  public: {
+    key: 'public',
+    name: 'Finchat(公有云版)',
+    icon: 'https://app.finogeeks.club/uploads/pkg/icon/2370/AppIcon60x60_3x.png',
+    versions: []
+  } 
+}})
+.write()
+
+const update = async (req, res) => {
+  const body = await json(req)
+  const versions = body.versions
+  if (versions && Array.isArray(versions)) {
+    const finogeeks = db.get('finogeeks').value()
+    const data = {}
+    let timestamp = Date.now()
+    versions.forEach(item => {
+      const key = item.type
+      const isExist = finogeeks[key] && finogeeks[key].versions.find(i => i.version === item.version)
+      if (!isExist) {
+        if (!data[key]) {
+          data[key] = finogeeks[key].versions || []
+        }
+        data[key].push({
+          urls: item.urls,
+          timestamp: item.timestamp || timestamp,
+          name: item.name,
+          version: item.version,
+          dependency: item.dependency,
+        })
+      }
+    })
+    Object.keys(data).map(key => {
+      finogeeks[key].versions = data[key]
+    })
+    await db.set('finogeeks', finogeeks).write() 
+    send(res, 200, 'ok')
+    return
+  }
+  send(res, 404, 'versions is required')
+}
+
+const get = async (req, res) => {
+  const finogeeks = db.get('finogeeks').value()
+
+  send(res, 200, Object.values(finogeeks))
+}
+
+module.exports = {
+  get, update
+}

+ 5 - 4
nginx.conf

@@ -93,20 +93,21 @@ http {
     listen       9000;
     server_name  localhost;
 
-    location ^~ /api/upload {
+
+    location ^~ /api/massmessage {
       proxy_redirect off;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-      proxy_pass http://127.0.0.1:5000;
+      proxy_pass http://finchat-mass-message.finchat-mass-message:9000/;
     }
 
-    location ^~ /api/massmessage {
+    location ^~ /api/ {
       proxy_redirect off;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-      proxy_pass http://finchat-mass-message.finchat-mass-message:9000/;
+      proxy_pass http://127.0.0.1:5000;
     }
 
     location ~* /(.*)/static {

+ 2 - 0
package.json

@@ -11,7 +11,9 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "lowdb": "^1.0.0",
     "micro": "^9.3.3",
+    "micro-cors": "^0.1.1",
     "micro-upload": "^1.0.1",
     "microrouter": "^3.1.3",
     "pm2": "^3.1.3"

+ 4 - 1
route.js

@@ -1,6 +1,9 @@
 const { router, get, post } = require('microrouter')
 const upload = require('./api/upload')
+const publish = require('./api/publish')
 
 module.exports = router(
-  post('/api/upload', upload)
+  post('/api/upload', upload),
+  post('/api/publish', publish.update),
+  get('/api/publish', publish.get)
 )

+ 2 - 1
server.js

@@ -1,6 +1,7 @@
 const micro = require('micro')
 const route = require('./route')
+const cors = require('micro-cors')
 
-const server = micro(route)
+const server = micro(cors()(route))
 
 server.listen(5000)

File diff suppressed because it is too large
+ 321 - 2
yarn.lock