Add docker support
This commit is contained in:
@@ -27,6 +27,7 @@ import com.devsoap.dbt.services.LedgerService
|
||||
import com.devsoap.dbt.services.TransactionManagerService
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.google.inject.multibindings.Multibinder
|
||||
import groovy.json.JsonBuilder
|
||||
import groovy.util.logging.Slf4j
|
||||
import ratpack.guice.ConfigurableModule
|
||||
import ratpack.handling.HandlerDecorator
|
||||
@@ -48,7 +49,6 @@ class DBTModule extends ConfigurableModule<DBTConfig> {
|
||||
bind(ExecutorHandler)
|
||||
|
||||
bind(LedgerData)
|
||||
|
||||
bind(LedgerService)
|
||||
bind(TransactionManagerService)
|
||||
|
||||
@@ -70,7 +70,7 @@ class DBTModule extends ConfigurableModule<DBTConfig> {
|
||||
config.executor.remoteUrl = "http://localhost:${serverConfig.port}/${config.executor.path}"
|
||||
}
|
||||
if(!config.ledger.remoteUrl) {
|
||||
config.executor.remoteUrl = "http://localhost:${serverConfig.port}/${config.ledger.path}"
|
||||
config.ledger.remoteUrl = "http://localhost:${serverConfig.port}/${config.ledger.path}"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,15 +25,19 @@ import ratpack.groovy.handling.GroovyChainAction
|
||||
class ExecutorChainAction extends GroovyChainAction {
|
||||
|
||||
private final String executorPath
|
||||
private final boolean enabled
|
||||
|
||||
@Inject
|
||||
ExecutorChainAction(DBTConfig config) {
|
||||
executorPath = config.executor.path
|
||||
enabled = config.executor.enabled
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute() throws Exception {
|
||||
log.info("Registering executor at $executorPath")
|
||||
if(!enabled) return
|
||||
|
||||
log.info("Registering executor at /$executorPath")
|
||||
path(executorPath, ExecutorHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,19 @@ import ratpack.handling.Handlers
|
||||
class LedgerChainAction extends GroovyChainAction {
|
||||
|
||||
private final String ledgerPath
|
||||
private final boolean enabled
|
||||
|
||||
@Inject
|
||||
LedgerChainAction(DBTConfig config) {
|
||||
ledgerPath = config.ledger.path
|
||||
enabled = config.ledger.enabled
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute() throws Exception {
|
||||
log.info("Registering ledger at $ledgerPath")
|
||||
if(!enabled) return
|
||||
|
||||
log.info("Registering ledger at /$ledgerPath")
|
||||
path(ledgerPath, Handlers.chain(
|
||||
registry.get(LedgerGetTransactionHandler),
|
||||
registry.get(LedgerUpdateTransactionHandler),
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.devsoap.dbt.config
|
||||
|
||||
class ExecutorConfig {
|
||||
boolean enabled = true
|
||||
String path = 'executor'
|
||||
String remoteUrl
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.devsoap.dbt.config
|
||||
|
||||
class LedgerConfig {
|
||||
boolean enabled = true
|
||||
String path = 'ledger'
|
||||
String remoteUrl
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import groovy.util.logging.Slf4j
|
||||
import ratpack.exec.Promise
|
||||
import ratpack.handling.Context
|
||||
import ratpack.handling.Handler
|
||||
import ratpack.http.HttpMethod
|
||||
import ratpack.http.Status
|
||||
import ratpack.http.client.HttpClient
|
||||
import ratpack.jdbctx.Transaction
|
||||
@@ -46,32 +47,36 @@ class ExecutorHandler implements Handler {
|
||||
|
||||
@Override
|
||||
void handle(Context ctx) throws Exception {
|
||||
ctx.request.body.then { body ->
|
||||
def mapper = ctx.get(ObjectMapper)
|
||||
def ds = ctx.get(DataSource)
|
||||
def transaction = mapper.readValue(body.text, BlockTransaction)
|
||||
if(ctx.request.method == HttpMethod.POST) {
|
||||
ctx.request.body.then { body ->
|
||||
def mapper = ctx.get(ObjectMapper)
|
||||
def ds = ctx.get(DataSource)
|
||||
def transaction = mapper.readValue(body.text, BlockTransaction)
|
||||
|
||||
if(!validateChain(transaction)) {
|
||||
ctx.response.status(Status.of(400, 'Transaction chain invalid'))
|
||||
return
|
||||
}
|
||||
|
||||
executeCommands(ds, transaction).onError { e ->
|
||||
log.info("Sending rolled back transaction to ledger")
|
||||
println mapper.writeValueAsString(transaction)
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
ctx.error(e)
|
||||
if(!validateChain(transaction)) {
|
||||
ctx.response.status(Status.of(400, 'Transaction chain invalid'))
|
||||
return
|
||||
}
|
||||
}.then {
|
||||
log.info("Updating ledger with execution result")
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
ctx.response.send(mapper.writeValueAsString(transaction))
|
||||
|
||||
executeCommands(ds, transaction).onError { e ->
|
||||
log.info("Sending rolled back transaction to ledger")
|
||||
println mapper.writeValueAsString(transaction)
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
ctx.error(e)
|
||||
}
|
||||
}.then {
|
||||
log.info("Updating ledger with execution result")
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
ctx.response.send(mapper.writeValueAsString(transaction))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.next()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user