Skip to content

Commit

Permalink
feat: CLI create-config
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Oct 14, 2020
1 parent d20ccf4 commit ec96204
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 deletions.
20 changes: 20 additions & 0 deletions packages/daf-cli/default/client.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
server:
hostname: localhost
port: 3332
schemaPath: /open-api.json
apiBasePath: /agent
apiDocsPath: /api-docs
defaultIdentity:
create: true
messagingServiceEndpoint: /messaging
publicProfileServiceEndpoint: /public-profile
publicName: Alice
publicPicture: https://picsum.photos/200
ngrok:
connect: true
# authtoken: XZY
# subdomain: alice-did
# region: eu
# exposedMethods:
# - resolveDid
# - dataStoreORMGetIdentities
agent:
$require: daf-core#Agent
$args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ server:
publicPicture: https://picsum.photos/200
ngrok:
connect: true
# authtoken: XZY
# subdomain: alice-did
# region: eu
# exposedMethods:
Expand Down
62 changes: 30 additions & 32 deletions packages/daf-cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ngrok from 'ngrok'
import parse from 'url-parse'
import { AgentRouter } from 'daf-express'
import { getOpenApiSchema } from 'daf-rest'
import swaggerUi from "swagger-ui-express";
import swaggerUi from 'swagger-ui-express'
import { getAgent, getConfig } from './setup'

program
Expand All @@ -15,9 +15,7 @@ program
const agent = getAgent(program.config)
const { server: options } = getConfig(program.config)

const exposedMethods = options.exposedMethods
? options.exposedMethods
: agent.availableMethods()
const exposedMethods = options.exposedMethods ? options.exposedMethods : agent.availableMethods()

const apiBasePath = options.apiBasePath

Expand All @@ -30,8 +28,6 @@ program

app.use(apiBasePath, agentRouter)



app.listen(options.port, async () => {
console.log(`🚀 Agent server ready at http://localhost:${options.port}`)
console.log('🧩 Available methods', JSON.stringify(agent.availableMethods()))
Expand All @@ -46,24 +42,20 @@ program
addr: options.port,
subdomain: options.ngrok.subdomain,
region: options.ngrok.region,
authtoken: options.ngrok.authtoken,
})
hostname = parse(baseUrl).hostname
}

const openApiSchema = getOpenApiSchema(agent, apiBasePath, exposedMethods)
openApiSchema.servers = [
{ url: baseUrl }
]

app.use(
options.apiDocsPath,
swaggerUi.serve,
swaggerUi.setup(openApiSchema)
)
openApiSchema.servers = [{ url: baseUrl }]

app.use(options.apiDocsPath, swaggerUi.serve, swaggerUi.setup(openApiSchema))
console.log('📖 API Documentation', baseUrl + options.apiDocsPath)

app.get(options.schemaPath, (req, res) => { res.json(openApiSchema) })


app.get(options.schemaPath, (req, res) => {
res.json(openApiSchema)
})

console.log('🗺 OpenAPI schema', baseUrl + options.schemaPath)

Expand All @@ -87,16 +79,20 @@ program
})
console.log('📨 Messaging endpoint', messagingServiceEndpoint)

app.post(options.defaultIdentity.messagingServiceEndpoint, express.text({ type: '*/*' }), async (req, res) => {
try {
const message = await agent.handleMessage({ raw: req.body, save: true })
console.log('Received message', message.type, message.id)
res.json({ id: message.id })
} catch (e) {
console.log(e)
res.send(e.message)
}
})
app.post(
options.defaultIdentity.messagingServiceEndpoint,
express.text({ type: '*/*' }),
async (req, res) => {
try {
const message = await agent.handleMessage({ raw: req.body, save: true })
console.log('Received message', message.type, message.id)
res.json({ id: message.id })
} catch (e) {
console.log(e)
res.send(e.message)
}
},
)

const publicProfileServiceEndpoint = baseUrl + options.defaultIdentity.publicProfileServiceEndpoint

Expand Down Expand Up @@ -192,11 +188,13 @@ program

app.get('/', (req, res) => {
const links = [
{ label: "API Docs", url: options.apiDocsPath},
{ label: "API Schema", url: options.apiBasePath},
{ label: 'API Docs', url: options.apiDocsPath },
{ label: 'API Schema', url: options.apiBasePath },
]

const html = `<html><head><title>DID Agent</title></head><body>${links.map(l=>`<a href="${l.url}">${l.label}</a>`).join('<br/>')}</body></html>`

const html = `<html><head><title>DID Agent</title></head><body>${links
.map((l) => `<a href="${l.url}">${l.label}</a>`)
.join('<br/>')}</body></html>`
res.send(html)
})
}
Expand Down
45 changes: 37 additions & 8 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,39 @@ import { createAgentFromConfig } from './lib/agentCreator'
const defaultConfig = './agent.yml'
program.option('--config <path>', 'Configuration file', defaultConfig)

export const getConfig = (fileName: string): any => {
if (!fs.existsSync(dirname(fileName))) {
fs.mkdirSync(dirname(fileName))
}
program
.command('create-config')
.description('Create default agent config')
.option('--filename <string>', 'Config file name', './agent.yml')
.option('--template <string>', 'Use template (default,client)', 'default')

.action(async (options) => {
const { filename, template } = options

const templateFile = __dirname + '/../default/' + template + '.yml'
if (!fs.existsSync(templateFile)) {
console.log('Template not available: ' + template)
process.exit(1)
}

if (!fs.existsSync(dirname(filename))) {
fs.mkdirSync(dirname(filename))
}

if (!fs.existsSync(filename)) {
console.log('Creating: ' + filename)
const contents = fs.readFileSync(templateFile)
fs.writeFileSync(filename, contents)
} else {
console.log('File already exists: ' + filename)
}
})

export const getConfig = (fileName: string): any => {
if (!fs.existsSync(fileName)) {
console.log('Config file does not exist. Creating: ' + fileName)
const contents = fs.readFileSync(__dirname + '/../default/config.yml')
fs.writeFileSync(fileName, contents)
console.log('Config file not found: ' + fileName)
console.log('Use "daf create-config" to create one')
process.exit(1)
}

const config = yaml.parse(fs.readFileSync(fileName).toString())
Expand All @@ -41,5 +65,10 @@ export type EnabledInterfaces = IIdentityManager &
export type ConfiguredAgent = TAgent<EnabledInterfaces>

export function getAgent(fileName: string) {
return createAgentFromConfig<EnabledInterfaces>(getConfig(fileName))
try {
return createAgentFromConfig<EnabledInterfaces>(getConfig(fileName))
} catch (e) {
console.log('Unable to create agent from ' + fileName + '.', e.message)
process.exit(1)
}
}

0 comments on commit ec96204

Please sign in to comment.