Skip to content

Commit

Permalink
build: fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
tfkhdyt committed Sep 26, 2023
1 parent 2c3fdd1 commit 6ef4682
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 134 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
.env
.env.production
drizzle
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
.env
.env.production
drizzle
52 changes: 37 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
FROM oven/bun AS builder
# syntax = docker/dockerfile:1

WORKDIR /src
# Adjust BUN_VERSION as desired
ARG BUN_VERSION=1.0.2
FROM oven/bun:${BUN_VERSION} as base

COPY . .
LABEL fly_launch_runtime="Bun"

ENV NODE_ENV=production
# Bun app lives here
WORKDIR /app

RUN bun i && \
bun run generate && \
bun build --target bun --outfile dist/kbbi.js ./src/main.ts
# Set production environment
ENV NODE_ENV="production"

FROM oven/bun

WORKDIR /app
# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
RUN bun install

# Copy application code
COPY --link . .

# Build application
RUN bun run generate

# Remove development dependencies
RUN rm -rf node_modules && \
bun install --ci


COPY --from=builder /src/dist/kbbi.js /app/kbbi.js
COPY --from=builder /src/package.json /app/package.json
COPY --from=builder /src/bun.lockb /app/bun.lockb
COPY --from=builder /src/drizzle /app/drizzle
# Final stage for app image
FROM base

RUN bun i -p
# Copy built application
COPY --from=build /app /app

ENTRYPOINT [ "bun", "run", "/app/kbbi.js" ]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "bun", "run", "src/main.ts" ]
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions drizzle.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
schema: './src/db/postgres/schemas/*',
out: './drizzle',
};
16 changes: 8 additions & 8 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# fly.toml app configuration file generated for kbbi-bot on 2023-09-26T17:57:06+07:00
# fly.toml app configuration file generated for kbbi-bot-v3 on 2023-09-26T19:48:53+07:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "kbbi-bot"
app = "kbbi-bot-v3"
primary_region = "sin"

