0.1.0-alpha.23 • Published 4 months ago

rayze v0.1.0-alpha.23

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Rayze

Rayze is a web framework for easy use base on fastify

Use

1. init

bun init

2. install

bun install rayze

3. run

Add script in package.json

  "scripts": {
    "dev": "rayze",
    "build": "rayze build"
  },

4. tsconfig.json

{
  "compilerOptions": {
    // Enable latest features
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",

    // Bundler mode
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "noEmit": true,

    // Best practices
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,

    // Some stricter flags (disabled by default)
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noPropertyAccessFromIndexSignature": false,

    // typeorm
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false
  }
}

Route

1. create route

- src
  - routes
    - hello.ts

And hello.ts will auto gen http://localhost:8080/hello

import { Route } from 'rayze'

export default {
  handler: async req => {
    console.log(req.query())
    return 'hello rayze!'
    // return { xxx: yyy }  // or json
  }
} as Route

2. custom url

import { Route } from 'rayze'

export default {
  method: 'POST',
  url: '/user/:id',
  handler: async (req, reply) => {
    req.param()
    reply.status(500)
    return ''
  }
} as Route

Orm

rayze use typeorm

1. config

touch .env

DB_TYPE=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=postgres
DB_USERNAME=postgres
DB_PASSWORD=123456
DB_SYNCHRONIZE=true

2. install drive

bun install pg

3. create model

- src
  - models
    - user.ts

And user.ts

import { Column, Entity, Model } from 'rayze'

@Entity({ name: 'user' })
export class User extends Model {
  @Column({ name: 'nickname' })
  nickname: string

  @Column({ name: 'username', nullable: true })
  username?: string
}

4. use orm

const { manager } = this.orm // this is the fastify instant

const find = await manager.findOneBy(User, { id: 1 })

Config

1. custom port or other

You can custom from env or config file.

- package.json
- rayze.config.ts

And rayze.config.ts

import { defineConfig } from 'rayze'

export default defineConfig({
  port: 9000
})

Hook

1. add hook file

you can add anything what you like of startup.ts

-src
  - hooks
    - on-request.ts

And on-request.ts

export default (): OnRequestHook => {
  return async function (request, reply) {
    this.log.info('Custom onRequest hook url: ' + request.url)
  }
}

2. hook list

all the fastify hooks, and ext pre-ready application hook that run before onReady.

import { type PreReadyHook, FastifyInstance } from 'rayze'

export default (): PreReadyHook => {
  return async function () {
    await Bun.sleep(2000)
    // you can use fastify to add route or register plugin
    this.get('/x', { config: { expose: true } }, (req, rep) => {
      rep.send('ok')
    })
  }
}

Deploy

1. build single file

bun run build
./dist/app

2. build as docker

touch Dockerfile and run docker build -t appName:version .

FROM oven/bun:alpine as builder
WORKDIR /workspace
COPY package.json ./
RUN bun install
COPY ./ ./
RUN bun run build

FROM alpine
RUN apk --no-cache add libstdc++
WORKDIR /workspace
ENV MODE=production \
    PORT=8080
HEALTHCHECK --interval=5s --timeout=3s --retries=5 --start-period=5s \
    CMD wget --spider http://127.0.0.1:$PORT/rayze/health || exit 1
COPY --from=builder /workspace/dist/app /workspace/app
EXPOSE $PORT
ENTRYPOINT ["./app"]
0.1.0-alpha.23

4 months ago

0.1.0-alpha.22

5 months ago

0.1.0-alpha.21

5 months ago

0.1.0-alpha.20

5 months ago

0.1.0-alpha.19

5 months ago

0.1.0-alpha.18

5 months ago

0.1.0-alpha.17

5 months ago

0.1.0-alpha.16

5 months ago

0.1.0-alpha.15

5 months ago

0.1.0-alpha.14

5 months ago

0.1.0-alpha.13

5 months ago

0.1.0-alpha.12

5 months ago

0.1.0-alpha.11

5 months ago

0.1.0-alpha.10

5 months ago

0.1.0-alpha.9

5 months ago

0.1.0-alpha.8

5 months ago

0.1.0-alpha.7

5 months ago

0.1.0-alpha.6

5 months ago

0.1.0-alpha.5

5 months ago

0.1.0-alpha.4

5 months ago

0.1.0-alpha.3

5 months ago

0.1.1-beta.13

6 months ago

0.1.1-beta.12

6 months ago

0.1.1-beta.11

6 months ago

0.1.1-beta.10

6 months ago

0.1.1-beta.9

6 months ago

0.1.1-beta.8

6 months ago

0.1.1-beta.7

6 months ago

0.1.1-beta.6

6 months ago

0.1.1-beta.5

6 months ago

0.1.1-beta.4

6 months ago

0.1.1-beta.2

6 months ago

0.1.1-beta.1

6 months ago

0.0.1-alpha.7

6 months ago

0.0.1-alpha.6

6 months ago

0.1.0-alpha.2

6 months ago

0.1.0-alpha.1

6 months ago

0.1.0-beta.1

6 months ago

0.0.1-alpha.5

6 months ago

0.0.1-alpha.4

6 months ago

0.0.1-alpha.3

6 months ago

0.0.1-alpha.2

6 months ago

0.0.1-alpha.1

6 months ago