forked from kiworkshop/community-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kiworkshop#22] Succeeded to run but failed to render
- Loading branch information
1 parent
3528089
commit ac75db2
Showing
15 changed files
with
585 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1 @@ | ||
import express from 'express'; | ||
import next from 'next'; | ||
import nextI18NextMiddleware from 'next-i18next/middleware'; | ||
import inversifyServices from '../src/inversifyServices'; | ||
import registerControllers from './registerControllers'; | ||
|
||
const nextI18next = inversifyServices.common.i18NService; | ||
|
||
export const PORT = process.env.PORT || 3000; | ||
export const APP = next({ dev: process.env.NODE_ENV !== 'production' }); | ||
export const SERVER = express(); | ||
export const handle = APP.getRequestHandler(); | ||
|
||
APP.prepare().then(() => { | ||
SERVER.use(nextI18NextMiddleware(nextI18next)); | ||
|
||
registerControllers(APP, SERVER); | ||
|
||
SERVER.get('*', (req, res) => handle(req, res)); | ||
|
||
SERVER.listen(PORT); | ||
|
||
// tslint:disable-next-line: no-console | ||
console.log(`> Ready on http://localhost:${PORT}`); | ||
}) | ||
import "./main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import moment from 'moment-timezone'; | ||
import { logger } from "../utils"; | ||
|
||
export class ApiError { | ||
public timestamp: string; | ||
|
||
public constructor( | ||
public status: number, | ||
public error: string, | ||
public message: string, | ||
) { | ||
this.timestamp = moment().tz("Asia/Seoul").toISOString(true); | ||
logger.log('error', JSON.stringify(this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AssertionError } from "assert-plus"; | ||
import { ErrorRequestHandler } from "express-serve-static-core"; | ||
import { logger } from "../utils"; | ||
import { ApiError } from "./ApiError"; | ||
|
||
export const defaultErrorHandler: ErrorRequestHandler = (err: Error, _, res, next) => { | ||
if (res.headersSent) { | ||
return next(err) | ||
} | ||
|
||
if (err instanceof AssertionError) { | ||
res.status(400).send(new ApiError(400, "Bad Request", err.message)); | ||
} | ||
|
||
res.status(500).send(err.stack); | ||
logger.log('error', JSON.stringify(err.stack)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as bodyParser from 'body-parser'; | ||
import { ErrorRequestHandler } from 'express-serve-static-core'; | ||
import { Container } from 'inversify'; | ||
import { InversifyExpressServer } from 'inversify-express-utils'; | ||
import nextI18NextMiddleware from 'next-i18next/middleware'; | ||
import Optional from 'optional-js'; | ||
import I18NService from 'src/common/service/I18NService'; | ||
import "../../mother/notice/api/NoticeController"; | ||
import { defaultErrorHandler } from "../error/DefaultErrorHandler"; | ||
import { NextApp } from '../nextjs/NextApp'; | ||
|
||
export const createApp = (container: Container, errorHandlers?: ErrorRequestHandler[]) => new InversifyExpressServer(container) | ||
.setConfig((theApp) => { | ||
theApp.use(bodyParser.urlencoded({ extended: true })); | ||
theApp.use(bodyParser.json()); | ||
|
||
theApp.use(nextI18NextMiddleware(I18NService)); | ||
|
||
}) | ||
.setErrorConfig((theApp) => { | ||
Optional.ofNullable(errorHandlers) | ||
.map(handlers => handlers.forEach(h => theApp.use(h))); | ||
|
||
const handle = new NextApp().get().getRequestHandler(); | ||
|
||
theApp.get("*", (req, res) => handle(req, res)); | ||
|
||
theApp.use(defaultErrorHandler); | ||
}) | ||
.build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Container } from 'inversify'; | ||
import { NextApp } from '../nextjs/NextApp'; | ||
|
||
export const createContainer = () => { | ||
const container = new Container(); | ||
container.bind<NextApp>('NextApp').to(NextApp); | ||
return container; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { injectable } from 'inversify'; | ||
import next from 'next'; | ||
|
||
const APP = next({ dev: process.env.NODE_ENV !== 'production' }); | ||
|
||
@injectable() | ||
export class NextApp { | ||
public get() { return APP; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const logger = { | ||
log: jest.fn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './logger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import winston from 'winston' | ||
|
||
export const logger = winston.createLogger({ | ||
transports: [ | ||
new winston.transports.Console() | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const errorHandlers = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import "reflect-metadata"; | ||
|
||
import { createApp } from "./common/inversify/createApp"; | ||
import { createContainer } from "./common/inversify/createContainer"; | ||
import { logger } from "./common/utils"; | ||
import { errorHandlers } from "./errorHandlers"; | ||
import "./mother/notice/api/NoticeController"; | ||
|
||
const PORT = 3000; | ||
|
||
createApp(createContainer(), errorHandlers).listen(PORT); | ||
logger.log("info", `server is running on port:${PORT}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Request, Response } from "express"; | ||
import { inject } from "inversify"; | ||
import { controller, httpGet, interfaces, request, response } from "inversify-express-utils"; | ||
import { NextApp } from "server/main/common/nextjs/NextApp"; | ||
|
||
const PATH = "/mother" | ||
|
||
@controller(PATH) | ||
export class MotherController implements interfaces.Controller { | ||
|
||
constructor(@inject("NextApp") private nextApp: NextApp) { } | ||
|
||
@httpGet("/") | ||
public get( | ||
@request() req: Request, | ||
@response() res: Response, | ||
) { | ||
this.nextApp.get().render(req, res, PATH) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Request, Response } from "express"; | ||
import { inject } from "inversify"; | ||
import { controller, httpGet, interfaces, request, requestParam, response } from "inversify-express-utils"; | ||
import { NextApp } from "server/main/common/nextjs/NextApp"; | ||
|
||
const PATH = "/mother/notice"; | ||
|
||
@controller(PATH) | ||
export class ImageController implements interfaces.Controller { | ||
|
||
constructor(@inject("NextApp") private nextApp: NextApp) { } | ||
|
||
@httpGet("/add") | ||
public add( | ||
@request() req: Request, | ||
@response() res: Response, | ||
) { | ||
this.nextApp.get().render(req, res, `${PATH}/add`); | ||
} | ||
|
||
@httpGet("/edit/:id") | ||
public edit( | ||
@request() req: Request, | ||
@response() res: Response, | ||
@requestParam("id") id: string, | ||
) { | ||
this.nextApp.get().render(req, res, `${PATH}/form`, { id }); | ||
} | ||
|
||
@httpGet("/:id") | ||
public detail( | ||
@request() req: Request, | ||
@response() res: Response, | ||
@requestParam("id") id: string, | ||
) { | ||
this.nextApp.get().render(req, res, `${PATH}/detail`, { id }); | ||
} | ||
|
||
@httpGet("/") | ||
public index( | ||
@request() req: Request, | ||
@response() res: Response, | ||
) { | ||
this.nextApp.get().render(req, res, PATH); | ||
} | ||
} |