test connection

This commit is contained in:
Jan Prochazka 2020-01-01 12:48:47 +01:00
parent 7b7aaa8e0f
commit ae3bb23d4c
8 changed files with 1164 additions and 46 deletions

View File

@ -1,17 +1,18 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
};
env: {
browser: false,
commonjs: true,
es6: true,
},
extends: ['eslint:recommended', 'node'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
},
rules: {
'no-unused-vars': 'warn',
},
};

View File

@ -4,13 +4,17 @@
"private": true,
"dependencies": {
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"cross-env": "^6.0.3",
"eslint": "^6.8.0",
"express": "^4.17.1"
"express": "^4.17.1",
"mssql": "^6.0.1"
},
"scripts": {
"start": "node src/index.js"
"start": "nodemon src/index.js"
},
"devDependencies": {}
"devDependencies": {
"nodemon": "^2.0.2"
}
}

13
api/src/connectProcess.js Normal file
View File

@ -0,0 +1,13 @@
const mssql = require('mssql');
process.on('message', async connection => {
const { server, port, user, password } = connection;
try {
const pool = await mssql.connect({ server, port, user, password });
const resp = await pool.request().query('SELECT @@VERSION AS version');
const { version } = resp.recordset[0];
process.send({ version });
} catch (e) {
process.send({ error: e.message });
}
});

23
api/src/connection.js Normal file
View File

@ -0,0 +1,23 @@
const express = require('express');
const router = express.Router();
const { fork } = require('child_process');
router.post('/test', async (req, res) => {
const subprocess = fork(`${__dirname}/connectProcess.js`);
subprocess.send(req.body);
subprocess.on('message', resp => res.json(resp));
// const { server, port, user, password } = req.body;
// let pool;
// try {
// pool = await mssql.connect({ server, port, user, password });
// const resp = await pool.request().query('SELECT @@VERSION AS version');
// const { version } = resp.recordset[0];
// res.json({ version });
// } catch (e) {
// res.json({ error: e.message });
// }
// if (pool) await pool.close();
});
module.exports = router;

View File

@ -1,12 +1,16 @@
const express = require("express");
const cors = require("cors");
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const connection = require('./connection');
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get("/", function(req, res) {
// res.json({msg: 'This is CORS-enabled for all origins!'})
res.send("Hello World");
app.get('/', (req, res) => {
res.send('DbGate API');
});
app.use('/connection', connection);
app.listen(3000);

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,9 @@
import React from "react";
import useFetch from "./useFetch";
import "./index.css";
import Screen from "./Screen";
import {CurrentWidgetProvider} from "./widgets/useCurrentWidget";
function App() {
const resp = useFetch("http://localhost:3000", {});
console.log("FETCH data", resp.data);
return (
<CurrentWidgetProvider>
<Screen />

View File

@ -1,4 +1,5 @@
import React from 'react';
import axios from 'axios';
import ModalBase from './ModalBase';
import { FormRow, FormLabel, FormValue, FormTextField, FormSubmit } from '../utility/forms';
import { TextField } from '../utility/inputs';
@ -6,9 +7,16 @@ import { Formik, Form } from 'formik';
// import FormikForm from '../utility/FormikForm';
export default function ConnectionModal({ modalState }) {
const handleSubmit = values => {
console.log(values);
modalState.close();
const [sqlConnectResult, setSqlConnectResult] = React.useState('Not connected');
const handleSubmit = async values => {
const resp = await axios.post('http://localhost:3000/connection/test', values);
console.log('resp.data', resp.data);
const { error, version } = resp.data;
setSqlConnectResult(error || version);
// modalState.close();
};
return (
<ModalBase modalState={modalState}>
@ -24,6 +32,7 @@ export default function ConnectionModal({ modalState }) {
<FormSubmit />
</Form>
</Formik>
<div>Connect result: {sqlConnectResult}</div>
</ModalBase>
);
}