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

70 lines
2.3 KiB
Go
Raw 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-11-03 20:22:55 +00:00
router.GET("/user/:user/auth/", userAuthHandler(repo))
2022-11-05 19:12:23 +00:00
router.POST("/user/:user/auth/", userAuthProfileHandler(repo))
router.POST("/user/:user/auth/verify/", userAuthVerifyHandler(repo))
2022-11-06 18:38:27 +00:00
router.POST("/user/:user/auth/token/", userAuthTokenHandler(repo))
2022-11-07 18:44:10 +00:00
router.GET("/user/:user/auth/indieauth-metadata", userAuthMetadataHandler(repo))
2022-09-10 13:22:18 +00:00
router.GET("/user/:user/media/*filepath", userMediaHandler(repo))
2022-08-13 13:32:26 +00:00
router.GET("/user/:user/index.xml", userRSSHandler(repo))
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-09-10 13:22:18 +00:00
router.POST("/user/:user/webmention/", userWebmentionHandler(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-11-03 20:22:55 +00:00
router.GET("/auth/", userAuthHandler(repo))
2022-11-05 19:12:23 +00:00
router.POST("/auth/", userAuthProfileHandler(repo))
router.POST("/auth/verify/", userAuthVerifyHandler(repo))
2022-11-06 18:38:27 +00:00
router.POST("/auth/token/", userAuthTokenHandler(repo))
2022-11-07 18:44:10 +00:00
router.GET("/auth/indieauth-metadata", userAuthMetadataHandler(repo))
2022-09-10 13:22:18 +00:00
router.GET("/media/*filepath", userMediaHandler(repo))
2022-08-13 13:32:26 +00:00
router.GET("/index.xml", userRSSHandler(repo))
2022-08-03 18:48:47 +00:00
router.GET("/posts/:post/", postHandler(repo))
router.GET("/posts/:post/media/*filepath", postMediaHandler(repo))
2022-09-10 13:22:18 +00:00
router.POST("/webmention/", userWebmentionHandler(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
}