forked from b3log/lute-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (70 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Lute HTTP - HTTP Server for Lute.
// Copyright (c) 2019-present, b3log.org
//
// Lute HTTP is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package main
import (
"os"
"github.com/b3log/gulu"
"github.com/b3log/lute"
"github.com/valyala/fasthttp"
)
var logger = gulu.Log.NewLogger(os.Stdout)
// handleMarkdown2HTML 处理 Markdown 转 HTML。
// POST 请求 Body 传入 Markdown 原文;响应 Body 是处理好的 HTML。
func handleMarkdown2HTML(ctx *fasthttp.RequestCtx) {
body := ctx.PostBody()
engine := lute.New()
html, err := engine.Markdown("", body)
if nil != err {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
logger.Errorf("markdown text [%s] failed: %s\n", body, err.Error())
return
}
ctx.SetBody(html)
}
// handleMarkdownFormat 处理 Markdown 格式化。
// POST 请求 Body 传入 Markdown 原文;响应 Body 是格式化好的 Markdown 文本。
func handleMarkdownFormat(ctx *fasthttp.RequestCtx) {
body := ctx.PostBody()
engine := lute.New()
formatted, err := engine.Format("", body)
if nil != err {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
logger.Errorf("format markdown text [%s] failed: %s\n", body, err.Error())
return
}
ctx.SetBody(formatted)
}
// handle 处理请求分发。
func handle(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/", "":
handleMarkdown2HTML(ctx)
case "/format":
handleMarkdownFormat(ctx)
default:
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
}
// Lute 的 HTTP Server 入口点。
func main() {
gulu.Log.SetLevel("debug")
addr := ":8249"
logger.Infof("booting Lute HTTP Server on [%s]", addr)
server := &fasthttp.Server{
Handler: handle,
MaxRequestBodySize: 1024 * 1024 * 2, // 2MB
}
err := server.ListenAndServe(addr)
if nil != err {
logger.Fatalf("booting Lute HTTP server failed: %s", err.Error())
}
}