From e55adf62872c8c97e7440929c7c70ecabaf0193f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Wed, 17 Feb 2021 21:04:22 +0100 Subject: [PATCH] fix: mysql character encoding (resolve #131) --- README.md | 3 ++- config.default.yml | 1 + config/config.go | 4 +++- middlewares/logging.go | 2 +- scripts/push_sample_data.py | 2 +- version.txt | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ba410af..35c1560 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,8 @@ You can specify configuration options either via a config file (default: `config | `db.user` | `WAKAPI_DB_USER` | - | Database user | | `db.password` | `WAKAPI_DB_PASSWORD` | - | Database password | | `db.name` | `WAKAPI_DB_NAME` | `wakapi_db.db` | Database name | -| `db.dialect` | `WAKAPI_DB_TYPE` | `sqlite3` | Database type (one of `sqlite3`, `mysql`, `postgres`, `cockroach`) | +| `db.dialect` | `WAKAPI_DB_TYPE` | `sqlite3` | Database type (one of `sqlite3`, `mysql`, `postgres`, `cockroach`) | +| `db.charset` | `WAKAPI_DB_CHARSET` | `utf8mb4` | Database connection charset (for MySQL only) | | `db.max_conn` | `WAKAPI_DB_MAX_CONNECTIONS` | `2` | Maximum number of database connections | | `db.ssl` | `WAKAPI_DB_SSL` | `false` | Whether to use TLS encryption for database connection (Postgres and CockroachDB only) | diff --git a/config.default.yml b/config.default.yml index 6fbec91..81b3bed 100644 --- a/config.default.yml +++ b/config.default.yml @@ -22,6 +22,7 @@ db: password: # leave blank when using sqlite3 name: wakapi_db.db # database name for mysql / postgres or file path for sqlite (e.g. /tmp/wakapi.db) dialect: sqlite3 # mysql, postgres, sqlite3 + charset: utf8mb4 # only used for mysql connections max_conn: 2 # maximum number of concurrent connections to maintain ssl: false # whether to use tls for db connection (must be true for cockroachdb) (ignored for mysql and sqlite) diff --git a/config/config.go b/config/config.go index f3ce24d..62b0320 100644 --- a/config/config.go +++ b/config/config.go @@ -77,6 +77,7 @@ type dbConfig struct { Password string `env:"WAKAPI_DB_PASSWORD"` Name string `default:"wakapi_db.db" env:"WAKAPI_DB_NAME"` Dialect string `yaml:"-"` + Charset string `default:"utf8mb4" env:"WAKAPI_DB_CHARSET"` Type string `yaml:"dialect" default:"sqlite3" env:"WAKAPI_DB_TYPE"` MaxConn uint `yaml:"max_conn" default:"2" env:"WAKAPI_DB_MAX_CONNECTIONS"` Ssl bool `default:"false" env:"WAKAPI_DB_SSL"` @@ -181,12 +182,13 @@ func (c *dbConfig) GetDialector() gorm.Dialector { func mysqlConnectionString(config *dbConfig) string { //location, _ := time.LoadLocation("Local") - return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES", + return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES", config.User, config.Password, config.Host, config.Port, config.Name, + config.Charset, "Local", ) } diff --git a/middlewares/logging.go b/middlewares/logging.go index 65fcd3d..03c8019 100644 --- a/middlewares/logging.go +++ b/middlewares/logging.go @@ -43,7 +43,7 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { } lg.logFunc( - "[request] status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s, user=%s\n", + "[request] status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s", ww.Status(), r.Method, r.URL.String(), diff --git a/scripts/push_sample_data.py b/scripts/push_sample_data.py index 796d310..738cc8f 100644 --- a/scripts/push_sample_data.py +++ b/scripts/push_sample_data.py @@ -85,7 +85,7 @@ def post_data_sync(data: List[Heartbeat], url: str, api_key: str): def randomword(length: int) -> str: - letters = string.ascii_lowercase + letters = string.ascii_lowercase + 'äöü💩' # test utf8 and utf8mb4 characters as well return ''.join(random.choice(letters) for _ in range(length)) diff --git a/version.txt b/version.txt index ae96cc7..2f4320f 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.24.3 +1.24.4