Skip to content

Commit

Permalink
fix(app-service): clean unused codes; remove cloud.storage() #21
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Feb 2, 2022
1 parent 621fb14 commit c650bfe
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 1,362 deletions.
33 changes: 32 additions & 1 deletion packages/app-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions packages/app-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
},
"keywords": [
"BaaS",
"cloud",
"mysql",
"acl",
"mongodb",
"laf.js",
"serverless",
"less-api"
"laf"
],
"dependencies": {
"adm-zip": "^0.5.9",
"alipay-sdk": "^3.1.7",
"axios": "^0.21.1",
"cloud-function-engine": "^0.7.0",
Expand All @@ -44,9 +44,10 @@
"ws": "^8.2.3"
},
"devDependencies": {
"@types/adm-zip": "^0.4.34",
"@types/dotenv": "^8.2.0",
"@types/ejs": "^3.0.7",
"@types/express": "^4.17.13",
"@types/express": "^4.17.11",
"@types/fs-extra": "^9.0.8",
"@types/jsonwebtoken": "^8.5.1",
"@types/lodash": "^4.14.171",
Expand Down
6 changes: 4 additions & 2 deletions packages/app-service/scripts/create-internal-package.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { createCloudFunctionDeclarationPackage } = require("../dist/lib/utils/init");
const { createCloudFunctionDeclarationPackage } = require("../dist/api/init");

createCloudFunctionDeclarationPackage()
createCloudFunctionDeclarationPackage()

process.exit(0)
46 changes: 0 additions & 46 deletions packages/app-service/src/api/function-log.ts

This file was deleted.

39 changes: 38 additions & 1 deletion packages/app-service/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<[email protected]>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-11-05 13:38:09
* @LastEditTime: 2022-02-03 00:58:33
* @Description:
*/

Expand Down Expand Up @@ -37,4 +37,41 @@ export async function getFunctionById(func_id: string) {
})

return doc as any
}


export interface CloudFunctionLogStruct {
requestId: string
method: string
func_id: ObjectId
func_name: string
logs: string[]
time_usage: number
data: any
error: Error
debug?: boolean
created_at?: Date
created_by?: any
}

/**
* Add function execution log
* @param data
* @returns
*/
export async function addFunctionLog(data: CloudFunctionLogStruct): Promise<any> {
const db = DatabaseAgent.db

if (!data) return null
if (typeof data.error === 'object') {
data.error = data.error.toString() as any
}

const r = await db.collection(Constants.function_log_collection)
.insertOne({
...data,
created_at: new Date()
})

return r.insertedId
}
31 changes: 0 additions & 31 deletions packages/app-service/src/api/indexes.ts

This file was deleted.

23 changes: 23 additions & 0 deletions packages/app-service/src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path = require('path')
import { Constants } from '../constants'
import { DatabaseAgent } from '../lib/database'
import { execSync } from 'child_process'
import Config from '../config'

/**
* 在 node_modules 中创建 云函数 sdk 包:@, 这个包是为了云函数IDE 加载类型提示文件而创建的,不可发布
Expand Down Expand Up @@ -96,4 +97,26 @@ export function moduleExists(mod: string) {
} catch (_err) {
return false
}
}

/**
* Create necessary indexes of collections
* @param data
* @returns
*/
export async function ensureCollectionIndexes(): Promise<any> {
const db = DatabaseAgent.db
await db.collection(Constants.function_log_collection)
.createIndexes([
{
key: { created_at: 1 },
expireAfterSeconds: Config.FUNCTION_LOG_EXPIRED_TIME
},
{
key: { requestId: 1 }
},
{ key: { func_id: 1 } }
])

return true
}
11 changes: 0 additions & 11 deletions packages/app-service/src/cloud-sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { AxiosStatic } from "axios"
import { Db, getDb } from "database-proxy"
import { FunctionContext } from "cloud-function-engine"
import { FileStorageInterface } from "../lib/storage/interface"
import * as mongodb from "mongodb"
import { DatabaseAgent } from "../lib/database"
import request from 'axios'
import { SchedulerInstance } from "../lib/scheduler"
import { getToken, parseToken } from "../lib/utils/token"
import { invokeInFunction } from "./invoke"
import { createFileStorage } from "../lib/storage"
import { CloudFunction } from "../lib/function"
import { WebSocket } from "ws"
import { WebSocketAgent } from "../lib/ws"
Expand All @@ -31,14 +29,6 @@ export interface CloudSdkInterface {
*/
fetch: AxiosStatic

/**
* Get a file storage manager
*
* @deprecated this is deprecated and will be removed in a future release
* @param bucket the bucket name, default is 'public'
*/
storage(bucket?: string): FileStorageInterface

/**
* Get a laf.js database-ql instance
*/
Expand Down Expand Up @@ -134,7 +124,6 @@ DatabaseAgent.accessor.ready.then(() => {
export function create() {
const cloud: CloudSdkInterface = {
database: () => getDb(DatabaseAgent.accessor),
storage: createFileStorage,
fetch: request,
invoke: invokeInFunction,
emit: (event: string, param: any) => SchedulerInstance.emit(event, param),
Expand Down
3 changes: 1 addition & 2 deletions packages/app-service/src/cloud-sdk/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getFunctionByName } from "../api/function"
import { addFunctionLog, getFunctionByName } from "../api/function"
import { FunctionContext } from "cloud-function-engine"
import { addFunctionLog } from "../api/function-log"
import { CloudFunction } from "../lib/function"


Expand Down
2 changes: 1 addition & 1 deletion packages/app-service/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepFreeze } from "./lib/utils/lang"
import { deepFreeze } from "./lib/utils"

/**
* Constants collection
Expand Down
4 changes: 3 additions & 1 deletion packages/app-service/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./api/init"
import { ensureCollectionIndexes, getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./api/init"
import { logger } from "./lib/logger"


Expand All @@ -24,6 +24,8 @@ async function main() {
logger.info(res)

initCloudSdkPackage()

await ensureCollectionIndexes()
} catch (error) {
logger.error(error)
return 1
Expand Down
4 changes: 1 addition & 3 deletions packages/app-service/src/lib/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<[email protected]>
* @Date: 2021-08-16 15:29:15
* @LastEditTime: 2021-11-03 17:10:04
* @LastEditTime: 2022-02-03 00:42:33
* @Description:
*/

Expand All @@ -10,7 +10,6 @@ import { MongoAccessor } from 'database-proxy'
import Config from '../../config'
import { createLogger, logger } from '../logger'
import * as mongodb_uri from 'mongodb-uri'
import { ensureCollectionIndexes } from '../../api/indexes'


/**
Expand Down Expand Up @@ -45,7 +44,6 @@ export class DatabaseAgent {
accessor.init()
.then(async () => {
logger.info('db connected')
await ensureCollectionIndexes()
})
.catch(error => {
logger.error(error)
Expand Down
4 changes: 2 additions & 2 deletions packages/app-service/src/lib/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* @Author: Maslow<[email protected]>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-11-09 20:02:51
* @LastEditTime: 2022-02-03 00:58:59
* @Description:
*/

import { getFunctionById } from "../../api/function"
import { addFunctionLog, CloudFunctionLogStruct } from "../../api/function-log"
import { addFunctionLog, CloudFunctionLogStruct } from "../../api/function"
import { TriggerScheduler } from "cloud-function-engine"
import { createLogger } from "../logger"
import assert = require("assert")
Expand Down
Loading

0 comments on commit c650bfe

Please sign in to comment.