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.
 
 
 
 
 

155 lines
7.8 KiB

  1. package writefreely
  2. import (
  3. "github.com/gorilla/mux"
  4. "github.com/writeas/go-nodeinfo"
  5. "github.com/writeas/go-webfinger"
  6. "github.com/writeas/web-core/log"
  7. "github.com/writeas/writefreely/config"
  8. "net/http"
  9. "strings"
  10. )
  11. func initRoutes(handler *Handler, r *mux.Router, cfg *config.Config, db *datastore) {
  12. hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:]
  13. if cfg.App.SingleUser {
  14. hostSubroute = "{domain}"
  15. } else {
  16. if strings.HasPrefix(hostSubroute, "localhost") {
  17. hostSubroute = "localhost"
  18. }
  19. }
  20. if cfg.App.SingleUser {
  21. log.Info("Adding %s routes (single user)...", hostSubroute)
  22. } else {
  23. log.Info("Adding %s routes (multi-user)...", hostSubroute)
  24. }
  25. // Primary app routes
  26. write := r.Host(hostSubroute).Subrouter()
  27. // Federation endpoint configurations
  28. wf := webfinger.Default(wfResolver{db, cfg})
  29. wf.NoTLSHandler = nil
  30. // Federation endpoints
  31. // host-meta
  32. write.HandleFunc("/.well-known/host-meta", handler.Web(handleViewHostMeta, UserLevelOptional))
  33. // webfinger
  34. write.HandleFunc(webfinger.WebFingerPath, handler.LogHandlerFunc(http.HandlerFunc(wf.Webfinger)))
  35. // nodeinfo
  36. niCfg := nodeInfoConfig(cfg)
  37. ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{cfg, db})
  38. write.HandleFunc(nodeinfo.NodeInfoPath, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfoDiscover)))
  39. write.HandleFunc(niCfg.InfoURL, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfo)))
  40. // Set up dyamic page handlers
  41. // Handle auth
  42. auth := write.PathPrefix("/api/auth/").Subrouter()
  43. if cfg.App.OpenRegistration {
  44. auth.HandleFunc("/signup", handler.All(apiSignup)).Methods("POST")
  45. }
  46. auth.HandleFunc("/login", handler.All(login)).Methods("POST")
  47. auth.HandleFunc("/read", handler.WebErrors(handleWebCollectionUnlock, UserLevelNone)).Methods("POST")
  48. auth.HandleFunc("/me", handler.All(handleAPILogout)).Methods("DELETE")
  49. // Handle logged in user sections
  50. me := write.PathPrefix("/me").Subrouter()
  51. me.HandleFunc("/", handler.Redirect("/me", UserLevelUser))
  52. me.HandleFunc("/c", handler.Redirect("/me/c/", UserLevelUser)).Methods("GET")
  53. me.HandleFunc("/c/", handler.User(viewCollections)).Methods("GET")
  54. me.HandleFunc("/c/{collection}", handler.User(viewEditCollection)).Methods("GET")
  55. me.HandleFunc("/c/{collection}/stats", handler.User(viewStats)).Methods("GET")
  56. me.HandleFunc("/posts", handler.Redirect("/me/posts/", UserLevelUser)).Methods("GET")
  57. me.HandleFunc("/posts/", handler.User(viewArticles)).Methods("GET")
  58. me.HandleFunc("/posts/export.csv", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  59. me.HandleFunc("/posts/export.zip", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  60. me.HandleFunc("/posts/export.json", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
  61. me.HandleFunc("/export", handler.User(viewExportOptions)).Methods("GET")
  62. me.HandleFunc("/export.json", handler.Download(viewExportFull, UserLevelUser)).Methods("GET")
  63. me.HandleFunc("/settings", handler.User(viewSettings)).Methods("GET")
  64. me.HandleFunc("/logout", handler.Web(viewLogout, UserLevelNone)).Methods("GET")
  65. write.HandleFunc("/api/me", handler.All(viewMeAPI)).Methods("GET")
  66. apiMe := write.PathPrefix("/api/me/").Subrouter()
  67. apiMe.HandleFunc("/", handler.All(viewMeAPI)).Methods("GET")
  68. apiMe.HandleFunc("/posts", handler.UserAPI(viewMyPostsAPI)).Methods("GET")
  69. apiMe.HandleFunc("/collections", handler.UserAPI(viewMyCollectionsAPI)).Methods("GET")
  70. apiMe.HandleFunc("/password", handler.All(updatePassphrase)).Methods("POST")
  71. apiMe.HandleFunc("/self", handler.All(updateSettings)).Methods("POST")
  72. // Sign up validation
  73. write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
  74. // Handle collections
  75. write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
  76. apiColls := write.PathPrefix("/api/collections/").Subrouter()
  77. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(fetchCollection)).Methods("GET")
  78. apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(existingCollection)).Methods("POST", "DELETE")
  79. apiColls.HandleFunc("/{alias}/posts", handler.All(fetchCollectionPosts)).Methods("GET")
  80. apiColls.HandleFunc("/{alias}/posts", handler.All(newPost)).Methods("POST")
  81. apiColls.HandleFunc("/{alias}/posts/{post}", handler.All(fetchPost)).Methods("GET")
  82. apiColls.HandleFunc("/{alias}/posts/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST")
  83. apiColls.HandleFunc("/{alias}/posts/{post}/{property}", handler.All(fetchPostProperty)).Methods("GET")
  84. apiColls.HandleFunc("/{alias}/collect", handler.All(addPost)).Methods("POST")
  85. apiColls.HandleFunc("/{alias}/pin", handler.All(pinPost)).Methods("POST")
  86. apiColls.HandleFunc("/{alias}/unpin", handler.All(pinPost)).Methods("POST")
  87. apiColls.HandleFunc("/{alias}/inbox", handler.All(handleFetchCollectionInbox)).Methods("POST")
  88. apiColls.HandleFunc("/{alias}/outbox", handler.All(handleFetchCollectionOutbox)).Methods("GET")
  89. apiColls.HandleFunc("/{alias}/following", handler.All(handleFetchCollectionFollowing)).Methods("GET")
  90. apiColls.HandleFunc("/{alias}/followers", handler.All(handleFetchCollectionFollowers)).Methods("GET")
  91. // Handle posts
  92. write.HandleFunc("/api/posts", handler.All(newPost)).Methods("POST")
  93. posts := write.PathPrefix("/api/posts/").Subrouter()
  94. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(fetchPost)).Methods("GET")
  95. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST", "PUT")
  96. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(deletePost)).Methods("DELETE")
  97. posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}/{property}", handler.All(fetchPostProperty)).Methods("GET")
  98. posts.HandleFunc("/claim", handler.All(addPost)).Methods("POST")
  99. posts.HandleFunc("/disperse", handler.All(dispersePost)).Methods("POST")
  100. if cfg.App.OpenRegistration {
  101. write.HandleFunc("/auth/signup", handler.Web(handleWebSignup, UserLevelNoneRequired)).Methods("POST")
  102. }
  103. write.HandleFunc("/auth/login", handler.Web(webLogin, UserLevelNoneRequired)).Methods("POST")
  104. // Handle special pages first
  105. write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
  106. if cfg.App.SingleUser {
  107. write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  108. } else {
  109. write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  110. }
  111. // All the existing stuff
  112. write.HandleFunc("/{action}/edit", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
  113. write.HandleFunc("/{action}/meta", handler.Web(handleViewMeta, UserLevelOptional)).Methods("GET")
  114. // Collections
  115. if cfg.App.SingleUser {
  116. RouteCollections(handler, write.PathPrefix("/").Subrouter())
  117. } else {
  118. write.HandleFunc("/{prefix:[@~$!\\-+]}{collection}", handler.Web(handleViewCollection, UserLevelOptional))
  119. write.HandleFunc("/{collection}/", handler.Web(handleViewCollection, UserLevelOptional))
  120. RouteCollections(handler, write.PathPrefix("/{prefix:[@~$!\\-+]?}{collection}").Subrouter())
  121. // Posts
  122. write.HandleFunc("/{post}", handler.Web(handleViewPost, UserLevelOptional))
  123. }
  124. write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
  125. }
  126. func RouteCollections(handler *Handler, r *mux.Router) {
  127. r.HandleFunc("/page/{page:[0-9]+}", handler.Web(handleViewCollection, UserLevelOptional))
  128. r.HandleFunc("/tag:{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
  129. r.HandleFunc("/tag:{tag}/feed/", handler.Web(ViewFeed, UserLevelOptional))
  130. r.HandleFunc("/tags/{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
  131. r.HandleFunc("/sitemap.xml", handler.All(handleViewSitemap))
  132. r.HandleFunc("/feed/", handler.All(ViewFeed))
  133. r.HandleFunc("/{slug}", handler.Web(viewCollectionPost, UserLevelOptional))
  134. r.HandleFunc("/{slug}/edit", handler.Web(handleViewPad, UserLevelUser))
  135. r.HandleFunc("/{slug}/edit/meta", handler.Web(handleViewMeta, UserLevelUser))
  136. r.HandleFunc("/{slug}/", handler.Web(handleCollectionPostRedirect, UserLevelOptional)).Methods("GET")
  137. }