- commit
- a7b907a
- parent
- 5093a4e
- author
- Eric Bower
- date
- 2024-04-04 02:41:55 +0000 UTC
feat(pgs): support serving files without extensions
2 files changed,
+16,
-10
+7,
-10
1@@ -74,20 +74,17 @@ func checkIsRedirect(status int) bool {
2 }
3
4 func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpReply {
5- rts := expandRoute(projectName, fp, http.StatusOK)
6 fext := filepath.Ext(fp)
7- // add route as-is without expansion if there is a file ext
8- if fp != "" && fext != "" {
9+ rts := []*HttpReply{}
10+ // add route as-is without expansion
11+ if fp != "" && !strings.HasSuffix(fp, "/") {
12 defRoute := shared.GetAssetFileName(&utils.FileEntry{
13 Filepath: filepath.Join(projectName, fp),
14 })
15-
16- rts = append(rts,
17- &HttpReply{
18- Filepath: defRoute, Status: http.StatusOK,
19- },
20- )
21+ rts = append(rts, &HttpReply{Filepath: defRoute, Status: http.StatusOK})
22 }
23+ expts := expandRoute(projectName, fp, http.StatusOK)
24+ rts = append(rts, expts...)
25
26 // user routes
27 for _, redirect := range userRedirects {
28@@ -135,7 +132,7 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
29
30 // filename without extension mean we might have a directory
31 // so add a trailing slash with a 301
32- if !strings.HasSuffix(fp, "/") && fp != "" && fext == "" {
33+ if fp != "" && !strings.HasSuffix(fp, "/") && fext == "" {
34 redirectRoute := shared.GetAssetFileName(&utils.FileEntry{
35 Filepath: fp + "/",
36 })
+9,
-0
1@@ -66,6 +66,7 @@ func TestCalcRoutes(t *testing.T) {
2 Name: "trailing-slash",
3 Actual: calcRoutes("test", "/folder", []*RedirectRule{}),
4 Expected: []*HttpReply{
5+ {Filepath: "test/folder", Status: 200},
6 {Filepath: "test/folder.html", Status: 200},
7 {Filepath: "/folder/", Status: 301},
8 {Filepath: "test/404.html", Status: 404},
9@@ -108,6 +109,7 @@ func TestCalcRoutes(t *testing.T) {
10 },
11 ),
12 Expected: []*HttpReply{
13+ {Filepath: "test/wow", Status: 200},
14 {Filepath: "test/wow.html", Status: 200},
15 {Filepath: "/index.html", Status: 301},
16 {Filepath: "/wow/", Status: 301},
17@@ -128,6 +130,7 @@ func TestCalcRoutes(t *testing.T) {
18 },
19 ),
20 Expected: []*HttpReply{
21+ {Filepath: "test/tester1", Status: 200},
22 {Filepath: "test/tester1.html", Status: 200},
23 {Filepath: "https://pico.sh", Status: 301},
24 {Filepath: "/tester1/", Status: 301},
25@@ -218,6 +221,7 @@ func TestCalcRoutes(t *testing.T) {
26 },
27 ),
28 Expected: []*HttpReply{
29+ {Filepath: "test/wow", Status: 200},
30 {Filepath: "test/wow.html", Status: 200},
31 {Filepath: "https://pico.sh", Status: 301},
32 {Filepath: "/wow/", Status: 301},
33@@ -238,6 +242,7 @@ func TestCalcRoutes(t *testing.T) {
34 },
35 ),
36 Expected: []*HttpReply{
37+ {Filepath: "public/xyz", Status: 200},
38 {Filepath: "public/xyz.html", Status: 200},
39 {Filepath: "/wrk-xyz", Status: 301},
40 {Filepath: "/xyz/", Status: 301},
41@@ -258,7 +263,9 @@ func TestCalcRoutes(t *testing.T) {
42 },
43 ),
44 Expected: []*HttpReply{
45+ {Filepath: "public/folder2", Status: 200},
46 {Filepath: "public/folder2.html", Status: 200},
47+ {Filepath: "public/folder", Status: 200},
48 {Filepath: "public/folder.html", Status: 200},
49 {Filepath: "/folder/", Status: 301},
50 {Filepath: "public/404.html", Status: 404},
51@@ -278,6 +285,7 @@ func TestCalcRoutes(t *testing.T) {
52 },
53 ),
54 Expected: []*HttpReply{
55+ {Filepath: "public/folder2", Status: 200},
56 {Filepath: "public/folder2.html", Status: 200},
57 {Filepath: "/folder2/", Status: 301},
58 {Filepath: "public/404.html", Status: 404},
59@@ -316,6 +324,7 @@ func TestCalcRoutes(t *testing.T) {
60 },
61 ),
62 Expected: []*HttpReply{
63+ {Filepath: "public/space", Status: 200},
64 {Filepath: "public/space.html", Status: 200},
65 {Filepath: "/frontier/", Status: 301},
66 {Filepath: "/space/", Status: 301},