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.
 
 
 
 
 

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