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.
 
 
 
 
 

207 lines
10 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. // Set up dyamic page handlers
  63. // Handle auth
  64. auth := write.PathPrefix("/api/auth/").Subrouter()
  65. if apper.App().cfg.App.OpenRegistration {
  66. auth.HandleFunc("/signup", handler.All(apiSignup)).Methods("POST")
  67. }
  68. auth.HandleFunc("/login", handler.All(login)).Methods("POST")
  69. auth.HandleFunc("/read", handler.WebErrors(handleWebCollectionUnlock, UserLevelNone)).Methods("POST")
  70. auth.HandleFunc("/me", handler.All(handleAPILogout)).Methods("DELETE")
  71. // Handle logged in user sections
  72. me := write.PathPrefix("/me").Subrouter()
  73. me.HandleFunc("/", handler.Redirect("/me", UserLevelUser))
  74. me.HandleFunc("/c", handler.Redirect("/me/c/", UserLevelUser)).Methods("GET")
  75. me.HandleFunc("/c/", handler.User(viewCollections)).Methods("GET")
  76. me.HandleFunc("/c/{collection}", handler.User(viewEditCollection)).Methods("GET")
  77. me.HandleFunc("/c/{collection}/stats", handler.User(viewStats)).Methods("GET")
  78. me.HandleFunc("/posts", handler.Redirect("/me/posts/", UserLevelUser)).Methods("GET")
  79. me.HandleFunc("/posts/", handler.User(viewArticles)).Methods("GET")
  80. me.HandleFunc("/posts/export.csv", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  81. me.HandleFunc("/posts/export.zip", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  82. me.HandleFunc("/posts/export.json", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  83. me.HandleFunc("/export", handler.User(viewExportOptions)).Methods("GET")
  84. me.HandleFunc("/export.json", handler.Download(viewExportFull, UserLevelUser)).Methods("GET")
  85. me.HandleFunc("/settings", handler.User(viewSettings)).Methods("GET")
  86. me.HandleFunc("/invites", handler.User(handleViewUserInvites)).Methods("GET")
  87. me.HandleFunc("/logout", handler.Web(viewLogout, UserLevelNone)).Methods("GET")
  88. write.HandleFunc("/api/me", handler.All(viewMeAPI)).Methods("GET")
  89. apiMe := write.PathPrefix("/api/me/").Subrouter()
  90. apiMe.HandleFunc("/", handler.All(viewMeAPI)).Methods("GET")
  91. apiMe.HandleFunc("/posts", handler.UserAPI(viewMyPostsAPI)).Methods("GET")
  92. apiMe.HandleFunc("/collections", handler.UserAPI(viewMyCollectionsAPI)).Methods("GET")
  93. apiMe.HandleFunc("/password", handler.All(updatePassphrase)).Methods("POST")
  94. apiMe.HandleFunc("/self", handler.All(updateSettings)).Methods("POST")
  95. apiMe.HandleFunc("/invites", handler.User(handleCreateUserInvite)).Methods("POST")
  96. // Sign up validation
  97. write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
  98. // Handle collections
  99. write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
  100. apiColls := write.PathPrefix("/api/collections/").Subrouter()
  101. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.AllReader(fetchCollection)).Methods("GET")
  102. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(existingCollection)).Methods("POST", "DELETE")
  103. apiColls.HandleFunc("/{alias}/posts", handler.AllReader(fetchCollectionPosts)).Methods("GET")
  104. apiColls.HandleFunc("/{alias}/posts", handler.All(newPost)).Methods("POST")
  105. apiColls.HandleFunc("/{alias}/posts/{post}", handler.AllReader(fetchPost)).Methods("GET")
  106. apiColls.HandleFunc("/{alias}/posts/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST")
  107. apiColls.HandleFunc("/{alias}/posts/{post}/{property}", handler.AllReader(fetchPostProperty)).Methods("GET")
  108. apiColls.HandleFunc("/{alias}/collect", handler.All(addPost)).Methods("POST")
  109. apiColls.HandleFunc("/{alias}/pin", handler.All(pinPost)).Methods("POST")
  110. apiColls.HandleFunc("/{alias}/unpin", handler.All(pinPost)).Methods("POST")
  111. apiColls.HandleFunc("/{alias}/inbox", handler.All(handleFetchCollectionInbox)).Methods("POST")
  112. apiColls.HandleFunc("/{alias}/outbox", handler.AllReader(handleFetchCollectionOutbox)).Methods("GET")
  113. apiColls.HandleFunc("/{alias}/following", handler.AllReader(handleFetchCollectionFollowing)).Methods("GET")
  114. apiColls.HandleFunc("/{alias}/followers", handler.AllReader(handleFetchCollectionFollowers)).Methods("GET")
  115. // Handle posts
  116. write.HandleFunc("/api/posts", handler.All(newPost)).Methods("POST")
  117. posts := write.PathPrefix("/api/posts/").Subrouter()
  118. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.AllReader(fetchPost)).Methods("GET")
  119. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST", "PUT")
  120. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(deletePost)).Methods("DELETE")
  121. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}/{property}", handler.AllReader(fetchPostProperty)).Methods("GET")
  122. posts.HandleFunc("/claim", handler.All(addPost)).Methods("POST")
  123. posts.HandleFunc("/disperse", handler.All(dispersePost)).Methods("POST")
  124. write.HandleFunc("/auth/signup", handler.Web(handleWebSignup, UserLevelNoneRequired)).Methods("POST")
  125. write.HandleFunc("/auth/login", handler.Web(webLogin, UserLevelNoneRequired)).Methods("POST")
  126. write.HandleFunc("/admin", handler.Admin(handleViewAdminDash)).Methods("GET")
  127. write.HandleFunc("/admin/users", handler.Admin(handleViewAdminUsers)).Methods("GET")
  128. write.HandleFunc("/admin/user/{username}", handler.Admin(handleViewAdminUser)).Methods("GET")
  129. write.HandleFunc("/admin/pages", handler.Admin(handleViewAdminPages)).Methods("GET")
  130. write.HandleFunc("/admin/page/{slug}", handler.Admin(handleViewAdminPage)).Methods("GET")
  131. write.HandleFunc("/admin/update/config", handler.AdminApper(handleAdminUpdateConfig)).Methods("POST")
  132. write.HandleFunc("/admin/update/{page}", handler.Admin(handleAdminUpdateSite)).Methods("POST")
  133. write.HandleFunc("/admin/updates", handler.Admin(handleViewAdminUpdates)).Methods("GET")
  134. // Handle special pages first
  135. write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
  136. write.HandleFunc("/invite/{code}", handler.Web(handleViewInvite, UserLevelNoneRequired)).Methods("GET")
  137. // TODO: show a reader-specific 404 page if the function is disabled
  138. write.HandleFunc("/read", handler.Web(viewLocalTimeline, UserLevelReader))
  139. RouteRead(handler, UserLevelReader, write.PathPrefix("/read").Subrouter())
  140. draftEditPrefix := ""
  141. if apper.App().cfg.App.SingleUser {
  142. draftEditPrefix = "/d"
  143. write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  144. } else {
  145. write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  146. }
  147. // All the existing stuff
  148. write.HandleFunc(draftEditPrefix+"/{action}/edit", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  149. write.HandleFunc(draftEditPrefix+"/{action}/meta", handler.Web(handleViewMeta, UserLevelOptional)).Methods("GET")
  150. // Collections
  151. if apper.App().cfg.App.SingleUser {
  152. RouteCollections(handler, write.PathPrefix("/").Subrouter())
  153. } else {
  154. write.HandleFunc("/{prefix:[@~$!\\-+]}{collection}", handler.Web(handleViewCollection, UserLevelReader))
  155. write.HandleFunc("/{collection}/", handler.Web(handleViewCollection, UserLevelReader))
  156. RouteCollections(handler, write.PathPrefix("/{prefix:[@~$!\\-+]?}{collection}").Subrouter())
  157. // Posts
  158. }
  159. write.HandleFunc(draftEditPrefix+"/{post}", handler.Web(handleViewPost, UserLevelOptional))
  160. write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
  161. return r
  162. }
  163. func RouteCollections(handler *Handler, r *mux.Router) {
  164. r.HandleFunc("/page/{page:[0-9]+}", handler.Web(handleViewCollection, UserLevelReader))
  165. r.HandleFunc("/tag:{tag}", handler.Web(handleViewCollectionTag, UserLevelReader))
  166. r.HandleFunc("/tag:{tag}/feed/", handler.Web(ViewFeed, UserLevelReader))
  167. r.HandleFunc("/tags/{tag}", handler.Web(handleViewCollectionTag, UserLevelReader))
  168. r.HandleFunc("/sitemap.xml", handler.AllReader(handleViewSitemap))
  169. r.HandleFunc("/feed/", handler.AllReader(ViewFeed))
  170. r.HandleFunc("/{slug}", handler.CollectionPostOrStatic)
  171. r.HandleFunc("/{slug}/edit", handler.Web(handleViewPad, UserLevelUser))
  172. r.HandleFunc("/{slug}/edit/meta", handler.Web(handleViewMeta, UserLevelUser))
  173. r.HandleFunc("/{slug}/", handler.Web(handleCollectionPostRedirect, UserLevelReader)).Methods("GET")
  174. }
  175. func RouteRead(handler *Handler, readPerm UserLevelFunc, r *mux.Router) {
  176. r.HandleFunc("/api/posts", handler.Web(viewLocalTimelineAPI, readPerm))
  177. r.HandleFunc("/p/{page}", handler.Web(viewLocalTimeline, readPerm))
  178. r.HandleFunc("/feed/", handler.Web(viewLocalTimelineFeed, readPerm))
  179. r.HandleFunc("/t/{tag}", handler.Web(viewLocalTimeline, readPerm))
  180. r.HandleFunc("/a/{post}", handler.Web(handlePostIDRedirect, readPerm))
  181. r.HandleFunc("/{author}", handler.Web(viewLocalTimeline, readPerm))
  182. r.HandleFunc("/", handler.Web(viewLocalTimeline, readPerm))
  183. }