-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dirserver.go
72 lines (61 loc) · 1.73 KB
/
dirserver.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
package main
import (
"fmt"
"net/url"
"strings"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
func mdToHTML(md []byte) []byte {
// create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(md)
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
return markdown.Render(doc, renderer)
}
func markdownTranslate(app *App, name string, dir string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if strings.Contains(c.Request.URL.String(), ".md") {
//
// Get the path to the file.
//
newURL, _ := url.QueryUnescape(c.Request.URL.String())
localFile := strings.Replace(newURL, name, "", 1)
filePath := app.AppendPath(dir, localFile)
bodydata := app.ReadFile(filePath)
//
// Translate the markdown to HTML.
//
htmlText := mdToHTML([]byte(bodydata))
fmt.Printf("Translated text: %s\n", htmlText)
//
// send back the translated data.
//
c.Data(200, "text/html", htmlText)
c.Next()
}
}
}
func dirserver(app *App, name string, dir string) {
//
// This will have the web server backend for EmailIt
//
r := gin.Default()
r.Use(markdownTranslate(app, name, dir))
r.Use(static.Serve("/"+name+"/", static.LocalFile(dir, true)))
//
// Run the server. TODO: I need to add a new unique port for each server.
//
err := r.Run(":9000")
if err != nil {
fmt.Print("\n Server error:\n", err.Error())
}
}