fix db driver setup

This commit is contained in:
c9s 2021-02-17 14:57:29 +08:00
parent d32d2b97c7
commit a1cb3859c3
4 changed files with 32 additions and 22 deletions

View File

@ -8,14 +8,14 @@ export function ping(cb) {
});
}
export function testDatabaseConnection(dsn, cb) {
return axios.post(baseURL + '/api/setup/test-db', {dsn: dsn}).then(response => {
export function testDatabaseConnection(params, cb) {
return axios.post(baseURL + '/api/setup/test-db', params).then(response => {
cb(response.data)
});
}
export function configureDatabase(dsn, cb) {
return axios.post(baseURL + '/api/setup/configure-db', {dsn: dsn}).then(response => {
export function configureDatabase(params, cb) {
return axios.post(baseURL + '/api/setup/configure-db', params).then(response => {
cb(response.data)
});
}

View File

@ -45,12 +45,16 @@ export default function ConfigureDatabaseForm({onConfigured}) {
const [testResponse, setTestResponse] = React.useState(null);
const [configured, setConfigured] = React.useState(false);
const getDSN = () => driver === "sqlite3" ? "bbgo.sqlite3" : mysqlURL
const resetTestResponse = () => {
setTestResponse(null)
}
const handleConfigureDatabase = (event) => {
configureDatabase(mysqlURL, (response) => {
const dsn = getDSN()
configureDatabase({driver, dsn}, (response) => {
console.log(response);
setTesting(false);
setTestResponse(response);
@ -67,8 +71,10 @@ export default function ConfigureDatabaseForm({onConfigured}) {
}
const handleTestConnection = (event) => {
const dsn = getDSN()
setTesting(true);
testDatabaseConnection(mysqlURL, (response) => {
testDatabaseConnection({driver, dsn}, (response) => {
console.log(response)
setTesting(false)
setTestResponse(response)

View File

@ -1,2 +0,0 @@
package bbgo

View File

@ -16,7 +16,8 @@ import (
func (s *Server) setupTestDB(c *gin.Context) {
payload := struct {
DSN string `json:"dsn"`
Driver string `json:"driver"`
DSN string `json:"dsn"`
}{}
if err := c.BindJSON(&payload); err != nil {
@ -24,13 +25,17 @@ func (s *Server) setupTestDB(c *gin.Context) {
return
}
dsn := payload.DSN
if len(dsn) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
if len(payload.Driver) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing driver parameter"})
return
}
db, err := sql.Open("mysql", dsn)
if len(payload.DSN) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn parameter"})
return
}
db, err := sql.Open(payload.Driver, payload.DSN)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@ -50,19 +55,20 @@ func (s *Server) setupConfigureDB(c *gin.Context) {
}{}
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
c.JSON(http.StatusBadRequest, gin.H{"error": "missing parameters"})
return
}
if len(payload.Driver) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing driver parameter"})
return
}
if len(payload.DSN) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn parameter"})
return
}
if payload.Driver == "" {
payload.Driver = "mysql"
}
if err := s.Environ.ConfigureDatabase(c, payload.Driver, payload.DSN); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@ -70,8 +76,8 @@ func (s *Server) setupConfigureDB(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"driver": payload.Driver,
"dsn": payload.DSN,
"driver": payload.Driver,
"dsn": payload.DSN,
})
}
@ -140,7 +146,7 @@ func (s *Server) setupRestart(c *gin.Context) {
envVars := os.Environ()
logrus.Infof("%s %v %+v", bin, args, envVars)
logrus.Infof("exec %s %v", bin, args)
if err := syscall.Exec(bin, args, envVars); err != nil {
logrus.WithError(err).Errorf("failed to restart %s", bin)