-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (75 loc) · 1.66 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
82
83
84
85
86
87
88
89
90
package main
import (
"github.com/flosch/pongo2"
"github.com/gin-gonic/gin"
"github.com/tecome/pongo2gin"
"github.com/telemac/goutils/files"
"log"
"net/http"
"path"
"strings"
)
// GetPongoTemplates all list
func GetPongoTemplates(c *gin.Context) {
posts := []string{
"Larry Ellison",
"Carlos Slim Helu",
"Mark Zuckerberg",
"Amancio Ortega ",
"Jeff Bezos",
" Warren Buffet ",
"Bill Gates",
"selman tunç",
}
// Call the HTML method of the Context to render a template
uri := c.Param("uri")
fileName := path.Join("templates/", uri)
fileExists, _ := files.FileExists(fileName)
if !fileExists {
c.HTML(404, "404.html", pongo2.Context{
"file": fileName,
})
return
}
c.HTML(http.StatusOK, uri,
pongo2.Context{
"title": "hello pongo",
"posts": posts,
},
)
}
func main() {
gin.SetMode(gin.DebugMode)
r := gin.Default()
r.Use(gin.Recovery())
r.HTMLRender = pongo2gin.TemplatePath("templates")
//r.GET("/templates/*uri", GetPongoTemplates)
r.NoRoute(func(c *gin.Context) {
uri := c.Request.RequestURI
// if uri ends with /
isDirectory := strings.HasSuffix(uri, "/")
if isDirectory {
uri += "index.html"
}
fileName := path.Join("templates/", uri)
fileExists, _ := files.FileExists(fileName)
if !fileExists {
c.HTML(404, "404.html", pongo2.Context{
"file": fileName,
"referer": []string{"c.Request.Host"},
})
return
}
c.HTML(http.StatusOK, uri,
pongo2.Context{
"title": uri,
},
)
})
//r.GET("/", func(c *gin.Context) {
// uri := c.FullPath()
// c.Redirect(http.StatusMovedPermanently, "assets/"+uri)
//})
r.StaticFS("/assets", http.Dir("./assets"))
log.Fatal(r.Run(":8888"))
}