Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init repo structure, build steps, docker steps, etc #1

Merged
merged 4 commits into from
Oct 19, 2023
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
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
yarn-error.log
4 changes: 4 additions & 0 deletions .env-EXAMPLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AUTH_EMAIL=not-set
AUTH_PASS=not-set
MERCURY_KEY=not-set
MERCURY_URL=not-set
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
extends: ["@stellar/eslint-config"],
env: {
es2020: true,
},
globals: {},
ignorePatterns: ["dist/", "node_modules/", "build/"],
overrides: [
{
files: ["webpack.*.js"],
rules: {
"import/no-extraneous-dependencies": [0, { devDependencies: false }],
},
},
],
rules: {
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"no-console": "off",
"react/jsx-filename-extension": ["error", { extensions: [".tsx", ".jsx"] }],
},
settings: {
"import/resolver": {
typescript: {},
node: {
extensions: [".ts", ".tsx"],
moduleDirectory: ["node_modules", "src"],
},
},
},
};
14 changes: 14 additions & 0 deletions .github/workflows/runTests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Run Tests
on: [pull_request]
jobs:
test-ci:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
- run: yarn install
- run: yarn build:prod
- run: yarn test:ci
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
node_modules
build
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:18
MAINTAINER SDF Wallets Team <[email protected]>

RUN mkdir -p /app
WORKDIR /app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to install node and yarn and stuff here? example: https://github.com/stellar/laboratory/blob/master/Dockerfile#L11

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, using the node image so they come built in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah v cool 👍


COPY . /app/
RUN yarn
RUN yarn build:prod

EXPOSE 3002
CMD ["node", "build/index.js"]
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Check if we need to prepend docker commands with sudo
SUDO := $(shell docker version >/dev/null 2>&1 || echo "sudo")

# If LABEL is not provided set default value
LABEL ?= $(shell git rev-parse --short HEAD)$(and $(shell git status -s),-dirty-$(shell id -u -n))
# If TAG is not provided set default value
TAG ?= stellar/freighter-backend:$(LABEL)
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
BUILD_DATE := $(shell date -u +%FT%TZ)

docker-build:
$(SUDO) docker build --pull --label org.opencontainers.image.created="$(BUILD_DATE)" -t $(TAG) .

docker-push:
$(SUDO) docker push $(TAG)
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# freighter-backend
# Freighter-Backend
Freighter's indexer integration layer and general backend

## Prerequisites

You will need

- Node (>=18.0): https://nodejs.org/en/download/
- Yarn (>=v1.22.5): https://classic.yarnpkg.com/en/docs/install

## Development
`yarn i && yarn start`

## Production build
`yarn build:prod`
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "freighter-backend",
"version": "0.0.1",
"description": "Freighter's indexer integration layer and general backend",
"main": "index.js",
"scripts": {
"start": "npx ts-node --project tsconfig.json src/index.ts",
"build:prod": "webpack --mode production --config ./webpack.prod.js",
"prepare": "husky install",
"test": "jest",
"test:ci": "jest --ci"
},
"husky": {
"hooks": {
"pre-commit": "concurrently 'pretty-quick --staged' 'lint-staged'",
"post-merge": "yarn install-if-package-changed",
"post-checkout": "yarn install-if-package-changed"
}
},
"lint-staged": {
"src/**/*.ts?(x)": [
"eslint --fix --max-warnings 0"
]
},
"author": "[email protected]",
"license": "Apache-2.0",
"private": true,
"engines": {
"node": ">=18.12.0"
},
"dependencies": {
"@fastify/helmet": "^11.1.1",
"@urql/core": "^4.1.3",
"axios": "^1.5.1",
"dotenv-expand": "^10.0.0",
"fastify": "^4.23.2",
"pino": "^8.15.3",
"pino-pretty": "^10.2.0",
"soroban-client": "^1.0.0-beta.2",
"yargs": "^17.7.2"
},
"devDependencies": {
"@babel/preset-typescript": "^7.23.2",
"@stellar/tsconfig": "^1.0.2",
"@types/jest": "^29.5.6",
"@types/node": "^20.8.2",
"@types/yargs": "^17.0.26",
"babel-jest": "^29.7.0",
"dotenv": "^16.3.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.0.1",
"prettier": "^3.0.3",
"pretty-quick": "^3.1.3",
"ts-jest": "^29.1.1",
"ts-loader": "^9.4.4",
"typescript": "^5.2.2",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0"
}
}
27 changes: 27 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

