Skip to content

Commit 19af238

Browse files
author
gaowei
committed
feat: add v0.0.1
0 parents  commit 19af238

File tree

31 files changed

+22159
-0
lines changed

31 files changed

+22159
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin
2+
md
3+
cache
4+
articles
5+
markdown-blog-package

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# mardown-blog
2+
这是一个实验性的,基于`Golang`开发的静态**Markdown博客**程序

go.mod

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module github.com/gaowei-space/markdown-blog
2+
3+
go 1.18
4+
5+
require (
6+
github.com/fatih/structs v1.1.0
7+
github.com/kataras/iris/v12 v12.2.0-beta2
8+
github.com/microcosm-cc/bluemonday v1.0.18
9+
github.com/russross/blackfriday v1.6.0
10+
github.com/urfave/cli v1.22.9
11+
)
12+
13+
require (
14+
github.com/BurntSushi/toml v1.1.0 // indirect
15+
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
16+
github.com/CloudyKit/jet/v3 v3.0.0 // indirect
17+
github.com/CloudyKit/jet/v6 v6.1.0 // indirect
18+
github.com/Shopify/goreferrer v0.0.0-20210630161223-536fa16abd6f // indirect
19+
github.com/ajg/form v1.5.1 // indirect
20+
github.com/andybalholm/brotli v1.0.4 // indirect
21+
github.com/aymerick/douceur v0.2.0 // indirect
22+
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible // indirect
23+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
24+
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
25+
github.com/flosch/pongo2/v4 v4.0.2 // indirect
26+
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
27+
github.com/goccy/go-json v0.9.8-0.20220506185958-23bd66f4c0d5 // indirect
28+
github.com/golang/snappy v0.0.4 // indirect
29+
github.com/google/go-querystring v1.1.0 // indirect
30+
github.com/google/uuid v1.3.0 // indirect
31+
github.com/gorilla/css v1.0.0 // indirect
32+
github.com/gorilla/websocket v1.5.0 // indirect
33+
github.com/imkira/go-interpol v1.1.0 // indirect
34+
github.com/iris-contrib/blackfriday v2.0.0+incompatible // indirect
35+
github.com/iris-contrib/jade v1.1.4 // indirect
36+
github.com/iris-contrib/pongo2 v0.0.1 // indirect
37+
github.com/iris-contrib/schema v0.0.6 // indirect
38+
github.com/josharian/intern v1.0.0 // indirect
39+
github.com/json-iterator/go v1.1.12 // indirect
40+
github.com/kataras/blocks v0.0.5 // indirect
41+
github.com/kataras/golog v0.1.7 // indirect
42+
github.com/kataras/pio v0.0.10 // indirect
43+
github.com/kataras/sitemap v0.0.5 // indirect
44+
github.com/kataras/tunnel v0.0.3 // indirect
45+
github.com/klauspost/compress v1.15.3 // indirect
46+
github.com/mailgun/raymond/v2 v2.0.46 // indirect
47+
github.com/mailru/easyjson v0.7.7 // indirect
48+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
49+
github.com/modern-go/reflect2 v1.0.2 // indirect
50+
github.com/moul/http2curl v1.0.0 // indirect
51+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
52+
github.com/ryanuber/columnize v2.1.0+incompatible // indirect
53+
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
54+
github.com/sergi/go-diff v1.2.0 // indirect
55+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
56+
github.com/sirupsen/logrus v1.8.1 // indirect
57+
github.com/smartystreets/goconvey v1.7.2 // indirect
58+
github.com/stretchr/testify v1.7.1 // indirect
59+
github.com/tdewolff/minify/v2 v2.11.2 // indirect
60+
github.com/tdewolff/parse/v2 v2.5.29 // indirect
61+
github.com/valyala/bytebufferpool v1.0.0 // indirect
62+
github.com/valyala/fasthttp v1.36.0 // indirect
63+
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
64+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
65+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
66+
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
67+
github.com/yosssi/ace v0.0.5 // indirect
68+
github.com/yudai/gojsondiff v1.0.0 // indirect
69+
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
70+
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect
71+
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
72+
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
73+
golang.org/x/text v0.3.7 // indirect
74+
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
75+
google.golang.org/protobuf v1.28.0 // indirect
76+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
77+
gopkg.in/ini.v1 v1.66.4 // indirect
78+
gopkg.in/yaml.v2 v2.4.0 // indirect
79+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
80+
)

go.sum

Lines changed: 315 additions & 0 deletions
Large diffs are not rendered by default.

