Пример #1
0
// 初始化路由项
func initRoute() error {
	m, err := web.NewModule("feed")
	if err != nil {
		return err
	}

	m.GetFunc("/sitemap.xml", func(w http.ResponseWriter, r *http.Request) {
		sitemapMutex.Lock()
		defer sitemapMutex.Unlock()

		if _, err := w.Write(sitemap.Bytes()); err != nil {
			logs.Error("feed.initRoute.route-/sitemap.xml:", err)
			w.WriteHeader(http.StatusNotFound) // 若是出错,给客户端的信息提示为404
		}
	})

	// NOTE:若修改此路由,请同时修改sitemap.xml中的相对应的.xsl路径
	m.GetFunc("/sitemap.xsl", func(w http.ResponseWriter, r *http.Request) {
		if _, err := w.Write(static.Sitemap); err != nil {
			logs.Error("feed.initRoute.route-/sitemap.xsl:", err)
			w.WriteHeader(http.StatusNotFound)
		}
	})

	m.GetFunc("/rss.xml", func(w http.ResponseWriter, r *http.Request) {
		rssMutex.Lock()
		defer rssMutex.Unlock()

		if _, err := w.Write(rss.Bytes()); err != nil {
			logs.Error("feed.initRoute.route-/rss.xml:", err)
			w.WriteHeader(http.StatusNotFound)
		}
	})

	m.GetFunc("/atom.xml", func(w http.ResponseWriter, r *http.Request) {
		atomMutex.Lock()
		defer atomMutex.Unlock()

		if _, err := w.Write(atom.Bytes()); err != nil {
			logs.Error("feed.initRoute.route-/atom.xml:", err)
			w.WriteHeader(http.StatusNotFound)
		}
	})

	return nil
}
Пример #2
0
func initRoute() error {
	m, err := web.NewModule("front")
	if err != nil {
		return err
	}

	m.Get("/", etagHandler(handlers.CompressFunc(pageRoot))).
		Get(app.TagsURL(), etagHandler(handlers.CompressFunc(pageTags))).
		Get(app.TagURL("{id}", 1), etagHandler(handlers.CompressFunc(pageTag))).
		Get(app.PostsURL(1), etagHandler(handlers.CompressFunc(pagePosts))).
		Get(app.PostURL("{id}"), etagHandler(handlers.CompressFunc(pagePost))). // 获取文章详细内容
		Post(app.PostURL("{id}"), etagHandler(handlers.CompressFunc(pagePost))) // 提交评论

	// TODO 静态文件压缩
	m.Get(cfg.UploadURLPrefix+"/", http.StripPrefix(cfg.UploadURLPrefix, http.FileServer(http.Dir(cfg.UploadDir)))).
		Get(cfg.ThemeURLPrefix+"/", http.StripPrefix(cfg.ThemeURLPrefix, http.FileServer(http.Dir(cfg.ThemeDir))))

	// API
	m.Prefix(cfg.FrontAPIPrefix).
		PostFunc("/posts/{id:\\d+}/comments", frontPostPostComment).
		GetFunc("/posts/{id:\\d+}/comments", frontGetPostComments)

	return nil
}
Пример #3
0
func initRoute() error {
	m, err := web.NewModule(moduleName)
	if err != nil {
		return err
	}

	m.Get(cfg.AdminURLPrefix+"/", http.StripPrefix(cfg.AdminURLPrefix, http.FileServer(http.Dir(cfg.AdminDir))))

	p := m.Prefix(cfg.AdminAPIPrefix)

	p.Get("/state", loginHandlerFunc(adminGetState)).
		Get("/modules", loginHandlerFunc(adminGetModules)).
		Put("/modules/{name}/start", loginHandlerFunc(adminPutModuleStart)).
		Put("/modules/{name}/stop", loginHandlerFunc(adminPutModuleStop))

	// users
	p.PostFunc("/login", adminPostLogin).
		Delete("/login", loginHandlerFunc(adminDeleteLogin)).
		Put("/password", loginHandlerFunc(adminChangePassword))

	// feed
	p.Put("/feed/sitemap", loginHandlerFunc(adminPutSitemap)).
		Put("/feed/rss", loginHandlerFunc(adminPutRss)).
		Put("/feed/atom", loginHandlerFunc(adminPutAtom))

	// meida
	p.Post("/media", loginHandlerFunc(adminPostMedia)).
		Get("/media", loginHandlerFunc(adminGetMedia))

	// themes
	p.Get("/themes", loginHandlerFunc(adminGetThemes)).
		Get("/themes/current", loginHandlerFunc(adminGetCurrentTheme)).
		Put("/themes/current", loginHandlerFunc(adminPutCurrentTheme))

	// options
	p.Get("/options/{key}", loginHandlerFunc(adminGetOption)).
		Patch("/options/{key}", loginHandlerFunc(adminPatchOption))

	// tags
	p.Put("/tags/{id:\\d+}", loginHandlerFunc(adminPutTag)).
		Delete("/tags/{id:\\d+}", loginHandlerFunc(adminDeleteTag)).
		Get("/tags/{id:\\d+}", loginHandlerFunc(adminGetTag)).
		Post("/tags", loginHandlerFunc(adminPostTag)).
		Get("/tags", loginHandlerFunc(adminGetTags))

	// comments
	p.Get("/comments", loginHandlerFunc(adminGetComments)).
		Get("/comments/count", loginHandlerFunc(adminGetCommentsCount)).
		Post("/comments", loginHandlerFunc(adminPostComment)).
		Put("/comments/{id:\\d+}", loginHandlerFunc(adminPutComment)).
		Delete("/comments/{id:\\d+}", loginHandlerFunc(adminDeleteComment)).
		Post("/comments/{id:\\d+}/waiting", loginHandlerFunc(adminSetCommentWaiting)).
		Post("/comments/{id:\\d+}/spam", loginHandlerFunc(adminSetCommentSpam)).
		Post("/comments/{id:\\d+}/approved", loginHandlerFunc(adminSetCommentApproved))

	// posts
	p.Get("/posts", loginHandlerFunc(adminGetPosts)).
		Get("/posts/count", loginHandlerFunc(adminGetPostsCount)).
		Post("/posts", loginHandlerFunc(adminPostPost)).
		Get("/posts/{id:\\d+}", loginHandlerFunc(adminGetPost)).
		Delete("/posts/{id:\\d+}", loginHandlerFunc(adminDeletePost)).
		Put("/posts/{id:\\d+}", loginHandlerFunc(adminPutPost)).
		Post("/posts/{id:\\d+}/draft", loginHandlerFunc(adminSetPostDraft)).
		Post("/posts/{id:\\d+}/published", loginHandlerFunc(adminSetPostPublished))

	return nil
}