owl-blogs/cmd/owl/web/server.go

96 lines
3.2 KiB
Go
Raw Permalink Normal View History

2022-09-05 18:34:24 +00:00
package web
2022-07-23 15:19:47 +00:00
import (
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-07-23 15:19:47 +00:00
"net/http"
"os"
"strconv"
2022-08-01 19:06:48 +00:00
"github.com/julienschmidt/httprouter"
)
2022-07-23 15:19:47 +00:00
2022-08-06 18:22:09 +00:00
func Router(repo *owl.Repository) http.Handler {
2022-08-03 17:23:37 +00:00
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
2022-08-03 17:43:00 +00:00
router.GET("/", repoIndexHandler(repo))
router.GET("/user/:user/", userIndexHandler(repo))
2022-12-01 18:10:08 +00:00
router.GET("/user/:user/lists/:list/", postListHandler(repo))
2022-11-29 19:36:50 +00:00
// Editor
router.GET("/user/:user/editor/auth/", userLoginGetHandler(repo))
router.POST("/user/:user/editor/auth/", userLoginPostHandler(repo))
router.GET("/user/:user/editor/", userEditorGetHandler(repo))
router.POST("/user/:user/editor/", userEditorPostHandler(repo))
// Media
2022-09-10 13:22:18 +00:00
router.GET("/user/:user/media/*filepath", userMediaHandler(repo))
2022-11-29 19:36:50 +00:00
// RSS
2022-08-13 13:32:26 +00:00
router.GET("/user/:user/index.xml", userRSSHandler(repo))
2022-11-29 19:36:50 +00:00
// Posts
2022-08-03 17:23:37 +00:00
router.GET("/user/:user/posts/:post/", postHandler(repo))
router.GET("/user/:user/posts/:post/media/*filepath", postMediaHandler(repo))
2022-11-29 19:36:50 +00:00
// Webmention
2022-09-10 13:22:18 +00:00
router.POST("/user/:user/webmention/", userWebmentionHandler(repo))
2022-11-29 19:36:50 +00:00
// Micropub
2022-11-08 20:22:02 +00:00
router.POST("/user/:user/micropub/", userMicropubHandler(repo))
2022-11-29 19:36:50 +00:00
// IndieAuth
router.GET("/user/:user/auth/", userAuthHandler(repo))
router.POST("/user/:user/auth/", userAuthProfileHandler(repo))
router.POST("/user/:user/auth/verify/", userAuthVerifyHandler(repo))
router.POST("/user/:user/auth/token/", userAuthTokenHandler(repo))
2022-11-07 18:50:13 +00:00
router.GET("/user/:user/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
2022-08-06 18:17:39 +00:00
router.NotFound = http.HandlerFunc(notFoundHandler(repo))
2022-08-03 17:23:37 +00:00
return router
}
2022-08-06 18:22:09 +00:00
func SingleUserRouter(repo *owl.Repository) http.Handler {
2022-08-03 18:48:47 +00:00
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", userIndexHandler(repo))
2022-12-01 18:10:08 +00:00
router.GET("/lists/:list/", postListHandler(repo))
2022-11-29 19:36:50 +00:00
// Editor
router.GET("/editor/auth/", userLoginGetHandler(repo))
router.POST("/editor/auth/", userLoginPostHandler(repo))
router.GET("/editor/", userEditorGetHandler(repo))
router.POST("/editor/", userEditorPostHandler(repo))
// Media
2022-09-10 13:22:18 +00:00
router.GET("/media/*filepath", userMediaHandler(repo))
2022-11-29 19:36:50 +00:00
// RSS
2022-08-13 13:32:26 +00:00
router.GET("/index.xml", userRSSHandler(repo))
2022-11-29 19:36:50 +00:00
// Posts
2022-08-03 18:48:47 +00:00
router.GET("/posts/:post/", postHandler(repo))
router.GET("/posts/:post/media/*filepath", postMediaHandler(repo))
2022-11-29 19:36:50 +00:00
// Webmention
2022-09-10 13:22:18 +00:00
router.POST("/webmention/", userWebmentionHandler(repo))
2022-11-29 19:36:50 +00:00
// Micropub
2022-11-08 20:22:02 +00:00
router.POST("/micropub/", userMicropubHandler(repo))
2022-11-29 19:36:50 +00:00
// IndieAuth
router.GET("/auth/", userAuthHandler(repo))
router.POST("/auth/", userAuthProfileHandler(repo))
router.POST("/auth/verify/", userAuthVerifyHandler(repo))
router.POST("/auth/token/", userAuthTokenHandler(repo))
2022-11-07 18:50:13 +00:00
router.GET("/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
2022-08-21 16:15:33 +00:00
router.NotFound = http.HandlerFunc(notFoundHandler(repo))
2022-08-03 18:48:47 +00:00
return router
}
func StartServer(repoPath string, port int) {
2022-08-03 18:48:47 +00:00
var repo owl.Repository
var err error
repo, err = owl.OpenRepository(repoPath)
2022-08-03 18:48:47 +00:00
2022-07-23 15:19:47 +00:00
if err != nil {
println("Error opening repository: ", err.Error())
os.Exit(1)
}
2022-08-03 18:48:47 +00:00
var router http.Handler
if config, _ := repo.Config(); config.SingleUser != "" {
2022-08-06 18:22:09 +00:00
router = SingleUserRouter(&repo)
} else {
router = Router(&repo)
2022-08-03 18:48:47 +00:00
}
2022-07-23 15:19:47 +00:00
println("Listening on port", port)
2022-08-01 19:06:48 +00:00
http.ListenAndServe(":"+strconv.Itoa(port), router)
2022-07-23 15:19:47 +00:00
}