internal/app/app.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package app
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strconv"
7+
"time"
8+
9+
"github.com/fatih/structs"
10+
"github.com/gaowei-space/markdown-blog/internal/utils"
11+
"github.com/kataras/iris/v12"
12+
"github.com/kataras/iris/v12/middleware/accesslog"
13+
"github.com/microcosm-cc/bluemonday"
14+
"github.com/russross/blackfriday"
15+
"github.com/urfave/cli"
16+
)
17+
18+
var (
19+
WebTitle = "Go'Blog"
20+
MdDir string
21+
Env string
22+
Index = "index"
23+
LayoutFile = "web/views/layouts/layout.html"
24+
ArticlesDir = "cache/articles/"
25+
LogsDir = "cache/logs/"
26+
AssetsDir = "web/assets"
27+
)
28+
29+
// web服务器默认端口
30+
const DefaultPort = 5006
31+
32+
func initParams(ctx *cli.Context) {
33+
MdDir = ctx.String("dir")
34+
Env = ctx.String("env")
35+
}
36+
37+
func setLog(app *iris.Application) {
38+
os.MkdirAll(LogsDir, 0777)
39+
f, _ := os.OpenFile(LogsDir+"access-"+time.Now().Format("20060102")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
40+
41+
if Env == "prod" {
42+
app.Logger().SetOutput(f)
43+
} else {
44+
app.Logger().SetLevel("debug")
45+
app.Logger().Debugf(`Log level set to "debug"`)
46+
}
47+
48+
// Close the file on shutdown.
49+
app.ConfigureHost(func(su *iris.Supervisor) {
50+
su.RegisterOnShutdown(func() {
51+
f.Close()
52+
})
53+
})
54+
55+
ac := accesslog.New(f)
56+
ac.AddOutput(app.Logger().Printer)
57+
app.UseRouter(ac.Handler)
58+
app.Logger().Debugf("Using <%s> to log requests", f.Name())
59+
}
60+
61+
func RunWeb(ctx *cli.Context) {
62+
initParams(ctx)
63+
app := iris.New()
64+
65+
setLog(app)
66+
67+
tmpl := iris.HTML("./", ".html").Reload(true)
68+
app.RegisterView(tmpl)
69+
app.OnErrorCode(iris.StatusNotFound, notFound)
70+
app.OnErrorCode(iris.StatusInternalServerError, internalServerError)
71+
72+
app.Use(func(ctx iris.Context) {
73+
ctx.ViewData("Title", WebTitle)
74+
ctx.ViewData("Nav", nav(ctx))
75+
ctx.ViewData("ActiveNav", getActiveNav(ctx))
76+
ctx.ViewLayout(LayoutFile)
77+
78+
ctx.Next()
79+
})
80+
81+
tmpl.AddFunc("inc", utils.Inc)
82+
83+
tmpl.AddFunc("getActive", utils.GetActive)
84+
85+
app.HandleDir("/static", AssetsDir)
86+
87+
// TODO 增加环境变量,获取缓存时间
88+
app.Get("/{f:path}", iris.Cache(time.Minute*0), show)
89+
90+
app.Run(iris.Addr(":" + strconv.Itoa(parsePort(ctx))))
91+
}
92+
93+
func parsePort(ctx *cli.Context) int {
94+
port := DefaultPort
95+
if ctx.IsSet("port") {
96+
port = ctx.Int("port")
97+
}
98+
if port <= 0 || port >= 65535 {
99+
port = DefaultPort
100+
}
101+
102+
return port
103+
}
104+
105+
func nav(ctx iris.Context) []map[string]interface{} {
106+
var option utils.Option
107+
option.RootPath = []string{MdDir}
108+
option.SubFlag = true
109+
option.IgnorePath = []string{}
110+
option.IgnoreFile = []string{`.DS_Store`}
111+
tree, err := utils.Explorer(option)
112+
if err != nil {
113+
ctx.Application().Logger().Infof("Nav list error: %s", err)
114+
}
115+
116+
list := make([]map[string]interface{}, 0)
117+
for _, v := range tree.Children {
118+
for _, item := range v.Children {
119+
list = append(list, structs.Map(item))
120+
}
121+
}
122+
123+
return list
124+
}
125+
126+
func getActiveNav(ctx iris.Context) string {
127+
f := ctx.Params().Get("f")
128+
if f == "" {
129+
f = Index
130+
}
131+
return f
132+
}
133+
134+
func show(ctx iris.Context) {
135+
f := getActiveNav(ctx)
136+
mdfile := MdDir + f + ".md"
137+
articlefile := ArticlesDir + f + ".html"
138+
139+
_, err := os.Stat(mdfile)
140+
if err != nil {
141+
ctx.StatusCode(404)
142+
ctx.Application().Logger().Errorf("Not Found '%s', Path is %s", mdfile, ctx.Path())
143+
return
144+
}
145+
146+
content, err := os.ReadFile(mdfile)
147+
if err != nil {
148+
ctx.StatusCode(500)
149+
ctx.Application().Logger().Errorf("ReadFile Error '%s', Path is %s", mdfile, ctx.Path())
150+
return
151+
}
152+
153+
os.MkdirAll(filepath.Dir(articlefile), 0777)
154+
155+
unsafe := blackfriday.MarkdownCommon(content)
156+
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
157+
if err := os.WriteFile(articlefile, html, 0777); err != nil {
158+
ctx.StatusCode(500)
159+
ctx.Application().Logger().Errorf("WriteFile Error %s, Path is %s", err, ctx.Path())
160+
return
161+
}
162+
163+
ctx.View(articlefile)
164+
}
165+
166+
func notFound(ctx iris.Context) {
167+
ctx.View("web/views/errors/404.html")
168+
}
169+
170+
func internalServerError(ctx iris.Context) {
171+
ctx.View("web/views/errors/500.html")
172+
}

0 commit comments

Comments
 (0)