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.
 
 
 
 
 

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