Skip to content

Commit

Permalink
feat:add close function.
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuki committed Aug 1, 2022
1 parent 8ee823f commit f10dc8d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
11 changes: 6 additions & 5 deletions packages/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface FourzeApp extends EventEmitter {
createServer(): Server

listen(port?: number, host?: string): Promise<Server>

close(): void
}

export interface FouzeServerContext {
Expand Down Expand Up @@ -229,6 +231,10 @@ export function createApp(options: FourzeAppOptions = {}) {
})
}

app.close = function () {
this.server?.close()
}

Object.defineProperties(app, {
port: {
get() {
Expand All @@ -247,10 +253,5 @@ export function createApp(options: FourzeAppOptions = {}) {
}
})

process.on("SIGINT", () => {
logger.info(`Server process [${process.pid}] is shutting down...`)
_server?.close()
process.exit(0)
})
return app
}
5 changes: 1 addition & 4 deletions packages/server/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ export function createRouter(params: FourzeRouterOptions | FourzeSetup): FourzeR
const route = module?.exports?.default ?? module?.default
if (isFourze(route) || isRoute(route) || (Array.isArray(route) && route.some(isRoute))) {
moduleNames.add(mod)
logger.info("register route", route)
} else {
logger.error("find not route", mod, route, typeof route, isFourze(route))
logger.error(`find not route with "${mod}" `, route)
}
}

Expand Down Expand Up @@ -140,8 +139,6 @@ export function createRouter(params: FourzeRouterOptions | FourzeSetup): FourzeR
}
await loadModule(moduleName)
}

logger.info("load module done", moduleName)
}

router.watch = function watch(this: FourzeRouter, dir?: string | FSWatcher, customWatcher?: FSWatcher) {
Expand Down
15 changes: 9 additions & 6 deletions packages/unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,26 @@ export default createUnplugin((options: UnpluginFourzeOptions = {}) => {
config(config, env) {
options.mock = options.mock ?? (env.command == "build" || env.mode === "mock")

newServer = options.server?.port != config.server?.port || options.server?.host != config.server?.host

return {
define: {
VITE_PLUGIN_FOURZE_MOCK: options.mock
}
}
},
configureServer({ middlewares, watcher }) {
configureServer({ middlewares, httpServer, watcher }) {
if (hmr) {
router.watch(watcher)
}
if (newServer) {
app.listen(options.server?.port, options.server?.host)

if (options.server) {
try {
app.listen(options.server?.port, options.server?.host)
} catch (error) {
logger.error("Server listen failed.", error)
}
} else {
console.log(router.routes)
middlewares.use(app)
logger.info("Fourze middleware was installed!")
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions playgrounds/vite/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import path from "path"
import { defineConfig } from "vite"

export default defineConfig({
server: {
port: 8000,
host: "0.0.0.0"
},
plugins: [
vue(),
jsx(),
Expand Down

0 comments on commit f10dc8d

Please sign in to comment.