Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitthi committed Dec 1, 2023
1 parent 0f03c8a commit 2481d24
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 246 deletions.
134 changes: 66 additions & 68 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ env:
'on':
push:
branches:
- main
- release
tags-ignore:
- '**'
paths-ignore:
Expand All @@ -25,55 +25,55 @@ jobs:
fail-fast: false
matrix:
settings:
- host: macos-latest
target: x86_64-apple-darwin
build: |
yarn build
strip -x *.node
# - host: macos-latest
# target: x86_64-apple-darwin
# build: |
# pnpm build
# strip -x *.node
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian
build: |-
set -e &&
yarn build --target x86_64-unknown-linux-gnu &&
pnpm build --target x86_64-unknown-linux-gnu &&
strip *.node
- host: ubuntu-latest
target: x86_64-unknown-linux-musl
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
build: set -e && yarn build && strip *.node
- host: macos-latest
target: aarch64-apple-darwin
build: |
sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*;
export CC=$(xcrun -f clang);
export CXX=$(xcrun -f clang++);
SYSROOT=$(xcrun --sdk macosx --show-sdk-path);
export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT";
yarn build --target aarch64-apple-darwin
strip -x *.node
- host: ubuntu-latest
target: aarch64-unknown-linux-gnu
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64
build: |-
set -e &&
yarn build --target aarch64-unknown-linux-gnu &&
aarch64-unknown-linux-gnu-strip *.node
- host: ubuntu-latest
target: armv7-unknown-linux-gnueabihf
setup: |
sudo apt-get update
sudo apt-get install gcc-arm-linux-gnueabihf -y
build: |
yarn build --target armv7-unknown-linux-gnueabihf
arm-linux-gnueabihf-strip *.node
- host: ubuntu-latest
target: aarch64-unknown-linux-musl
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
build: |-
set -e &&
rustup target add aarch64-unknown-linux-musl &&
yarn build --target aarch64-unknown-linux-musl &&
/aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node
# - host: ubuntu-latest
# target: x86_64-unknown-linux-musl
# docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
# build: set -e && pnpm build && strip *.node
# - host: macos-latest
# target: aarch64-apple-darwin
# build: |
# sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*;
# export CC=$(xcrun -f clang);
# export CXX=$(xcrun -f clang++);
# SYSROOT=$(xcrun --sdk macosx --show-sdk-path);
# export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT";
# pnpm build --target aarch64-apple-darwin
# strip -x *.node
# - host: ubuntu-latest
# target: aarch64-unknown-linux-gnu
# docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64
# build: |-
# set -e &&
# pnpm build --target aarch64-unknown-linux-gnu &&
# aarch64-unknown-linux-gnu-strip *.node
# - host: ubuntu-latest
# target: armv7-unknown-linux-gnueabihf
# setup: |
# sudo apt-get update
# sudo apt-get install gcc-arm-linux-gnueabihf -y
# build: |
# pnpm build --target armv7-unknown-linux-gnueabihf
# arm-linux-gnueabihf-strip *.node
# - host: ubuntu-latest
# target: aarch64-unknown-linux-musl
# docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
# build: |-
# set -e &&
# rustup target add aarch64-unknown-linux-musl &&
# pnpm build --target aarch64-unknown-linux-musl &&
# /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node
name: stable - ${{ matrix.settings.target }} - node@18
runs-on: ${{ matrix.settings.host }}
steps:
Expand All @@ -84,7 +84,7 @@ jobs:
with:
node-version: 18
check-latest: true
cache: yarn
cache: pnpm
- name: Install
uses: dtolnay/rust-toolchain@stable
if: ${{ !matrix.settings.docker }}
Expand All @@ -110,7 +110,7 @@ jobs:
if: ${{ matrix.settings.setup }}
shell: bash
- name: Install dependencies
run: yarn install
run: pnpm install
- name: Build in docker
uses: addnab/docker-run-action@v3
if: ${{ matrix.settings.docker }}
Expand All @@ -122,12 +122,12 @@ jobs:
run: ${{ matrix.settings.build }}
if: ${{ !matrix.settings.docker }}
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
# - name: Upload artifact
# uses: actions/upload-artifact@v3
# with:
# name: bindings-${{ matrix.settings.target }}
# path: ${{ env.APP_NAME }}.*.node
# if-no-files-found: error
publish:
name: Publish
runs-on: ubuntu-latest
Expand All @@ -140,28 +140,26 @@ jobs:
with:
node-version: 18
check-latest: true
cache: yarn
cache: pnpm
- name: Install dependencies
run: yarn install
run: pnpm install
- name: Download all artifacts
uses: actions/download-artifact@v3
with:
path: artifacts
- name: Move artifacts
run: yarn artifacts
run: pnpm artifacts
- name: List packages
run: ls -R ./npm
shell: bash
- name: Publish
run: |
if git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$";
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
# npm publish --access public
yarn run publish:all
else
echo "Not a release, skipping publish"
fi
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: List packages
run: ls -la
shell: bash
# - name: Publish
# run: |
# echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
# # npm publish --access public
# pnpm run publish:all
# env:
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
148 changes: 60 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

