2022-07-23 15:19:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
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-08-23 15:59:17 +00:00
|
|
|
router.POST("/user/:user/webmention/", userWebmentionHandler(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-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-08-23 15:59:17 +00:00
|
|
|
router.POST("/webmention/", userWebmentionHandler(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-08-21 16:15:33 +00:00
|
|
|
router.NotFound = http.HandlerFunc(notFoundHandler(repo))
|
2022-08-03 18:48:47 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:19:47 +00:00
|
|
|
func main() {
|
2022-08-03 14:55:48 +00:00
|
|
|
println("owl web server")
|
2022-07-23 15:19:47 +00:00
|
|
|
println("Parameters")
|
|
|
|
println("-repo <repo> - Specify the repository to use. Defaults to '.'")
|
|
|
|
println("-port <port> - Specify the port to use, Default is '8080'")
|
2022-08-03 18:48:47 +00:00
|
|
|
println("-user <name> - Start server in single user mode.")
|
2022-08-21 09:37:17 +00:00
|
|
|
println("-unsafe - Allow unsafe html.")
|
2022-07-23 15:19:47 +00:00
|
|
|
var repoName string
|
|
|
|
var port int
|
2022-08-03 18:48:47 +00:00
|
|
|
var singleUserName string
|
2022-08-21 09:37:17 +00:00
|
|
|
var allowRawHTML bool = false
|
|
|
|
for i, arg := range os.Args[0:len(os.Args)] {
|
2022-07-23 15:19:47 +00:00
|
|
|
if arg == "-port" {
|
2022-08-21 09:37:17 +00:00
|
|
|
if i+1 >= len(os.Args) {
|
|
|
|
println("-port requires a port number")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
port, _ = strconv.Atoi(os.Args[i+1])
|
|
|
|
}
|
|
|
|
if arg == "-repo" {
|
2022-08-21 09:37:17 +00:00
|
|
|
if i+1 >= len(os.Args) {
|
|
|
|
println("-repo requires a repopath")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
repoName = os.Args[i+1]
|
|
|
|
}
|
2022-08-03 18:48:47 +00:00
|
|
|
if arg == "-user" {
|
2022-08-21 09:37:17 +00:00
|
|
|
if i+1 >= len(os.Args) {
|
|
|
|
println("-user requires a username")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-08-03 18:48:47 +00:00
|
|
|
singleUserName = os.Args[i+1]
|
|
|
|
}
|
2022-08-21 09:37:17 +00:00
|
|
|
if arg == "-unsafe" {
|
|
|
|
allowRawHTML = true
|
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
}
|
|
|
|
if repoName == "" {
|
|
|
|
repoName = "."
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
port = 8080
|
|
|
|
}
|
|
|
|
|
2022-08-03 18:48:47 +00:00
|
|
|
var repo owl.Repository
|
|
|
|
var err error
|
|
|
|
if singleUserName != "" {
|
|
|
|
println("Single user mode")
|
|
|
|
println("Repository:", repoName)
|
|
|
|
println("User:", singleUserName)
|
|
|
|
repo, err = owl.OpenSingleUserRepo(repoName, singleUserName)
|
|
|
|
} else {
|
|
|
|
println("Multi user mode")
|
|
|
|
println("Repository:", repoName)
|
|
|
|
repo, err = owl.OpenRepository(repoName)
|
|
|
|
}
|
2022-08-21 09:37:17 +00:00
|
|
|
repo.SetAllowRawHtml(allowRawHTML)
|
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 singleUserName == "" {
|
|
|
|
println("Multi user mode Router used")
|
2022-08-06 18:22:09 +00:00
|
|
|
router = Router(&repo)
|
2022-08-03 18:48:47 +00:00
|
|
|
} else {
|
|
|
|
println("Single user mode Router used")
|
2022-08-06 18:22:09 +00:00
|
|
|
router = SingleUserRouter(&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
|
|
|
|
|
|
|
}
|