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.
 
 
 
 
 

208 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. "github.com/gorilla/mux"
  13. "github.com/writeas/go-webfinger"
  14. "github.com/writeas/web-core/log"
  15. "github.com/writefreely/go-nodeinfo"
  16. "net/http"
  17. "path/filepath"
  18. "strings"
  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 (app *App) InitRoutes(r *mux.Router) *mux.Router {
  31. // Create handler
  32. handler := NewWFHandler(app)
  33. // Set up routes
  34. hostSubroute := app.cfg.App.Host[strings.Index(app.cfg.App.Host, "://")+3:]
  35. if app.cfg.App.SingleUser {
  36. hostSubroute = "{domain}"
  37. } else {
  38. if strings.HasPrefix(hostSubroute, "localhost") {
  39. hostSubroute = "localhost"
  40. }
  41. }
  42. if 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{app.db, app.cfg})
  51. wf.NoTLSHandler = nil
  52. // Federation endpoints
  53. // host-meta
  54. write.HandleFunc("/.well-known/host-meta", handler.Web(handleViewHostMeta, UserLevelOptional))
  55. // webfinger
  56. write.HandleFunc(webfinger.WebFingerPath, handler.LogHandlerFunc(http.HandlerFunc(wf.Webfinger)))
  57. // nodeinfo
  58. niCfg := nodeInfoConfig(app.db, app.cfg)
  59. ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{app.cfg, 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 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.All(fetchCollection)).Methods("GET")
  102. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(existingCollection)).Methods("POST", "DELETE")
  103. apiColls.HandleFunc("/{alias}/posts", handler.All(fetchCollectionPosts)).Methods("GET")
  104. apiColls.HandleFunc("/{alias}/posts", handler.All(newPost)).Methods("POST")
  105. apiColls.HandleFunc("/{alias}/posts/{post}", handler.All(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.All(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.All(handleFetchCollectionOutbox)).Methods("GET")
  113. apiColls.HandleFunc("/{alias}/following", handler.All(handleFetchCollectionFollowing)).Methods("GET")
  114. apiColls.HandleFunc("/{alias}/followers", handler.All(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.All(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.All(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.Admin(handleAdminUpdateConfig)).Methods("POST")
  132. write.HandleFunc("/admin/update/{page}", handler.Admin(handleAdminUpdateSite)).Methods("POST")
  133. // Handle special pages first
  134. write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
  135. write.HandleFunc("/invite/{code}", handler.Web(handleViewInvite, UserLevelNoneRequired)).Methods("GET")
  136. // TODO: show a reader-specific 404 page if the function is disabled
  137. // TODO: change this based on configuration for either public or private-to-this-instance
  138. readPerm := UserLevelOptional
  139. write.HandleFunc("/read", handler.Web(viewLocalTimeline, readPerm))
  140. RouteRead(handler, readPerm, write.PathPrefix("/read").Subrouter())
  141. draftEditPrefix := ""
  142. if app.cfg.App.SingleUser {
  143. draftEditPrefix = "/d"
  144. write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  145. } else {
  146. write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  147. }
  148. // All the existing stuff
  149. write.HandleFunc(draftEditPrefix+"/{action}/edit", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  150. write.HandleFunc(draftEditPrefix+"/{action}/meta", handler.Web(handleViewMeta, UserLevelOptional)).Methods("GET")
  151. // Collections
  152. if app.cfg.App.SingleUser {
  153. RouteCollections(handler, write.PathPrefix("/").Subrouter())
  154. } else {
  155. write.HandleFunc("/{prefix:[@~$!\\-+]}{collection}", handler.Web(handleViewCollection, UserLevelOptional))
  156. write.HandleFunc("/{collection}/", handler.Web(handleViewCollection, UserLevelOptional))
  157. RouteCollections(handler, write.PathPrefix("/{prefix:[@~$!\\-+]?}{collection}").Subrouter())
  158. // Posts
  159. }
  160. write.HandleFunc(draftEditPrefix+"/{post}", handler.Web(handleViewPost, UserLevelOptional))
  161. write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
  162. return r
  163. }
  164. func RouteCollections(handler *Handler, r *mux.Router) {
  165. r.HandleFunc("/page/{page:[0-9]+}", handler.Web(handleViewCollection, UserLevelOptional))
  166. r.HandleFunc("/tag:{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
  167. r.HandleFunc("/tag:{tag}/feed/", handler.Web(ViewFeed, UserLevelOptional))
  168. r.HandleFunc("/tags/{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
  169. r.HandleFunc("/sitemap.xml", handler.All(handleViewSitemap))
  170. r.HandleFunc("/feed/", handler.All(ViewFeed))
  171. r.HandleFunc("/{slug}", handler.Web(viewCollectionPost, UserLevelOptional))
  172. r.HandleFunc("/{slug}/edit", handler.Web(handleViewPad, UserLevelUser))
  173. r.HandleFunc("/{slug}/edit/meta", handler.Web(handleViewMeta, UserLevelUser))
  174. r.HandleFunc("/{slug}/", handler.Web(handleCollectionPostRedirect, UserLevelOptional)).Methods("GET")
  175. }
  176. func RouteRead(handler *Handler, readPerm UserLevel, r *mux.Router) {
  177. r.HandleFunc("/api/posts", handler.Web(viewLocalTimelineAPI, readPerm))
  178. r.HandleFunc("/p/{page}", handler.Web(viewLocalTimeline, readPerm))
  179. r.HandleFunc("/feed/", handler.Web(viewLocalTimelineFeed, readPerm))
  180. r.HandleFunc("/t/{tag}", handler.Web(viewLocalTimeline, readPerm))
  181. r.HandleFunc("/a/{post}", handler.Web(handlePostIDRedirect, readPerm))
  182. r.HandleFunc("/{author}", handler.Web(viewLocalTimeline, readPerm))
  183. r.HandleFunc("/", handler.Web(viewLocalTimeline, readPerm))
  184. }