Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/ceramic-cli/src/bin/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ program
.option('--pinning-store-path <url>', `The directory path used for pinning service. Defaults to WORKING_DIR/${DEFAULT_PINNING_STORE_PATH}`)
.option('--gateway', 'Makes read only endpoints available. It is disabled by default')
.option('--port <int>', 'Port daemon is availabe. Default is 7007')
.option('--debug', 'Enable debug logging level. Default is false')
.description('Start the daemon')
.action(async ({ ipfsApi, ethereumRpc, anchorServiceApi, validateDocs, pinning, stateStorePath, gateway, port }) => {
await CeramicCliUtils.createDaemon(ipfsApi, ethereumRpc, anchorServiceApi, validateDocs, pinning, stateStorePath, gateway, port)
.action(async ({ ipfsApi, ethereumRpc, anchorServiceApi, validateDocs, pinning, stateStorePath, gateway, port, debug }) => {
await CeramicCliUtils.createDaemon(ipfsApi, ethereumRpc, anchorServiceApi, validateDocs, pinning, stateStorePath, gateway, port, debug)
})

program
Expand Down
6 changes: 4 additions & 2 deletions packages/ceramic-cli/src/ceramic-cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export class CeramicCliUtils {
* @param stateStorePath - State store path
* @param gateway - read only endpoints available. It is disabled by default
* @param port - port daemon is availabe. Default is 7007
* @param debug - Enable debug logging level
*/
static async createDaemon(ipfsApi: string, ethereumRpc: string, anchorServiceApi: string, validateDocs: boolean, pinning: string[], stateStorePath: string, gateway: boolean, port: number): Promise<CeramicDaemon> {
static async createDaemon(ipfsApi: string, ethereumRpc: string, anchorServiceApi: string, validateDocs: boolean, pinning: string[], stateStorePath: string, gateway: boolean, port: number, debug: boolean): Promise<CeramicDaemon> {
if (stateStorePath == null) {
stateStorePath = DEFAULT_PINNING_STORE_PATH
}
Expand All @@ -62,7 +63,8 @@ export class CeramicCliUtils {
validateDocs,
pinning: pinning,
gateway,
port
port,
debug
}

if (ipfsApi) {
Expand Down
86 changes: 71 additions & 15 deletions packages/ceramic-cli/src/ceramic-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import ipfsClient from 'ipfs-http-client'
import express, { Request, Response, NextFunction } from 'express'
import Ceramic from '@ceramicnetwork/ceramic-core'
import type { CeramicConfig } from "@ceramicnetwork/ceramic-core";
import { DoctypeUtils } from "@ceramicnetwork/ceramic-common"
import { DoctypeUtils, DefaultLoggerFactory, Logger } from "@ceramicnetwork/ceramic-common"

const DEFAULT_PORT = 7007
const DEBUG = true
const toApiPath = (ending: string): string => '/api/v0' + ending

const DEFAULT_ANCHOR_SERVICE_URL = "https://cas.3box.io:8081/api/v0/requests"
Expand All @@ -23,22 +22,21 @@ interface CreateOpts {
validateDocs?: boolean;
pinning?: string[];
gateway?: boolean;
}

function logErrors (err: Error, req: Request, res: Response, next: NextFunction): void {
console.error(err)
next(err)
}

function sendErrorResponse(err: Error, req: Request, res: Response, next: NextFunction): void {
res.json({ error: err.message })
next(err)
debug: boolean;
}

class CeramicDaemon {
private server: any
private logger: Logger
private readonly debug: boolean

constructor (public ceramic: Ceramic, opts: CreateOpts) {
this.debug = opts.debug
this.logger = DefaultLoggerFactory.getLogger(CeramicDaemon.name)
if (this.debug) {
this.logger.setLevel('debug')
}

const app = express()
app.use(express.json())
app.use((req: Request, res: Response, next: NextFunction) => {
Expand All @@ -48,10 +46,68 @@ class CeramicDaemon {

this.registerAPIPaths(app, opts.gateway)

if (DEBUG) {
app.use(logErrors)
if (this.debug) {
app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
this.logger.error(err)
next(err)
})
app.use((req: Request, res: Response, next: NextFunction) => {
const requestStart = Date.now();
const { rawHeaders, httpVersion, method, socket, url } = req;
const { remoteAddress, remoteFamily } = socket;

this.logger.debug(
JSON.stringify({
timestamp: Date.now(),
rawHeaders,
httpVersion,
method,
remoteAddress,
remoteFamily,
url
})
);

let errorMessage: string = null;
let body: string | any = [];
req.on("data", chunk => {
body.push(chunk)
})
req.on("end", () => {
body = Buffer.concat(body)
body = body.toString()
});
req.on("error", error => {
errorMessage = error.message
});

res.on("finish", () => {
const { rawHeaders, httpVersion, method, socket, url } = req
const { remoteAddress, remoteFamily } = socket

this.logger.debug(
JSON.stringify({
timestamp: Date.now(),
processingTime: Date.now() - requestStart,
rawHeaders,
body,
errorMessage,
httpVersion,
method,
remoteAddress,
remoteFamily,
url
})
)
})

next()
})
}
app.use(sendErrorResponse)
app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
res.json({ error: err.message })
next(err)
})
const port = opts.port || DEFAULT_PORT
this.server = app.listen(port, () => {
console.log('Ceramic API running on port ' + port)
Expand Down
4 changes: 3 additions & 1 deletion packages/ceramic-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"cids": "^1.0.0",
"dag-jose": "^0.2.0",
"did-resolver": "^2.1.1",
"lodash.clonedeep": "^4.5.0"
"lodash.clonedeep": "^4.5.0",
"loglevel": "^1.7.0",
"loglevel-plugin-prefix": "^0.8.4"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
1 change: 1 addition & 0 deletions packages/ceramic-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './ceramic-api'
export * from './context'
export * from './doctype'
export * from './utils/doctype-utils'
export * from './logger-factory'
Loading