[build]

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
internal_port = 8080
force_https = true
auto_stop_machines = false
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"format": "prettier -w ./src/",
"lint": "tsc --noEmit && eslint --fix src/",
"build": "tsc",
"start": "bun run ./src/main.ts",
"start": "bun run dist/main.js",
"generate": "drizzle-kit generate:pg"
},
"keywords": [],
Expand All @@ -23,8 +23,10 @@
"telegraf": "^4.14.0"
},
"devDependencies": {
"@flydotio/dockerfile": "latest",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"bun-types": "^1.0.3",
"drizzle-kit": "^0.19.13",
"eslint": "^8.50.0",
"prettier": "^3.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/Fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from './config/config'
import config from './config/config.js'

// fetcher class
export class Fetcher {
Expand Down
4 changes: 2 additions & 2 deletions src/Scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { load, CheerioAPI } from 'cheerio'
// import pretty from 'pretty'

// interface
import { IPengertian } from './interfaces/result.interface'
import { IPengertian } from './interfaces/result.interface.js'

// scraper class
export class Scraper {
Expand All @@ -30,7 +30,7 @@ export class Scraper {

// get prakategorial
this.prakategorial ??= this.$(
'font[title="prakategorial: kata tidak dipakai dalam bentuk dasarnya"]'
'font[title="prakategorial: kata tidak dipakai dalam bentuk dasarnya"]',
)
.next()
.text() as string
Expand Down
85 changes: 40 additions & 45 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { format, isFuture } from 'date-fns'
import { id } from 'date-fns/locale'
import { eq, sql } from 'drizzle-orm'
import { HttpsProxyAgent } from 'https-proxy-agent'
import { Context, Markup, Telegraf } from 'telegraf'
import { InlineKeyboardButton } from 'telegraf/types'

import { Fetcher } from './Fetcher'
import { Scraper } from './Scraper'
import { blackList } from './blacklist/blacklist'
import config from './config/config'
import { db } from './db/postgres'
import { User, users } from './db/postgres/schemas/user.schema'
import { CallbackQuery } from './interfaces/callback-query.interface'
import { IResult } from './interfaces/result.interface'
import { Context, Telegraf } from 'telegraf'

import { Fetcher } from './Fetcher.js'
import { Scraper } from './Scraper.js'
import config from './config/config.js'
import { db } from './db/postgres/index.js'
import { User, users } from './db/postgres/schemas/user.schema.js'
import { CallbackQuery } from './interfaces/callback-query.interface.js'
import { IResult } from './interfaces/result.interface.js'

interface MyContext extends Context {
user: User
Expand Down Expand Up @@ -81,12 +76,12 @@ export default class App {
return this.result
}

private createUrlButton(keyword: string) {
return Markup.button.url(
`📕 ${keyword.toLowerCase()}`,
`https://kbbi.kemdikbud.go.id/entri/${keyword.toLowerCase()}`,
)
}
// private createUrlButton(keyword: string) {
// return Markup.button.url(
// `📕 ${keyword.toLowerCase()}`,
// `https://kbbi.kemdikbud.go.id/entri/${keyword.toLowerCase()}`,
// )
// }

// private createReportButton(keyword: string) {
// return Markup.button.callback(
Expand All @@ -95,32 +90,32 @@ export default class App {
// )
// }

private createInlineKeyboard(
// reportBtn: InlineKeyboardButton.CallbackButton,
urlBtn: InlineKeyboardButton.UrlButton,
) {
return Markup.inlineKeyboard([/* reportBtn, */ urlBtn])
}
// private createInlineKeyboard(
// // reportBtn: InlineKeyboardButton.CallbackButton,
// urlBtn: InlineKeyboardButton.UrlButton,
// ) {
// return Markup.inlineKeyboard([/* reportBtn, */ urlBtn])
// }

checkBlackList(ctx: Context, next: () => Promise<void>) {
const username = ctx.message?.from.username
const result = blackList.find((value) => value.username === username)
if (!result) {
next()
} else {
if (isFuture(result.until)) {
ctx.replyWithMarkdown(
`*Anda telah dibanned dari bot ini!*
Alasan: ${result.reason}
Akses Anda akan dipulihkan pada:
*${format(result.until, 'EEEE, d MMMM yyyy HH:mm', { locale: id })}*`,
)
} else {
next()
}
}
}
// checkBlackList(ctx: Context, next: () => Promise<void>) {
// const username = ctx.message?.from.username
// const result = blackList.find((value) => value.username === username)
// if (!result) {
// next()
// } else {
// if (isFuture(result.until)) {
// ctx.replyWithMarkdown(
// `*Anda telah dibanned dari bot ini!*
// Alasan: ${result.reason}
//
// Akses Anda akan dipulihkan pada:
// *${format(result.until, 'EEEE, d MMMM yyyy HH:mm', { locale: id })}*`,
// )
// } else {
// next()
// }
// }
// }

async main(ctx: MyContext, keyword: string) {
const html = await this.fetchData(keyword)
Expand Down
12 changes: 0 additions & 12 deletions src/blacklist/blacklist.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import { message } from 'telegraf/filters'

import App from './app'
import config from './config/config'
import { db, migrationClient } from './db/postgres'
import { users } from './db/postgres/schemas/user.schema'
import App from './app.js'
import config from './config/config.js'
import { db, migrationClient } from './db/postgres/index.js'
import { users } from './db/postgres/schemas/user.schema.js'

await migrate(drizzle(migrationClient), { migrationsFolder: 'drizzle' })

Expand All @@ -15,7 +15,6 @@ const app = new App(config.botToken)
const bot = app.bot

// middleware
bot.use(app.checkBlackList)
bot.use(async (ctx, next) => {
try {
const userId = ctx.from?.id
Expand Down
Loading

0 comments on commit 6ef4682

Please sign in to comment.