export function buildConfig(config: Record<string, string>) {
if (!config.MERCURY_KEY) {
throw new Error('ENV configuration invalid - missing MERCURY_KEY')
}

if (!config.MERCURY_URL) {
throw new Error('ENV configuration invalid - missing MERCURY_URL')
}

if (!config.AUTH_EMAIL) {
throw new Error('ENV configuration invalid - missing AUTH_EMAIL')
}

if (!config.AUTH_PASS) {
throw new Error('ENV configuration invalid - missing AUTH_PASS')
}

return {
mercuryKey: config.MERCURY_KEY,
mercuryUrl: config.MERCURY_URL,
mercuryEmail: config.AUTH_EMAIL,
mercuryPassword: config.AUTH_PASS
}
}

export type Conf = ReturnType<typeof buildConfig>
61 changes: 61 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as dotEnv from 'dotenv'
import { expand } from 'dotenv-expand'

import { logger } from './logger'
import { buildConfig } from './config'

import yargs from 'yargs'

interface CliArgs {
env: string
port: number
}

async function main() {
const _config = dotEnv.config({ path: '.env' })
expand(_config)

const config = _config.parsed || {}
// @ts-ignore
const conf = buildConfig(config)

const argv = yargs(process.argv).options({
env: {
alias: 'e',
type: 'string',
description: 'env - production or development'
},
port: {
alias: 'p',
type: 'number',
description: 'port for server'
},
}).argv as CliArgs

// @ts-ignore
const env = argv.env || 'development'
// @ts-ignore
const port = argv.port || 3002

process.on('SIGTERM', () => {
process.exit(0)
})

process.on('SIGINT', function() {
process.exit(0)
})
}

process.on('uncaughtException', function (err) {
logger.error(err)
process.kill(process.pid, 'SIGTERM')
})

process.on('unhandledRejection', function (reason: string) {
logger.error(reason)
})

main().catch((e) => {
logger.error(e)
process.exit(1)
})
20 changes: 20 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pino from 'pino'

const logger = pino({
name: 'mercury-client-logger',
serializers: {
req: pino.stdSerializers.req,
err: pino.stdSerializers.err,
error: pino.stdSerializers.err,
},
transport: {
target: 'pino-pretty',
options: {
colorize: true
}
},
})

export {
logger
}
6 changes: 6 additions & 0 deletions src/stub.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

describe("Stub test", () => {
it("passes as placeholder", () => {
expect(true).toBeTruthy;
});
});
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"extends": "@stellar/tsconfig",
"compilerOptions": {
"importHelpers": false,
"target": "ESNEXT",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "commonjs",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
},
"include": [
"src/**/*"
],
"exclude": [
"build",
"node_modules"
],
"ts-node": {
"transpileOnly": true,
"files": true,
"compilerOptions": {
"rootDir": "."
}
}
}
25 changes: 25 additions & 0 deletions webpack.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path')
const nodeExternals = require('webpack-node-externals')
module.exports = {
entry: './src/index.ts',
mode: 'production',
target: 'node',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js'
},
resolve: {
extensions: ['.ts', '.js'],
},
externals: [ nodeExternals() ],
module: {
rules: [
{
test: /\.([cm]?ts)$/,
use: [
'ts-loader',
]
}
]
}
}
Loading
Loading