Skip to content

Commit

Permalink
connect with clientside id (#681)
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <jiangruiyi@gmail.com>
  • Loading branch information
shanghaikid authored Nov 7, 2024
1 parent 9ba7513 commit abb0d09
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
12 changes: 9 additions & 3 deletions client/src/context/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const authContext = createContext<AuthContextType>({
token: '',
database: '',
checkHealth: true,
clientId: '',
},
setAuthReq: () => {},
isManaged: false,
Expand All @@ -31,6 +32,7 @@ export const authContext = createContext<AuthContextType>({
const { Provider } = authContext;
export const AuthProvider = (props: { children: React.ReactNode }) => {
// get data from local storage
const localClientId = window.localStorage.getItem(MILVUS_CLIENT_ID) || '';
const localAuthReq = JSON.parse(
window.localStorage.getItem(ATTU_AUTH_REQ) ||
JSON.stringify({
Expand All @@ -40,14 +42,13 @@ export const AuthProvider = (props: { children: React.ReactNode }) => {
token: '',
database: MILVUS_DATABASE,
checkHealth: true,
clientId: localClientId,
})
);

// state
const [authReq, setAuthReq] = useState<AuthReq>(localAuthReq);
const [clientId, setClientId] = useState<string>(
window.localStorage.getItem(MILVUS_CLIENT_ID) || ''
);
const [clientId, setClientId] = useState<string>(localClientId);

// update local storage when authReq changes
useEffect(() => {
Expand All @@ -62,12 +63,17 @@ export const AuthProvider = (props: { children: React.ReactNode }) => {

// login API
const login = async (params: AuthReq) => {
// create a new client id
params.clientId = Math.random().toString(36).substring(16);
// connect to Milvus
const res = await MilvusService.connect(params);
// update auth request
setAuthReq({ ...params, database: res.database });
setClientId(res.clientId);

// save clientId to local storage
window.localStorage.setItem(MILVUS_CLIENT_ID, res.clientId);

return res;
};
// logout API
Expand Down
13 changes: 4 additions & 9 deletions client/src/pages/connect/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import { useFormValidation } from '@/hooks';
import { formatForm } from '@/utils';
import { useNavigate } from 'react-router-dom';
import { rootContext, authContext, dataContext } from '@/context';
import {
MILVUS_CLIENT_ID,
ATTU_AUTH_HISTORY,
MILVUS_DATABASE,
MILVUS_URL,
} from '@/consts';
import { ATTU_AUTH_HISTORY, MILVUS_DATABASE, MILVUS_URL } from '@/consts';
import { CustomRadio } from '@/components/customRadio/CustomRadio';
import Icons from '@/components/icons/Icons';
import CustomToolTip from '@/components/customToolTip/CustomToolTip';
Expand All @@ -32,6 +27,7 @@ const DEFAULT_CONNECTION = {
password: '',
checkHealth: true,
time: -1,
clientId: '',
};

export const AuthForm = () => {
Expand Down Expand Up @@ -108,14 +104,13 @@ export const AuthForm = () => {

try {
// login
const result = await login(authReq);
await login(authReq);

// set database
setDatabase(authReq.database);
// success message
openSnackBar(successTrans('connect'));
// save clientId to local storage
window.localStorage.setItem(MILVUS_CLIENT_ID, result.clientId);

// get connection history
const history = JSON.parse(
window.localStorage.getItem(ATTU_AUTH_HISTORY) || '[]'
Expand Down
12 changes: 10 additions & 2 deletions server/src/milvus/milvus.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ export class MilvusController {
}

async connectMilvus(req: Request, res: Response, next: NextFunction) {
const { address, username, password, database, token, checkHealth } =
req.body;
const {
address,
username,
password,
database,
token,
checkHealth,
clientId,
} = req.body;
try {
const result = await this.milvusService.connectMilvus({
address,
Expand All @@ -51,6 +58,7 @@ export class MilvusController {
password,
database,
checkHealth,
clientId,
});

res.send(result);
Expand Down
14 changes: 13 additions & 1 deletion server/src/milvus/milvus.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ export class MilvusService {

async connectMilvus(data: AuthReq): Promise<AuthObject> {
// Destructure the data object to get the connection details
const { address, token, username, password, database, checkHealth } = data;
const {
address,
token,
username,
password,
database,
checkHealth,
clientId,
} = data;
// Format the address to remove the http prefix
const milvusAddress = MilvusService.formatAddress(address);

Expand All @@ -39,6 +47,10 @@ export class MilvusService {
password,
logLevel: process.env.ATTU_LOG_LEVEL || 'info',
database: database || this.DEFAULT_DATABASE,
id: clientId,
pool: {
max: 10,
},
};

if (process.env.ROOT_CERT_PATH) {
Expand Down
1 change: 1 addition & 0 deletions server/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type AuthReq = {
token: string;
database: string;
checkHealth: boolean;
clientId: string;
};

export type AuthObject = {
Expand Down

0 comments on commit abb0d09

Please sign in to comment.