-
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.
- Loading branch information
yuki
committed
Jun 9, 2022
1 parent
f5788ed
commit 8770368
Showing
7 changed files
with
109 additions
and
114 deletions.
There are no files selected for viewing
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,3 +1,2 @@ | ||
# kokkoro-web | ||
|
||
Create web user interface for kokkoro |
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,18 +1,48 @@ | ||
import Koa from 'koa'; | ||
import views from 'koa-views'; | ||
import serve from 'koa-static'; | ||
import Router from 'koa-router'; | ||
import { join, relative } from 'path'; | ||
import { getStack } from '@kokkoro/utils'; | ||
|
||
const port = 2333; | ||
const app = new Koa(); | ||
const router = new Router(); | ||
export const router = new Router(); | ||
const render = views(__workname); | ||
|
||
router.get('/', ctx => { | ||
ctx.body = 'kokkoro'; | ||
}) | ||
export class Web extends Koa { | ||
constructor() { | ||
super(); | ||
|
||
app | ||
.use(router.routes()) | ||
.use(router.allowedMethods()) | ||
// 基础视图 | ||
this.view('/', viewPath('../views/index')); | ||
this.view('/error', viewPath('../views/error')); | ||
|
||
app.listen(port, () => { | ||
console.log(`server started on http://localhost:${port}`); | ||
}) | ||
// 中间件 | ||
this.use(async (ctx, next) => { | ||
await next(); | ||
ctx.status === 404 && ctx.redirect('/error'); | ||
}); | ||
this.use(render); | ||
this.use(router.routes()); | ||
this.use(router.allowedMethods()); | ||
} | ||
|
||
// 映射视图页面 | ||
view(router_path: string, view_path: string, locals?: any) { | ||
router.get(router_path, async (ctx) => { | ||
await ctx.render(view_path, locals); | ||
}) | ||
} | ||
|
||
// 开放静态资源目录 | ||
assets(assets_path: string) { | ||
this.use(serve(assets_path)); | ||
} | ||
} | ||
|
||
export function viewPath(path: string) { | ||
const stack = getStack(); | ||
const file_name = stack[2].getFileName()!; | ||
const raw_path = join(file_name, '../', path); | ||
|
||
return relative(__workname, raw_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
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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="zh"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Admin</title> | ||
<link rel="stylesheet" href="//cdnjs.loli.net/ajax/libs/normalize/8.0.1/normalize.min.css"> | ||
</head> | ||
|
||
<body> | ||
admin | ||
</body> | ||
|
||
</html> |
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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="zh"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Error</title> | ||
<link rel="stylesheet" href="//cdnjs.loli.net/ajax/libs/normalize/8.0.1/normalize.min.css"> | ||
</head> | ||
|
||
<body> | ||
<span>电波传达不到哦</span> | ||
</body> | ||
|
||
</html> |
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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="zh"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Wellcome</title> | ||
<link rel="stylesheet" href="//cdnjs.loli.net/ajax/libs/normalize/8.0.1/normalize.min.css"> | ||
</head> | ||
|
||
<body> | ||
<img src="https://kokkoro.js.org/assets/images/pixiv/74237509.jpg" alt="id: 74237509"> | ||
</body> | ||
|
||
</html> |