Fix buggy frontend routing

This commit is contained in:
Lukas SP 2020-08-24 18:32:14 +02:00
parent 6b0d0007e2
commit f5357a2c9d
9 changed files with 8 additions and 3 deletions

View File

@ -29,7 +29,7 @@ func Serve() error {
frontend := frontendHandler()
router.GET("/{path:*}", func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if !strings.HasPrefix(path, "/api") {
if !strings.HasPrefix(path, "/api") && (strings.Count(path, "/") == 1 || strings.HasPrefix(path, "/assets")) {
frontend(ctx)
return
}
@ -88,6 +88,11 @@ func frontendHandler() fasthttp.RequestHandler {
CacheDuration: 0,
}
fs.PathNotFound = func(ctx *fasthttp.RequestCtx) {
if strings.HasPrefix(string(ctx.Path()), "/assets") {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetBodyString("not found")
return
}
ctx.SendFile(filepath.Join(fs.Root, "index.html"))
}
return fs.NewRequestHandler()

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>pasty</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="assets/css/style.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/styles/default.min.css">
</head>
<body>
@ -63,6 +63,6 @@
</div>
</div>
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/highlight.min.js"></script>
<script src="js/autoload.js" type="module"></script>
<script src="assets/js/autoload.js" type="module"></script>
</body>
</html>