Nylon is a web framework for Node.js built with Tokio, Tower, Hyper, and Napi-rs


## Installation

```bash
npm install nylon-rs
```

or

```bash
yarn add nylon-rs
```


## Usage

edit tsconfig.json

```json
{
"compilerOptions": {
Expand All @@ -30,105 +31,76 @@ edit tsconfig.json
```

```ts
import * as os from 'os'
import { Nylon, Logger, Level, Request, Response, HttpException } from 'nylon-rs'
import { getHeapStatistics } from 'v8'
// // nylon-rs
import { NylonFactory } from 'nylon-rs/core'
import { Module, Controller, Get, Params, Query, Post, Body, Middleware, Req, Res } from 'nylon-rs/common'
import { Tracing, TracingOptions } from 'nylon-rs/tracing'
import { HttpStatusCode, Request, Response } from 'nylon-rs/types'

const middleware = async (request: Request, response: Response) => {
request.headers['mid-name'] = 'my middleware'
if (request.query?.state == 'end') {
response.body = {
end: 'my middleware'
}
response.is_end = true
response.status = HttpStatusCode.Created
}
// console.log('[Middleware]', request, response)
return {
request,
response
}
}

@Controller()
export class MainController {
@Get()
index() {
return {
message: 'Hello World!'
}
}

@Get('google')
toGoogle(@Res() res: Response) {
res.status = HttpStatusCode.MovedPermanently
res.headers['Location'] = 'https://google.com'
}
import os from 'os'

@Post()
@Middleware(middleware)
indexPost(
@Body()
body: {
name: string
async function bootstrap() {
let logger = new Logger(Level.Info)
let app = new Nylon()

app.get('/', [
async (ctx) => {
let req = new Request(ctx)
let res = new Response(ctx)
res.json({
data: {
name: 'Nylon',
version: '1.0.0',
user_agent: req.header('user-agent')
}
})
return res.next()
},
@Req() req: Request
) {
return {
middleware_name: req.headers['mid-name'],
message: `Hello ${body.name}!`
}
}
async (ctx) => {
// throw new Error(HttpException(401, 'Unauthorized'))

@Post('raw')
rawPost(
@Body({
raw: true
})
body: Buffer
) {
let data = JSON.parse(body.toString()) as {
name: string
}
return {
message: `Hello ${data.name}!`
let res = new Response(ctx)
res.status(201)
return res.end()
}
}
])

app.post('/', [
async (ctx) => {
let req = new Request(ctx)
let res = new Response(ctx)
res.json({
data: {
method: req.method(),
name: 'Nylon',
version: '1.0.0',
user_agent: req.header('user-agent')
}
})
return res.next()
},
async (ctx) => {
// throw new Error(HttpException(401, 'Unauthorized'))

@Get('hello/:name')
async hello(@Params('name') name: string, @Query('age') age: number) {
return {
message: `Hello ${name}! You are ${age || 1} years old.`
let res = new Response(ctx)
res.status(201)
return res.end()
}
}
}

@Module({
controllers: [MainController]
})
export class MainModule {}
])

async function bootstrap() {
const app = NylonFactory.create(MainModule, {
tracing: [TracingOptions.Trace]
})
await app.listen(3000, '0.0.0.0', () => {
let TracingScope = Tracing.scope('Bootstrap')
TracingScope.info('Worker', process.pid + ' is alive!')
TracingScope.info('HOST_NAME', os.hostname())
TracingScope.info('Platform', os.platform())
TracingScope.info('Node Heap size limit', `${getHeapStatistics().heap_size_limit / (1024 * 1024)} Mb`)
TracingScope.info(`🚀 Application is running on: 0.0.0.0:3000`)
let scopeScope = logger.scope('Bootstrap')
scopeScope.info(['Worker', process.pid + ' is alive!'].join(' '))
scopeScope.info(['HOST_NAME', os.hostname()].join(' '))
scopeScope.info(['Platform', os.platform()].join(' '))
scopeScope.info(['Node Heap size limit', `${getHeapStatistics().heap_size_limit / (1024 * 1024)} Mb`].join(' '))
scopeScope.info(`🚀 Application is running on: 0.0.0.0:3000`)
})
}

// Bootstrap for bun 1.0.x
// @ts-ignore
await bootstrap().then(() => {
// await bootstrap().then(() => {
// console.log('Bootstrap done!')
// })

bootstrap().then(() => {
console.log('Bootstrap done!')
})
```
```
Loading

0 comments on commit 2481d24

Please sign in to comment.