Skip to content

Commit

Permalink
upgrade cost center config.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie committed May 28, 2024
1 parent f164e9d commit 33c70b8
Show file tree
Hide file tree
Showing 28 changed files with 240 additions and 127 deletions.
48 changes: 48 additions & 0 deletions controllers/go.work.sum

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions frontend/providers/costcenter/deploy/manifests/configmap.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: costcenter-frontend-config
namespace: costcenter-frontend
data:
config.yaml: |-
costCenter:
transferEnabled: true
currencyType: "shellCoin"
invoice:
enabled: false
feiShuBotURL: ""
aliSms:
endpoint: ""
accessKeyID: ""
accessKeySecret: ""
templateCode: ""
signName: ""
mongo:
uri: ""
recharge:
enabled: false
payMethods:
wechat:
enabled: false
stripe:
enabled: false
publicKey: ""
components:
accountService:
url: "http://account-service.account-system.svc:2333"
27 changes: 10 additions & 17 deletions frontend/providers/costcenter/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ metadata:
app: costcenter-frontend
name: costcenter-frontend
---
apiVersion: v1
kind: ConfigMap
metadata:
name: costcenter-frontend-config
namespace: costcenter-frontend
data:
config.yaml: |-
addr: :3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -35,13 +26,6 @@ spec:
spec:
containers:
- name: costcenter-frontend
env:
- name: TRANSFER_ENABLED
value: '{{ .transferEnabled }}'
- name: RECHARGE_ENABLED
value: '{{ .rechargeEnabled }}'
- name: BILLING_URI
value: 'http://account-service.account-system.svc:2333'
resources:
limits:
cpu: 1000m
Expand All @@ -61,8 +45,17 @@ spec:
imagePullPolicy: Always
volumeMounts:
- name: costcenter-frontend-volume
mountPath: /config.yaml
mountPath: /app/config.yaml
subPath: config.yaml
readinessProbe:
httpGet:
path: /api/platform/getAppConfig
port: 3000
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 2
successThreshold: 3
failureThreshold: 3
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ import {
createColumnHelper,
flexRender,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
HeaderContext,
RowData,
Table as TTable,
useReactTable
} from '@tanstack/react-table';
import { useMemo } from 'react';
import { enableGpu } from '@/service/enabled';
import Amount from '@/components/billing/AmountTableHeader';

export function CommonBillingTable({
Expand Down Expand Up @@ -161,7 +157,7 @@ export function CommonBillingTable({
}
})
];
}, [enableGpu, t, currency]);
}, [global.AppConfig.gpuEnabled, t, currency]);
const table = useReactTable({
data,
state: {
Expand Down Expand Up @@ -273,7 +269,7 @@ export function TransferBillingTable({ data }: { data: BillingItem[] }) {
}
})
];
}, [enableGpu, t, currency]);
}, [global.AppConfig.gpuEnabled, t, currency]);

const table = useReactTable({
data,
Expand Down Expand Up @@ -362,7 +358,7 @@ export function BillingDetailsTable({
cell: customCell(true)
})
];
}, [enableGpu, t, currency]);
}, [global.AppConfig.gpuEnabled, t, currency]);
const table = useReactTable({
data,
state: {
Expand Down
28 changes: 13 additions & 15 deletions frontend/providers/costcenter/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { EVENT_NAME } from 'sealos-desktop-sdk';
import '@/styles/globals.scss';
import { theme } from '@/styles/chakraTheme';
import { ChakraProvider } from '@chakra-ui/react';
// import { persistQueryClient, removeOldestQuery } from '@tanstack/react-query-persist-client';
// import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister';
import { Hydrate, QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { AppProps } from 'next/app';
import Router, { useRouter } from 'next/router';
Expand All @@ -14,13 +12,12 @@ import 'nprogress/nprogress.css';
import 'react-day-picker/dist/style.css';
import { appWithTranslation, i18n } from 'next-i18next';
import { useEffect } from 'react';
import { setCookie } from '@/utils/cookieUtils';
import request from '@/service/request';
import { EnvData } from '@/types/env';
import { ApiResp } from '@/types/api';
import { Response as initDataRes } from '@/pages/api/platform/getAppConfig';
import useEnvStore from '@/stores/env';

// Make sure to call `loadStripe` outside of a component’s render to avoid
// Make sure to call `loadStripe` outside a component’s render to avoid
// recreating the `Stripe` object on every render.
const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -56,20 +53,21 @@ const App = ({ Component, pageProps }: AppProps) => {
});
}
})();

