diff --git a/routes/routes.go b/routes/routes.go index f6c1a49..ccf86cf 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,7 +1,10 @@ package routes import ( + "io/ioutil" + "log" "net/http" + "path" "git.trj.tw/golang/go-file-serve/module/context" "git.trj.tw/golang/go-file-serve/routes/api" @@ -19,12 +22,27 @@ func NewEngine() *gin.Engine { // SetRoutes - func SetRoutes(r *gin.Engine) { + tmpls := make([]string, 0) + files, err := ioutil.ReadDir("./templates") + if err != nil { + log.Fatal(err) + } + for _, v := range files { + if !v.IsDir() { + tmpls = append(tmpls, path.Join("./templates", v.Name())) + } + } + r.LoadHTMLFiles(tmpls...) r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "message": "success", }) }) + r.GET("/html", func(c *gin.Context) { + c.HTML(200, "content.tmpl", gin.H{"title": "test title"}) + }) + apiGroup := r.Group("/api") { apiGroup.GET("/files", context.PatchContext(api.GetFileList)) diff --git a/templates/content.tmpl b/templates/content.tmpl new file mode 100644 index 0000000..d3a5aea --- /dev/null +++ b/templates/content.tmpl @@ -0,0 +1,3 @@ +{{ template "head.tmpl" . }} +Content +{{ template "foot.tmpl" . }} diff --git a/templates/foot.tmpl b/templates/foot.tmpl new file mode 100644 index 0000000..69aae56 --- /dev/null +++ b/templates/foot.tmpl @@ -0,0 +1,3 @@ + + + diff --git a/templates/head.tmpl b/templates/head.tmpl new file mode 100644 index 0000000..8f66eed --- /dev/null +++ b/templates/head.tmpl @@ -0,0 +1,7 @@ + + + + + {{ .title }} + +