package routes import ( "html/template" "io/ioutil" "log" "net/http" "path" "regexp" "git.trj.tw/golang/go-file-serve/module/context" "git.trj.tw/golang/go-file-serve/routes/api" "github.com/gin-gonic/gin" ) // NewEngine - func NewEngine() *gin.Engine { engine := gin.New() engine.Use(gin.Logger()) engine.Use(gin.Recovery()) return engine } // SetRoutes - func SetRoutes(r *gin.Engine) { r.SetFuncMap(template.FuncMap{ "parsePath": func(p string) string { regex := regexp.MustCompile("^/") p = regex.ReplaceAllString(p, "") regex = regexp.MustCompile("/$") p = regex.ReplaceAllString(p, "") return p }, }) 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)) } viewGroup := r.Group("/v") { viewGroup.GET("*path", context.PatchContext(FSList)) } dlGroup := r.Group("/dl") { dlGroup.GET("*path", func(c *gin.Context) { c.JSON(200, gin.H{}) }) } }