Skip to content

Commit

Permalink
Task/api url (#917)
Browse files Browse the repository at this point in the history
Co-authored-by: Sceuick <[email protected]>
  • Loading branch information
sceuick and sceuick authored Apr 27, 2024
1 parent ff7aa85 commit 86e054e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 54 deletions.
102 changes: 52 additions & 50 deletions srv/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,69 @@ import { config } from './config'
import { createServer } from './server'
import pipeline from './api/pipeline'

const upload = multer({
limits: {
fileSize: config.limits.upload * 1024 * 1024,
fieldSize: config.limits.upload * 1024 * 1024,
},
})

const app = express()
const server = createServer(app)

app.use(express.urlencoded({ limit: `${config.limits.upload}mb`, extended: false }))
app.use(express.json({ limit: `${config.limits.payload}mb` }))
app.use(logMiddleware())
app.use(
cors({
origin: '*',
optionsSuccessStatus: 200,
export function createApp() {
const upload = multer({
limits: {
fileSize: config.limits.upload * 1024 * 1024,
fieldSize: config.limits.upload * 1024 * 1024,
},
})
)
app.use(upload.any())

const baseFolder = resolve(__dirname, '..')
const app = express()
const server = createServer(app)

setupSockets(server)
app.use(express.urlencoded({ limit: `${config.limits.upload}mb`, extended: false }))
app.use(express.json({ limit: `${config.limits.payload}mb` }))
app.use(logMiddleware())
app.use(
cors({
origin: '*',
optionsSuccessStatus: 200,
})
)
app.use(upload.any())

const index = resolve(baseFolder, 'dist', 'index.html')
const baseFolder = resolve(__dirname, '..')

app.use('/v1', keyedRouter)
app.use('/api', api)
setupSockets(server)

if (config.pipelineProxy) {
app.use('/pipeline', pipeline)
}
const index = resolve(baseFolder, 'dist', 'index.html')

if (!config.storage.enabled) {
app.use('/assets', express.static(config.assetFolder))
app.use('/', express.static(config.assetFolder))
app.use('/', express.static(resolve(baseFolder, 'assets')))
}
app.use('/v1', keyedRouter)
app.use('/api', api)

app.use('/', express.static(resolve(baseFolder, 'dist')))
app.use('/', express.static(resolve(baseFolder, 'extras')))
if (config.extraFolder) {
app.use('/', express.static(config.extraFolder))
}
if (config.pipelineProxy) {
app.use('/pipeline', pipeline)
}

app.use((req, res, next) => {
if (req.url.startsWith('/api') || req.url.startsWith('/v1')) {
return next(errors.NotFound)
if (!config.storage.enabled) {
app.use('/assets', express.static(config.assetFolder))
app.use('/', express.static(config.assetFolder))
app.use('/', express.static(resolve(baseFolder, 'assets')))
}

return res.sendFile(index)
})
app.use((err: any, _req: any, res: express.Response, _next: any) => {
if (err.status > 0) {
res.status(err.status)
} else {
res.status(500)
app.use('/', express.static(resolve(baseFolder, 'dist')))
app.use('/', express.static(resolve(baseFolder, 'extras')))
if (config.extraFolder) {
app.use('/', express.static(config.extraFolder))
}

res.json({ message: err?.message || err || 'Internal server error' })
})
app.use((req, res, next) => {
if (req.url.startsWith('/api') || req.url.startsWith('/v1')) {
return next(errors.NotFound)
}

export { app, server }
return res.sendFile(index)
})
app.use((err: any, _req: any, res: express.Response, _next: any) => {
if (err.status > 0) {
res.status(err.status)
} else {
res.status(500)
}

res.json({ message: err?.message || err || 'Internal server error' })
})

return { server, app }
}
4 changes: 3 additions & 1 deletion srv/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lt from 'localtunnel'
import * as os from 'os'
import throng from 'throng'
import { initMessageBus } from './api/ws'
import { server } from './app'
import { createApp } from './app'
import { config } from './config'
import { store } from './db'
import { connect, createIndexes } from './db/client'
Expand All @@ -16,6 +16,8 @@ export async function start() {
// No longer accept requests when shutting down
// Allow as many responses currently generating to complete as possible during the shutdown window
// The shutdown window is ~10 seconds
const { server } = createApp()

process.on('SIGTERM', () => {
logger.warn(`Received SIGTERM. Server shutting down.`)
server.close()
Expand Down
18 changes: 16 additions & 2 deletions web/shared/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'solid-js'
import { SettingState, settingStore, userStore } from '../store'
import { getPagePlatform, getWidthPlatform, useEffect, useResizeObserver } from './hooks'
import { wait } from '/common/util'
import { getUserSubscriptionTier, wait } from '/common/util'
import { createDebounce } from './util'

window.googletag = window.googletag || { cmd: [] }
Expand Down Expand Up @@ -69,6 +69,7 @@ const Slot: Component<{
const user = userStore((s) => ({
user: s.user,
sub: s.sub,
tiers: s.tiers,
}))

const [stick, setStick] = createSignal(props.sticky)
Expand All @@ -81,6 +82,11 @@ const Slot: Component<{
const [slotId, setSlotId] = createSignal<string>()
const [actualId, setActualId] = createSignal('...')

const tier = createMemo(() => {
if (!user.user) return
return getUserSubscriptionTier(user.user, user.tiers)
})

const id = createMemo(() => {
if (cfg.provider === 'ez') return `ezoic-pub-ad-placeholder-${uniqueId() || '###'}`
return `${props.slot}-${uniqueId()}`
Expand Down Expand Up @@ -311,7 +317,15 @@ const Slot: Component<{
return (
<>
<Switch>
<Match when={!cfg.ready || !user.user || !specs() || user.sub?.tier?.disableSlots}>
<Match
when={
!cfg.ready ||
!user.user ||
!specs() ||
tier()?.tier.disableSlots ||
user.sub?.tier?.disableSlots
}
>
{null}
</Match>
<Match when={specs()!.video && cfg.slots.gtmVideoTag}>
Expand Down
2 changes: 1 addition & 1 deletion web/store/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const baseUrl =
: HOST === 'agnai.chat' || HOST === 'prd-assets.agnai.chat'
? `${PROTO}//api.agnai.chat`
: HOST === 'dev.agnai.chat' || HOST === 'dev-assets.agnai.chat'
? `${PROTO}//dev-api.agnai.chat`
? `${PROTO}//lb-api.agnai.chat`
: location.origin

export const api = {
Expand Down

0 comments on commit 86e054e

Please sign in to comment.