mirror of https://github.com/muety/wakapi.git
feat: allow using mysql socket, fixes #433
This commit is contained in:
parent
9f38246fe2
commit
e7b6a87153
|
@ -4,8 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/muety/wakapi/utils"
|
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,6 +12,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/muety/wakapi/utils"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
|
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/gorilla/securecookie"
|
"github.com/gorilla/securecookie"
|
||||||
"github.com/jinzhu/configor"
|
"github.com/jinzhu/configor"
|
||||||
|
@ -96,6 +97,7 @@ type securityConfig struct {
|
||||||
|
|
||||||
type dbConfig struct {
|
type dbConfig struct {
|
||||||
Host string `env:"WAKAPI_DB_HOST"`
|
Host string `env:"WAKAPI_DB_HOST"`
|
||||||
|
Socket string `env:"WAKAPI_DB_SOCKET"`
|
||||||
Port uint `env:"WAKAPI_DB_PORT"`
|
Port uint `env:"WAKAPI_DB_PORT"`
|
||||||
User string `env:"WAKAPI_DB_USER"`
|
User string `env:"WAKAPI_DB_USER"`
|
||||||
Password string `env:"WAKAPI_DB_PASSWORD"`
|
Password string `env:"WAKAPI_DB_PASSWORD"`
|
||||||
|
|
|
@ -2,8 +2,9 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfig_IsDev(t *testing.T) {
|
func TestConfig_IsDev(t *testing.T) {
|
||||||
|
@ -37,6 +38,28 @@ func Test_mysqlConnectionString(t *testing.T) {
|
||||||
), mysqlConnectionString(c))
|
), mysqlConnectionString(c))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_mysqlConnectionStringSocket(t *testing.T) {
|
||||||
|
c := &dbConfig{
|
||||||
|
Socket: "/var/run/mysql.sock",
|
||||||
|
Port: 9999,
|
||||||
|
User: "test_user",
|
||||||
|
Password: "test_password",
|
||||||
|
Name: "test_name",
|
||||||
|
Dialect: "mysql",
|
||||||
|
Charset: "utf8mb4",
|
||||||
|
MaxConn: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, fmt.Sprintf(
|
||||||
|
"%s:%s@unix(%s)/%s?charset=utf8mb4&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
||||||
|
c.User,
|
||||||
|
c.Password,
|
||||||
|
c.Socket,
|
||||||
|
c.Name,
|
||||||
|
"Local",
|
||||||
|
), mysqlConnectionString(c))
|
||||||
|
}
|
||||||
|
|
||||||
func Test_postgresConnectionString(t *testing.T) {
|
func Test_postgresConnectionString(t *testing.T) {
|
||||||
c := &dbConfig{
|
c := &dbConfig{
|
||||||
Host: "test_host",
|
Host: "test_host",
|
||||||
|
|
12
config/db.go
12
config/db.go
|
@ -2,6 +2,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
|
@ -54,11 +55,16 @@ func (c *dbConfig) GetDialector() gorm.Dialector {
|
||||||
}
|
}
|
||||||
|
|
||||||
func mysqlConnectionString(config *dbConfig) string {
|
func mysqlConnectionString(config *dbConfig) string {
|
||||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
host := fmt.Sprintf("tcp(%s:%d)", config.Host, config.Port)
|
||||||
|
|
||||||
|
if config.Socket != "" {
|
||||||
|
host = fmt.Sprintf("unix(%s)", config.Socket)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s:%s@%s/%s?charset=%s&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
||||||
config.User,
|
config.User,
|
||||||
config.Password,
|
config.Password,
|
||||||
config.Host,
|
host,
|
||||||
config.Port,
|
|
||||||
config.Name,
|
config.Name,
|
||||||
config.Charset,
|
config.Charset,
|
||||||
"Local",
|
"Local",
|
||||||
|
|
Loading…
Reference in New Issue