|
| 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