Skip to content

Instantly share code, notes, and snippets.

View sonus21's full-sized avatar

Sonu Kumar sonus21

View GitHub Profile
@sonus21
sonus21 / vies.py
Last active April 9, 2023 14:15
views.py
@require_GET
def order_detail(request, id):
order = get_object_or_404(Order, id=id)
return JsonResponse(model_to_dict(order), safe=False)
@require_POST
@csrf_exempt
def create_order(request):
form = CreateOrder(json.loads(request.body))
package database
const (
Primary = "primary"
Secondary = "secondary"
)
public class Constants {
public static final String PRIMARY = "primary";
public static final String SECONDARY = "secondary";
}
@sonus21
sonus21 / OrderController.java
Created April 9, 2023 10:53
OrderController
@RequestMapping("/api/v1/orders")
@RestController
@Database(Constants.SECONDARY)
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@sonus21
sonus21 / SqlDatabaseConfiguration.java
Created April 9, 2023 10:52
SqlDatabaseConfiguration
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.github.sonus21.readwrite.repositories"})
@EntityScan(basePackages = "com.github.sonus21.readwrite.models.entities")
public class SqlDatabaseConfiguration {
private final DataSourceSecondaryConfig dataSourceSecondaryConfig;
private final DataSourcePrimaryConfig dataSourcePrimaryConfig;
@Autowired
@sonus21
sonus21 / DatabaseRouter.java
Created April 9, 2023 10:51
Database router
public class DatabaseRouter extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContext.get();
}
}
@sonus21
sonus21 / DatabaseContext.java
Created April 9, 2023 10:51
Database Context holder
public class DatabaseContext {
private static final ThreadLocal<String> databaseName = new ThreadLocal<>();
public static void set(String name) {
databaseName.set(name);
}
public static void clear() {
databaseName.remove();
}
@sonus21
sonus21 / DatabaseAspect.java
Last active April 9, 2023 15:06
Database Aspect
@Aspect
@Component
public class DatabaseAspect {
/**
* AOP joint point handler that will be invoked by AOP when it encounters a method annotated with Database
* or the class containing this method is annotated with Database.
*
* @param jointPoint joint point object
* @return the result of joint point
* @throws Throwable if any exception occurred while handling joint point
@sonus21
sonus21 / Database.java
Last active April 9, 2023 15:07
Database annotation
/**
* A Database annotation that should be used at controller,worker,consumer,methods or classes
* to route the database traffic to appropriate node.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Database {
String value() default "";
}
@sonus21
sonus21 / main.go
Last active April 9, 2023 11:03
Main package
func getPrimaryDbConfig() database.MySqlConfig {
// use any other mechanism like viper/toml/env file to create the cpnfig
return database.MySqlConfig{
Host: "localhost",
Port: 3306,
Database: "my_app_db",
Username: "my_app",
Password: "my_app_pass",
MaxConnectionRetries: 3,
MaxOpenConnection: 30,