2018-10-15 18:44:15 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
2018-10-17 22:57:37 +00:00
|
|
|
"github.com/writeas/go-nodeinfo"
|
2018-10-15 18:44:15 +00:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
2018-10-17 22:57:37 +00:00
|
|
|
"net/http"
|
2018-10-15 18:44:15 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-10-17 02:31:27 +00:00
|
|
|
func initRoutes(handler *Handler, r *mux.Router, cfg *config.Config, db *datastore) {
|
2018-10-27 21:02:40 +00:00
|
|
|
hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:]
|
2018-11-08 04:43:11 +00:00
|
|
|
if cfg.App.SingleUser {
|
2018-10-15 18:44:15 +00:00
|
|
|
hostSubroute = "{domain}"
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(hostSubroute, "localhost") {
|
|
|
|
hostSubroute = "localhost"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 04:43:11 +00:00
|
|
|
if cfg.App.SingleUser {
|
2018-10-15 18:44:15 +00:00
|
|
|
log.Info("Adding %s routes (single user)...", hostSubroute)
|
2018-11-08 04:43:11 +00:00
|
|
|
} else {
|
|
|
|
log.Info("Adding %s routes (multi-user)...", hostSubroute)
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Primary app routes
|
2018-10-17 22:57:37 +00:00
|
|
|
write := r.Host(hostSubroute).Subrouter()
|
|
|
|
|
|
|
|
// Federation endpoints
|
|
|
|
// nodeinfo
|
|
|
|
niCfg := nodeInfoConfig(cfg)
|
|
|
|
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 04:43:11 +00: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 05:11:42 +00:00
|
|
|
if cfg.App.SingleUser {
|
|
|
|
write.HandleFunc("/me/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
} else {
|
|
|
|
write.HandleFunc("/new", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
2018-11-08 04:43:11 +00:00
|
|
|
// All the existing stuff
|
|
|
|
write.HandleFunc("/{action}/edit", handler.Web(handleViewPad, UserLevelOptional)).Methods("GET")
|
|
|
|
write.HandleFunc("/{action}/meta", handler.Web(handleViewMeta, UserLevelOptional)).Methods("GET")
|
|
|
|
// Collections
|
|
|
|
if cfg.App.SingleUser {
|
|
|
|
} else {
|
|
|
|
// Posts
|
|
|
|
write.HandleFunc("/{post}", handler.Web(handleViewPost, UserLevelOptional))
|
|
|
|
}
|
2018-11-08 05:11:42 +00:00
|
|
|
write.HandleFunc("/", handler.Web(handleViewHome, UserLevelOptional))
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|