(async () => {
try {
const { data } = await request<any, ApiResp<EnvData>>('/api/enabled');
state.setEnv('invoiceEnabled', !!data?.invoiceEnabled);
state.setEnv('transferEnabled', !!data?.transferEnabled);
state.setEnv('rechargeEnabled', !!data?.rechargeEnabled);
state.setEnv('currency', data?.currency || 'shellCoin');
state.setEnv('gpuEnabled', !!data?.gpuEnabled);
const stripeE = !!data?.stripeEnabled;
const { data } = await request<any, ApiResp<initDataRes>>('/api/platform/getInitData');
state.setEnv('invoiceEnabled', !!data?.INVOICE_ENABLED);
state.setEnv('transferEnabled', !!data?.TRANSFER_ENABLED);
state.setEnv('rechargeEnabled', !!data?.RECHARGE_ENABLED);
state.setEnv('currency', data?.CURRENCY || 'shellCoin');
state.setEnv('gpuEnabled', !!data?.GPU_ENABLED);
const stripeE = !!data?.STRIPE_ENABLED;
state.setEnv('stripeEnabled', stripeE);
stripeE && state.setStripe(data?.stripePub || '');
state.setEnv('wechatEnabled', !!data?.wechatEnabled);
stripeE && state.setStripe(data?.STRIPE_PUB || '');
state.setEnv('wechatEnabled', !!data?.WECHAT_ENABLED);
} catch (error) {
console.error('get env error');
console.error('get init config error');
}
})();
sealosApp.addAppEventListen(EVENT_NAME.CHANGE_I18N, changeI18n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const base = process.env['BILLING_URI'] as string;
const base = global.AppConfig.components.accountService.url as string;
if (!base) throw Error("can't ot get alapha1");
const kc = await authSession(req.headers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { generatePaymentCrd, PaymentForm } from '@/constants/payment';
import { authSession } from '@/service/backend/auth';
import { ApplyYaml, GetUserDefaultNameSpace } from '@/service/backend/kubernetes';
import { jsonRes } from '@/service/backend/response';
import { enableRecharge } from '@/service/enabled';
import crypto from 'crypto';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, resp: NextApiResponse) {
try {
if (!enableRecharge()) {
if (!global.AppConfig.recharge.enabled) {
throw new Error('recharge is not enabled');
}
if (req.method !== 'POST') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { paymentMeta } from '@/constants/payment';
import { authSession } from '@/service/backend/auth';
import { GetCRD, GetUserDefaultNameSpace } from '@/service/backend/kubernetes';
import { jsonRes } from '@/service/backend/response';
import { enableRecharge } from '@/service/enabled';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, resp: NextApiResponse) {
try {
if (!enableRecharge) {
if (!global.AppConfig.recharge.enabled) {
throw new Error('recharge is not enabled');
}
const kc = await authSession(req.headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { generatePaymentCrd, PaymentForm } from '@/constants/payment';
import { authSession } from '@/service/backend/auth';
import { ApplyYaml, GetUserDefaultNameSpace } from '@/service/backend/kubernetes';
import { jsonRes } from '@/service/backend/response';
import { enableRecharge } from '@/service/enabled';
import crypto from 'crypto';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, resp: NextApiResponse) {
try {
if (!enableRecharge()) {
if (!global.AppConfig.recharge.enabled) {
throw new Error('recharge is not enabled');
}
if (req.method !== 'POST') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { generatePaymentCrd, generateTransferCrd } from '@/constants/payment';
import { generateTransferCrd } from '@/constants/payment';
import { authSession } from '@/service/backend/auth';
import {
ApplyYaml,
GetUserDefaultNameSpace,
watchClusterObject
} from '@/service/backend/kubernetes';
import { jsonRes } from '@/service/backend/response';
import { enableTransfer } from '@/service/enabled';
import { TransferState } from '@/types/Transfer';
import { getTime } from 'date-fns';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, resp: NextApiResponse) {
try {
if (!enableTransfer()) {
if (!global.AppConfig.transferEnabled) {
throw new Error('transfer is not enabled');
}
if (req.method !== 'POST') {
Expand Down
5 changes: 3 additions & 2 deletions frontend/providers/costcenter/src/pages/api/billing/buget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export default async function handler(req: NextApiRequest, resp: NextApiResponse
code: 400,
message: 'startTime is invalid'
});
const consumptionUrl = process.env.BILLING_URI + '/account/v1alpha1/costs/consumption';
const rechagreUrl = process.env.BILLING_URI + '/account/v1alpha1/costs/recharge';
const base = global.AppConfig.components.accountService.url as string;
const consumptionUrl = base + '/account/v1alpha1/costs/consumption';
const rechagreUrl = base + '/account/v1alpha1/costs/recharge';

const results = await Promise.all([
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function handler(req: NextApiRequest, resp: NextApiResponse
code: 400,
message: 'endTime is invalid'
});
const url = process.env.BILLING_URI + '/account/v1alpha1/costs';
const url = global.AppConfig.components.accountService.url + '/account/v1alpha1/costs';
const res = await (
await fetch(url, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function handler(req: NextApiRequest, resp: NextApiResponse
code: 400,
message: 'endTime is invalid'
});
const url = process.env.BILLING_URI + '/account/v1alpha1/namespaces';
const url = global.AppConfig.components.accountService.url + '/account/v1alpha1/namespaces';
const res = await (
await fetch(url, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default async function handler(req: NextApiRequest, resp: NextApiResponse
code: 400,
message: 'endTime is invalid'
});
const url = process.env.BILLING_URI + '/account/v1alpha1/costs/properties';
const url =
global.AppConfig.components.accountService.url + '/account/v1alpha1/costs/properties';
const res = (await (
await fetch(url, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default async function handler(req: NextApiRequest, resp: NextApiResponse
startTime
};

const url = process.env.BILLING_URI + '/account/v1alpha1/payment';
const url = global.AppConfig.components.accountService.url + '/account/v1alpha1/payment';
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data)
Expand Down
35 changes: 0 additions & 35 deletions frontend/providers/costcenter/src/pages/api/enabled.ts

This file was deleted.

17 changes: 11 additions & 6 deletions frontend/providers/costcenter/src/pages/api/invoice/sms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { jsonRes } from '@/service/backend/response';
import { addOrUpdateCode, checkSendable } from '@/service/backend/db/verifyCode';
import { getClientIPFromRequest, retrySerially } from '@/utils/tools';
import { authSession } from '@/service/backend/auth';
import { enableInvoice } from '@/service/enabled';
import * as process from 'process';
const accessKeyId = process.env.ALI_ACCESS_KEY_ID;
const accessKeySecret = process.env.ALI_ACCESS_KEY_SECRET;
const templateCode = process.env.ALI_TEMPLATE_CODE;
const signName = process.env.ALI_SIGN_NAME;
const requestTimestamps: Record<string, number> = {};

function checkRequestFrequency(ipAddress: string) {
const accessKeyId = global.AppConfig.invoice.aliSms.accessKeyID;
const accessKeySecret = global.AppConfig.invoice.aliSms.accessKeySecret;
const templateCode = global.AppConfig.invoice.aliSms.templateCode;
const signName = global.AppConfig.invoice.aliSms.signName;
const currentTime = Date.now();
const lastRequestTime = requestTimestamps[ipAddress] || 0;
const timeDiff = currentTime - lastRequestTime;
Expand All @@ -27,9 +27,14 @@ function checkRequestFrequency(ipAddress: string) {
return true;
}
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const accessKeyId = global.AppConfig.invoice.aliSms.accessKeyID;
const accessKeySecret = global.AppConfig.invoice.aliSms.accessKeySecret;
const templateCode = global.AppConfig.invoice.aliSms.templateCode;
const signName = global.AppConfig.invoice.aliSms.signName;
try {
if (!enableInvoice()) {
if (!global.AppConfig.invoice.enabled) {
throw new Error('invoice is not enabled');
}
const kc = await authSession(req.headers);
Expand Down
Loading

0 comments on commit 33c70b8

Please sign in to comment.