A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

227 lines
12 KiB

  1. /*
  2. * Copyright © 2018-2019 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "net/http"
  13. "path/filepath"
  14. "strings"
  15. "github.com/gorilla/mux"
  16. "github.com/writeas/go-webfinger"
  17. "github.com/writeas/web-core/log"
  18. "github.com/writefreely/go-nodeinfo"
  19. )
  20. // InitStaticRoutes adds routes for serving static files.
  21. // TODO: this should just be a func, not method
  22. func (app *App) InitStaticRoutes(r *mux.Router) {
  23. // Handle static files
  24. fs := http.FileServer(http.Dir(filepath.Join(app.cfg.Server.StaticParentDir, staticDir)))
  25. fs = cacheControl(fs)
  26. app.shttp = http.NewServeMux()
  27. app.shttp.Handle("/", fs)
  28. r.PathPrefix("/").Handler(fs)
  29. }
  30. // InitRoutes adds dynamic routes for the given mux.Router.
  31. func InitRoutes(apper Apper, r *mux.Router) *mux.Router {
  32. // Create handler
  33. handler := NewWFHandler(apper)
  34. // Set up routes
  35. hostSubroute := apper.App().cfg.App.Host[strings.Index(apper.App().cfg.App.Host, "://")+3:]
  36. if apper.App().cfg.App.SingleUser {
  37. hostSubroute = "{domain}"
  38. } else {
  39. if strings.HasPrefix(hostSubroute, "localhost") {
  40. hostSubroute = "localhost"
  41. }
  42. }
  43. if apper.App().cfg.App.SingleUser {
  44. log.Info("Adding %s routes (single user)...", hostSubroute)
  45. } else {
  46. log.Info("Adding %s routes (multi-user)...", hostSubroute)
  47. }
  48. // Primary app routes
  49. write := r.PathPrefix("/").Subrouter()
  50. // Federation endpoint configurations
  51. wf := webfinger.Default(wfResolver{apper.App().db, apper.App().cfg})
  52. wf.NoTLSHandler = nil
  53. // Federation endpoints
  54. // host-meta
  55. write.HandleFunc("/.well-known/host-meta", handler.Web(handleViewHostMeta, UserLevelReader))
  56. // webfinger
  57. write.HandleFunc(webfinger.WebFingerPath, handler.LogHandlerFunc(http.HandlerFunc(wf.Webfinger)))
  58. // nodeinfo
  59. niCfg := nodeInfoConfig(apper.App().db, apper.App().cfg)
  60. ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{apper.App().cfg, apper.App().db})
  61. write.HandleFunc(nodeinfo.NodeInfoPath, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfoDiscover)))
  62. write.HandleFunc(niCfg.InfoURL, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfo)))
  63. // handle mentions
  64. write.HandleFunc("/@/{handle}", handler.Web(handleViewMention, UserLevelReader))
  65. configureSlackOauth(handler, write, apper.App())
  66. configureWriteAsOauth(handler, write, apper.App())
  67. configureGitlabOauth(handler, write, apper.App())
  68. configureGenericOauth(handler, write, apper.App())
  69. configureGiteaOauth(handler, write, apper.App())
  70. // Set up dyamic page handlers
  71. // Handle auth
  72. auth := write.PathPrefix("/api/auth/").Subrouter()
  73. if apper.App().cfg.App.OpenRegistration {
  74. auth.HandleFunc("/signup", handler.All(apiSignup)).Methods("POST")
  75. }
  76. auth.HandleFunc("/login", handler.All(login)).Methods("POST")
  77. auth.HandleFunc("/read", handler.WebErrors(handleWebCollectionUnlock, UserLevelNone)).Methods("POST")
  78. auth.HandleFunc("/me", handler.All(handleAPILogout)).Methods("DELETE")
  79. // Handle logged in user sections
  80. me := write.PathPrefix("/me").Subrouter()
  81. me.HandleFunc("/", handler.Redirect("/me", UserLevelUser))
  82. me.HandleFunc("/c", handler.Redirect("/me/c/", UserLevelUser)).Methods("GET")
  83. me.HandleFunc("/c/", handler.User(viewCollections)).Methods("GET")
  84. me.HandleFunc("/c/{collection}", handler.User(viewEditCollection)).Methods("GET")
  85. me.HandleFunc("/c/{collection}/stats", handler.User(viewStats)).Methods("GET")
  86. me.HandleFunc("/posts", handler.Redirect("/me/posts/", UserLevelUser)).Methods("GET")
  87. me.HandleFunc("/posts/", handler.User(viewArticles)).Methods("GET")
  88. me.HandleFunc("/posts/export.csv", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  89. me.HandleFunc("/posts/export.zip", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  90. me.HandleFunc("/posts/export.json", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  91. me.HandleFunc("/export", handler.User(viewExportOptions)).Methods("GET")
  92. me.HandleFunc("/export.json", handler.Download(viewExportFull, UserLevelUser)).Methods("GET")
  93. me.HandleFunc("/import", handler.User(viewImport)).Methods("GET")
  94. me.HandleFunc("/settings", handler.User(viewSettings)).Methods("GET")
  95. me.HandleFunc("/invites", handler.User(handleViewUserInvites)).Methods("GET")
  96. me.HandleFunc("/logout", handler.Web(viewLogout, UserLevelNone)).Methods("GET")
  97. write.HandleFunc("/api/me", handler.All(viewMeAPI)).Methods("GET")
  98. apiMe := write.PathPrefix("/api/me/").Subrouter()
  99. apiMe.HandleFunc("/", handler.All(viewMeAPI)).Methods("GET")
  100. apiMe.HandleFunc("/posts", handler.UserAPI(viewMyPostsAPI)).Methods("GET")
  101. apiMe.HandleFunc("/collections", handler.UserAPI(viewMyCollectionsAPI)).Methods("GET")
  102. apiMe.HandleFunc("/password", handler.All(updatePassphrase)).Methods("POST")
  103. apiMe.HandleFunc("/self", handler.All(updateSettings)).Methods("POST")
  104. apiMe.HandleFunc("/invites", handler.User(handleCreateUserInvite)).Methods("POST")
  105. apiMe.HandleFunc("/import", handler.User(handleImport)).Methods("POST")
  106. apiMe.HandleFunc("/oauth/remove", handler.User(removeOauth)).Methods("POST")
  107. // Sign up validation
  108. write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
  109. write.HandleFunc("/api/markdown", handler.All(handleRenderMarkdown)).Methods("POST")
  110. // Handle collections
  111. write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
  112. apiColls := write.PathPrefix("/api/collections/").Subrouter()
  113. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.AllReader(fetchCollection)).Methods("GET")
  114. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(existingCollection)).Methods("POST", "DELETE")
  115. apiColls.HandleFunc("/{alias}/posts", handler.AllReader(fetchCollectionPosts)).Methods("GET")
  116. apiColls.HandleFunc("/{alias}/posts", handler.All(newPost)).Methods("POST")
  117. apiColls.HandleFunc("/{alias}/posts/{post}", handler.AllReader(fetchPost)).Methods("GET")
  118. apiColls.HandleFunc("/{alias}/posts/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST")
  119. apiColls.HandleFunc("/{alias}/posts/{post}/{property}", handler.AllReader(fetchPostProperty)).Methods("GET")
  120. apiColls.HandleFunc("/{alias}/collect", handler.All(addPost)).Methods("POST")
  121. apiColls.HandleFunc("/{alias}/pin", handler.All(pinPost)).Methods("POST")
  122. apiColls.HandleFunc("/{alias}/unpin", handler.All(pinPost)).Methods("POST")
  123. apiColls.HandleFunc("/{alias}/inbox", handler.All(handleFetchCollectionInbox)).Methods("POST")
  124. apiColls.HandleFunc("/{alias}/outbox", handler.AllReader(handleFetchCollectionOutbox)).Methods("GET")
  125. apiColls.HandleFunc("/{alias}/following", handler.AllReader(handleFetchCollectionFollowing)).Methods("GET")
  126. apiColls.HandleFunc("/{alias}/followers", handler.AllReader(handleFetchCollectionFollowers)).Methods("GET")
  127. // Handle posts
  128. write.HandleFunc("/api/posts", handler.All(newPost)).Methods("POST")
  129. posts := write.PathPrefix("/api/posts/").Subrouter()
  130. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.AllReader(fetchPost)).Methods("GET")
  131. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST", "PUT")
  132. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(deletePost)).Methods("DELETE")
  133. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}/{property}", handler.AllReader(fetchPostProperty)).Methods("GET")
  134. posts.HandleFunc("/claim", handler.All(addPost)).Methods("POST")
  135. posts.HandleFunc("/disperse", handler.All(dispersePost)).Methods("POST")
  136. write.HandleFunc("/auth/signup", handler.Web(handleWebSignup, UserLevelNoneRequired)).Methods("POST")
  137. write.HandleFunc("/auth/login", handler.Web(webLogin, UserLevelNoneRequired)).Methods("POST")
  138. write.HandleFunc("/admin", handler.Admin(handleViewAdminDash)).Methods("GET")
  139. write.HandleFunc("/admin/monitor", handler.Admin(handleViewAdminMonitor)).Methods("GET")
  140. write.HandleFunc("/admin/settings", handler.Admin(handleViewAdminSettings)).Methods("GET")
  141. write.HandleFunc("/admin/users", handler.Admin(handleViewAdminUsers)).Methods("GET")
  142. write.HandleFunc("/admin/user/{username}", handler.Admin(handleViewAdminUser)).Methods("GET")
  143. write.HandleFunc("/admin/user/{username}/status", handler.Admin(handleAdminToggleUserStatus)).Methods("POST")
  144. write.HandleFunc("/admin/user/{username}/passphrase", handler.Admin(handleAdminResetUserPass)).Methods("POST")
  145. write.HandleFunc("/admin/pages", handler.Admin(handleViewAdminPages)).Methods("GET")
  146. write.HandleFunc("/admin/page/{slug}", handler.Admin(handleViewAdminPage)).Methods("GET")
  147. write.HandleFunc("/admin/update/config", handler.AdminApper(handleAdminUpdateConfig)).Methods("POST")
  148. write.HandleFunc("/admin/update/{page}", handler.Admin(handleAdminUpdateSite)).Methods("POST")
  149. write.HandleFunc("/admin/updates", handler.Admin(handleViewAdminUpdates)).Methods("GET")
  150. // Handle special pages first
  151. write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
  152. write.HandleFunc("/signup", handler.Web(handleViewLanding, UserLevelNoneRequired))
  153. write.HandleFunc("/invite/{code:[a-zA-Z0-9]+}", handler.Web(handleViewInvite, UserLevelOptional)).Methods("GET")
  154. // TODO: show a reader-specific 404 page if the function is disabled
  155. write.HandleFunc("/read", handler.Web(viewLocalTimeline, UserLevelReader))
  156. RouteRead(handler, UserLevelReader, write.PathPrefix("/read").Subrouter())
  157. draftEditPrefix := ""
  158. if apper.App().cfg.App.SingleUser {
  159. draftEditPrefix = "/d"
  160. write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelUser)).Methods("GET")
  161. } else {
  162. write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelUser)).Methods("GET")
  163. }
  164. // All the existing stuff
  165. write.HandleFunc(draftEditPrefix+"/{action}/edit", handler.Web(handleViewPad, UserLevelUser)).Methods("GET")
  166. write.HandleFunc(draftEditPrefix+"/{action}/meta", handler.Web(handleViewMeta, UserLevelUser)).Methods("GET")
  167. // Collections
  168. if apper.App().cfg.App.SingleUser {
  169. RouteCollections(handler, write.PathPrefix("/").Subrouter())
  170. } else {
  171. write.HandleFunc("/{prefix:[@~$!\\-+]}{collection}", handler.Web(handleViewCollection, UserLevelReader))
  172. write.HandleFunc("/{collection}/", handler.Web(handleViewCollection, UserLevelReader))
  173. RouteCollections(handler, write.PathPrefix("/{prefix:[@~$!\\-+]?}{collection}").Subrouter())
  174. // Posts
  175. }
  176. write.HandleFunc(draftEditPrefix+"/{post}", handler.Web(handleViewPost, UserLevelOptional))
  177. write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
  178. return r
  179. }
  180. func RouteCollections(handler *Handler, r *mux.Router) {
  181. r.HandleFunc("/page/{page:[0-9]+}", handler.Web(handleViewCollection, UserLevelReader))
  182. r.HandleFunc("/tag:{tag}", handler.Web(handleViewCollectionTag, UserLevelReader))
  183. r.HandleFunc("/tag:{tag}/feed/", handler.Web(ViewFeed, UserLevelReader))
  184. r.HandleFunc("/sitemap.xml", handler.AllReader(handleViewSitemap))
  185. r.HandleFunc("/feed/", handler.AllReader(ViewFeed))
  186. r.HandleFunc("/{slug}", handler.CollectionPostOrStatic)
  187. r.HandleFunc("/{slug}/edit", handler.Web(handleViewPad, UserLevelUser))
  188. r.HandleFunc("/{slug}/edit/meta", handler.Web(handleViewMeta, UserLevelUser))
  189. r.HandleFunc("/{slug}/", handler.Web(handleCollectionPostRedirect, UserLevelReader)).Methods("GET")
  190. }
  191. func RouteRead(handler *Handler, readPerm UserLevelFunc, r *mux.Router) {
  192. r.HandleFunc("/api/posts", handler.Web(viewLocalTimelineAPI, readPerm))
  193. r.HandleFunc("/p/{page}", handler.Web(viewLocalTimeline, readPerm))
  194. r.HandleFunc("/feed/", handler.Web(viewLocalTimelineFeed, readPerm))
  195. r.HandleFunc("/t/{tag}", handler.Web(viewLocalTimeline, readPerm))
  196. r.HandleFunc("/a/{post}", handler.Web(handlePostIDRedirect, readPerm))
  197. r.HandleFunc("/{author}", handler.Web(viewLocalTimeline, readPerm))
  198. r.HandleFunc("/", handler.Web(viewLocalTimeline, readPerm))
  199. }