This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func doesOrderExist(ctx context.Context, db *sql.DB, req *OrderCreateRequest) (bool, error) { | |
cntResult := db.QueryRowContext(ctx, "SELECT COUNT(id) from orders WHERE merchant_id=? AND merchant_order_id=?", req.MerchantId, req.MerchantOrderId) | |
err := cntResult.Err() | |
if err != nil { | |
return false, err | |
} | |
var count int | |
err = cntResult.Scan(&count) | |
if err != nil { | |
return false, err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// determineDbName this method is used to determine the actual database name from request method and path | |
// here more complex logic can be placed like for this path, path regex, header parameters etc | |
func determineDbName(method, path string, header http.Header) string { | |
if method == http.MethodGet { | |
return Secondary | |
} | |
// add any other logic | |
return Primary | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Middleware a middleware that is used in chi to set the database context | |
func Middleware() func(next http.Handler) http.Handler { | |
return func(next http.Handler) http.Handler { | |
fn := func(w http.ResponseWriter, r *http.Request) { | |
// identify database name from request | |
name := dbNameFromRequest(r) | |
// set db name in the context | |
ctx := SetDb(r.Context(), name) | |
// call next handler with database context | |
next.ServeHTTP(w, r.WithContext(ctx)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
_dbCtxKey = "DBCtx" | |
) | |
var ( | |
_dbs map[string]*sql.DB | |
_defaultDB string | |
) | |
// Init initialize the database context internal state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MySqlConfig this is used to store the different MySQL configuration | |
type MySqlConfig struct { | |
Host string | |
Port int | |
Database string | |
Username string | |
Password string | |
MaxConnectionRetries int | |
MaxOpenConnection int | |
MaxIdleConnection int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .constants import SECONDARY, PRIMARY | |
from CoreApp.middlewares import db_wrapper | |
@db_wrapper(PRIMARY) | |
def reserve_driver(request): | |
# logic goes here | |
pass | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from .utils import db_from_the_request | |
from .db_router import set_db_for_router, clear_db_for_router | |
class DatabaseMiddleware(object): | |
""" | |
DatabaseMiddleware sets the db in the thread local context and used | |
across multiple calls to handle the DB calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS = [ | |
# ... | |
"CoreApp" | |
# ... | |
] | |
MIDDLEWARE = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
threadLocal = threading.local() | |
def get_current_db_name(): | |
return getattr(threadLocal, "DB", None) | |
def set_db_for_router(db): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _determine_db_name(method, path): | |
# If you have multiple read databases, you can use DNS to direct them to one database. | |
# Alternatively, you can randomly select a database from the list if you prefer not to | |
# use DNS. | |
if method in ["GET"]: | |
return SECONDARY | |
# in many cases POST method can be read only as well | |
# and in some cases GET method can be performing CUD operations as well | |
# in these cases we can add a check on the path | |
return PRIMARY |