2018-10-15 20:44:15 +02:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
2018-10-18 00:57:37 +02:00
|
|
|
"github.com/writeas/go-nodeinfo"
|
2018-11-08 07:28:08 +01:00
|
|
|
"github.com/writeas/go-webfinger"
|
2018-10-15 20:44:15 +02:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
2018-10-18 00:57:37 +02:00
|
|
|
"net/http"
|
2018-10-15 20:44:15 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-10-17 04:31:27 +02:00
|
|
|
func initRoutes(handler *Handler, r *mux.Router, cfg *config.Config, db *datastore) {
|
2018-10-27 23:02:40 +02:00
|
|
|
hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:]
|
2018-11-08 05:43:11 +01:00
|
|
|
if cfg.App.SingleUser {
|
2018-10-15 20:44:15 +02:00
|
|
|
hostSubroute = "{domain}"
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(hostSubroute, "localhost") {
|
|
|
|
hostSubroute = "localhost"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 05:43:11 +01:00
|
|
|
if cfg.App.SingleUser {
|
2018-10-15 20:44:15 +02:00
|
|
|
log.Info("Adding %s routes (single user)...", hostSubroute)
|
2018-11-08 05:43:11 +01:00
|
|
|
} else {
|
|
|
|
log.Info("Adding %s routes (multi-user)...", hostSubroute)
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Primary app routes
|
2018-11-08 16:17:17 +01:00
|
|
|
write := r.PathPrefix("/").Subrouter()
|
2018-10-18 00:57:37 +02:00
|
|
|
|
2018-11-08 07:28:08 +01:00
|
|
|
// Federation endpoint configurations
|
|
|
|
wf := webfinger.Default(wfResolver{db, cfg})
|
|
|
|
wf.NoTLSHandler = nil
|
|
|
|
|
2018-10-18 00:57:37 +02:00
|
|
|
// Federation endpoints
|
2018-11-08 07:28:08 +01:00
|
|
|
// host-meta
|
|
|
|
write.HandleFunc("/.well-known/host-meta", handler.Web(handleViewHostMeta, UserLevelOptional))
|
|
|
|
// webfinger
|
|
|
|
write.HandleFunc(webfinger.WebFingerPath, handler.LogHandlerFunc(http.HandlerFunc(wf.Webfinger)))
|
2018-10-18 00:57:37 +02:00
|
|
|
// nodeinfo
|
2018-11-10 04:10:46 +01:00
|
|
|
niCfg := nodeInfoConfig(db, cfg)
|
2018-10-18 00:57:37 +02:00
|
|
|
ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{cfg, db})
|
|
|
|
write.HandleFunc(nodeinfo.NodeInfoPath, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfoDiscover)))
|
|
|
|
write.HandleFunc(niCfg.InfoURL, handler.LogHandlerFunc(http.HandlerFunc(ni.NodeInfo)))
|
2018-11-08 05:43:11 +01:00
|
|
|
|
2018-11-08 07:31:01 +01:00
|
|
|
// Set up dyamic page handlers
|
|
|
|
// Handle auth
|
|
|
|
auth := write.PathPrefix("/api/auth/").Subrouter()
|
|
|
|
if cfg.App.OpenRegistration {
|
|
|
|
auth.HandleFunc("/signup", handler.All(apiSignup)).Methods("POST")
|
|
|
|
}
|
|
|
|
auth.HandleFunc("/login", handler.All(login)).Methods("POST")
|
|
|
|
auth.HandleFunc("/read", handler.WebErrors(handleWebCollectionUnlock, UserLevelNone)).Methods("POST")
|
|
|
|
auth.HandleFunc("/me", handler.All(handleAPILogout)).Methods("DELETE")
|
|
|
|
|
2018-11-08 07:19:03 +01:00
|
|
|
// Handle logged in user sections
|
|
|
|
me := write.PathPrefix("/me").Subrouter()
|
|
|
|
me.HandleFunc("/", handler.Redirect("/me", UserLevelUser))
|
|
|
|
me.HandleFunc("/c", handler.Redirect("/me/c/", UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/c/", handler.User(viewCollections)).Methods("GET")
|
|
|
|
me.HandleFunc("/c/{collection}", handler.User(viewEditCollection)).Methods("GET")
|
|
|
|
me.HandleFunc("/c/{collection}/stats", handler.User(viewStats)).Methods("GET")
|
|
|
|
me.HandleFunc("/posts", handler.Redirect("/me/posts/", UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/posts/", handler.User(viewArticles)).Methods("GET")
|
|
|
|
me.HandleFunc("/posts/export.csv", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/posts/export.zip", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/posts/export.json", handler.Download(viewExportPosts, UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/export", handler.User(viewExportOptions)).Methods("GET")
|
|
|
|
me.HandleFunc("/export.json", handler.Download(viewExportFull, UserLevelUser)).Methods("GET")
|
|
|
|
me.HandleFunc("/settings", handler.User(viewSettings)).Methods("GET")
|
|
|
|
me.HandleFunc("/logout", handler.Web(viewLogout, UserLevelNone)).Methods("GET")
|
|
|
|
|
|
|
|
write.HandleFunc("/api/me", handler.All(viewMeAPI)).Methods("GET")
|
|
|
|
apiMe := write.PathPrefix("/api/me/").Subrouter()
|
|
|
|
apiMe.HandleFunc("/", handler.All(viewMeAPI)).Methods("GET")
|
|
|
|
apiMe.HandleFunc("/posts", handler.UserAPI(viewMyPostsAPI)).Methods("GET")
|
|
|
|
apiMe.HandleFunc("/collections", handler.UserAPI(viewMyCollectionsAPI)).Methods("GET")
|
|
|
|
apiMe.HandleFunc("/password", handler.All(updatePassphrase)).Methods("POST")
|
|
|
|
apiMe.HandleFunc("/self", handler.All(updateSettings)).Methods("POST")
|
|
|
|
|
|
|
|
// Sign up validation
|
|
|
|
write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
|
|
|
|
|
|
|
|
// Handle collections
|
|
|
|
write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
|
|
|
|
apiColls := write.PathPrefix("/api/collections/").Subrouter()
|
|
|
|
apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(fetchCollection)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias:[0-9a-zA-Z\\-]+}", handler.All(existingCollection)).Methods("POST", "DELETE")
|
|
|
|
apiColls.HandleFunc("/{alias}/posts", handler.All(fetchCollectionPosts)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias}/posts", handler.All(newPost)).Methods("POST")
|
|
|
|
apiColls.HandleFunc("/{alias}/posts/{post}", handler.All(fetchPost)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias}/posts/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST")
|
|
|
|
apiColls.HandleFunc("/{alias}/posts/{post}/{property}", handler.All(fetchPostProperty)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias}/collect", handler.All(addPost)).Methods("POST")
|
|
|
|
apiColls.HandleFunc("/{alias}/pin", handler.All(pinPost)).Methods("POST")
|
|
|
|
apiColls.HandleFunc("/{alias}/unpin", handler.All(pinPost)).Methods("POST")
|
2018-11-08 07:28:08 +01:00
|
|
|
apiColls.HandleFunc("/{alias}/inbox", handler.All(handleFetchCollectionInbox)).Methods("POST")
|
|
|
|
apiColls.HandleFunc("/{alias}/outbox", handler.All(handleFetchCollectionOutbox)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias}/following", handler.All(handleFetchCollectionFollowing)).Methods("GET")
|
|
|
|
apiColls.HandleFunc("/{alias}/followers", handler.All(handleFetchCollectionFollowers)).Methods("GET")
|
2018-11-08 07:19:03 +01:00
|
|
|
|
2018-11-08 05:43:11 +01:00
|
|
|
// Handle posts
|
|
|
|
write.HandleFunc("/api/posts", handler.All(newPost)).Methods("POST")
|
|
|
|
posts := write.PathPrefix("/api/posts/").Subrouter()
|
|
|
|
posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(fetchPost)).Methods("GET")
|
|
|
|
posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(existingPost)).Methods("POST", "PUT")
|
|
|
|
posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}", handler.All(deletePost)).Methods("DELETE")
|
|
|
|
posts.HandleFunc("/{post:[a-zA-Z0-9]{10}}/{property}", handler.All(fetchPostProperty)).Methods("GET")
|
|
|
|
posts.HandleFunc("/claim", handler.All(addPost)).Methods("POST")
|
|
|
|
posts.HandleFunc("/disperse", handler.All(dispersePost)).Methods("POST")
|
|
|
|
|
2018-11-08 07:31:01 +01:00
|
|
|
if cfg.App.OpenRegistration {
|
|
|
|
write.HandleFunc("/auth/signup", handler.Web(handleWebSignup, UserLevelNoneRequired)).Methods("POST")
|
|
|
|
}
|
|
|
|
write.HandleFunc("/auth/login", handler.Web(webLogin, UserLevelNoneRequired)).Methods("POST")
|
|
|
|
|
2018-11-19 02:18:22 +01:00
|
|
|
write.HandleFunc("/admin", handler.Admin(handleViewAdminDash)).Methods("GET")
|
2018-12-03 23:30:31 +01:00
|
|
|
write.HandleFunc("/admin/update/config", handler.Admin(handleAdminUpdateConfig)).Methods("POST")
|
2018-11-19 03:58:50 +01:00
|
|
|
write.HandleFunc("/admin/update/{page}", handler.Admin(handleAdminUpdateSite)).Methods("POST")
|
2018-11-19 02:18:22 +01:00
|
|
|
|
2018-11-08 07:31:01 +01:00
|
|
|
// Handle special pages first
|
|
|
|
write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
|
|
|
|
|
2018-11-10 04:10:46 +01:00
|
|
|
draftEditPrefix := ""
|
2018-11-08 06:11:42 +01:00
|
|
|
if cfg.App.SingleUser {
|
2018-11-10 04:10:46 +01:00
|
|
|
draftEditPrefix = "/d"
|
2018-11-08 06:11:42 +01:00
|
|
|
write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
} else {
|
|
|
|
write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
2018-11-08 05:43:11 +01:00
|
|
|
// All the existing stuff
|
2018-11-10 04:10:46 +01:00
|
|
|
write.HandleFunc(draftEditPrefix+"/{action}/edit", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
write.HandleFunc(draftEditPrefix+"/{action}/meta", handler.Web(handleViewMeta, UserLevelOptional)).Methods("GET")
|
2018-11-08 05:43:11 +01:00
|
|
|
// Collections
|
|
|
|
if cfg.App.SingleUser {
|
2018-11-08 07:19:03 +01:00
|
|
|
RouteCollections(handler, write.PathPrefix("/").Subrouter())
|
2018-11-08 05:43:11 +01:00
|
|
|
} else {
|
2018-11-08 07:19:03 +01:00
|
|
|
write.HandleFunc("/{prefix:[@~$!\\-+]}{collection}", handler.Web(handleViewCollection, UserLevelOptional))
|
|
|
|
write.HandleFunc("/{collection}/", handler.Web(handleViewCollection, UserLevelOptional))
|
|
|
|
RouteCollections(handler, write.PathPrefix("/{prefix:[@~$!\\-+]?}{collection}").Subrouter())
|
2018-11-08 05:43:11 +01:00
|
|
|
// Posts
|
|
|
|
}
|
2018-11-10 04:10:46 +01:00
|
|
|
write.HandleFunc(draftEditPrefix+"/{post}", handler.Web(handleViewPost, UserLevelOptional))
|
2018-11-08 06:11:42 +01:00
|
|
|
write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
2018-11-08 07:19:03 +01:00
|
|
|
|
|
|
|
func RouteCollections(handler *Handler, r *mux.Router) {
|
|
|
|
r.HandleFunc("/page/{page:[0-9]+}", handler.Web(handleViewCollection, UserLevelOptional))
|
|
|
|
r.HandleFunc("/tag:{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
|
|
|
|
r.HandleFunc("/tag:{tag}/feed/", handler.Web(ViewFeed, UserLevelOptional))
|
|
|
|
r.HandleFunc("/tags/{tag}", handler.Web(handleViewCollectionTag, UserLevelOptional))
|
|
|
|
r.HandleFunc("/sitemap.xml", handler.All(handleViewSitemap))
|
|
|
|
r.HandleFunc("/feed/", handler.All(ViewFeed))
|
|
|
|
r.HandleFunc("/{slug}", handler.Web(viewCollectionPost, UserLevelOptional))
|
|
|
|
r.HandleFunc("/{slug}/edit", handler.Web(handleViewPad, UserLevelUser))
|
|
|
|
r.HandleFunc("/{slug}/edit/meta", handler.Web(handleViewMeta, UserLevelUser))
|
|
|
|
r.HandleFunc("/{slug}/", handler.Web(handleCollectionPostRedirect, UserLevelOptional)).Methods("GET")
|
|
|
|
}
|