Skip to content

Commit 7bfe472

Browse files
authored
fix: sub routing for apps (#66)
This lets apps perform sub routing without hitting 404s, but does not communicate routing upwards (progress towards #44).
1 parent 981c16a commit 7bfe472

6 files changed

Lines changed: 66 additions & 29 deletions

File tree

backend/internal/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (server *Server) Run() error {
132132

133133
// TODO: Move the compiler routes to a separate file/into compiler
134134
// Apps
135-
server.router.GET("/api/app-resources/:id/base.html", func(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
135+
server.router.GET("/api/app-resources/:id/base/*filepath", func(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
136136
id := params.ByName("id")
137137
res.Header().Set("Content-Type", "text/html; charset=utf-8")
138138

backend/internal/server/web_prod.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/julienschmidt/httprouter"
16+
"robinplatform.dev/internal/log"
1617
)
1718

1819
//go:generate rm -rf web
@@ -32,13 +33,27 @@ func (server *Server) loadRoutes() {
3233
return err
3334
}
3435
if !entry.IsDir() {
35-
route := strings.TrimPrefix(assetPath, "web")
36+
routes := []string{strings.TrimPrefix(assetPath, "web")}
37+
3638
if strings.HasSuffix(assetPath, ".html") {
37-
route = regexp.MustCompile(`\[([^\]]+)\]`).ReplaceAllString(route, ":$1")
38-
route = strings.TrimSuffix(route, ".html")
39+
wildcardStartIndex := strings.Index(routes[0], "[[...")
40+
if wildcardStartIndex != -1 {
41+
wildcardName := routes[0][wildcardStartIndex+5:]
42+
wildcardName = wildcardName[:len(wildcardName)-7]
43+
44+
routes = []string{
45+
routes[0][:wildcardStartIndex-1],
46+
routes[0][:wildcardStartIndex] + "*" + wildcardName,
47+
}
48+
}
49+
50+
for idx := range routes {
51+
routes[idx] = regexp.MustCompile(`\[([^\]]+)\]`).ReplaceAllString(routes[idx], ":$1")
52+
routes[idx] = strings.TrimSuffix(routes[idx], ".html")
3953

40-
if strings.HasSuffix(route, "/index") {
41-
route = strings.TrimSuffix(route, "index")
54+
if strings.HasSuffix(routes[0], "/index") {
55+
routes[idx] = strings.TrimSuffix(routes[idx], "index")
56+
}
4257
}
4358
}
4459

@@ -47,20 +62,27 @@ func (server *Server) loadRoutes() {
4762
mimetype = "text/html"
4863
}
4964

50-
router.GET(route, func(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
51-
file, err := nextBuild.Open(assetPath)
52-
if os.IsNotExist(err) {
53-
// TODO: serve 404 page
54-
res.WriteHeader(404)
55-
res.Write([]byte("404 - Not Found"))
56-
} else if err != nil {
57-
res.WriteHeader(500)
58-
res.Write([]byte(err.Error()))
59-
} else {
60-
res.Header().Set("Content-Type", mimetype)
61-
io.Copy(res, file)
62-
}
63-
})
65+
for _, route := range routes {
66+
logger.Debug("Registering route", log.Ctx{
67+
"route": route,
68+
"path": assetPath,
69+
})
70+
71+
router.GET(route, func(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
72+
file, err := nextBuild.Open(assetPath)
73+
if os.IsNotExist(err) {
74+
// TODO: serve 404 page
75+
res.WriteHeader(404)
76+
res.Write([]byte("404 - Not Found"))
77+
} else if err != nil {
78+
res.WriteHeader(500)
79+
res.Write([]byte(err.Error()))
80+
} else {
81+
res.Header().Set("Content-Type", mimetype)
82+
io.Copy(res, file)
83+
}
84+
})
85+
}
6486
}
6587
return nil
6688
})

backend/robin.servers.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"devServers": {
3-
"cli": "go run github.com/mitranim/gow -e 'go,tsx,html' run ./cmd/cli start"
3+
"cli": {
4+
"healthChecks": [
5+
{
6+
"type": "tcp",
7+
"port": 9010
8+
}
9+
],
10+
"command": "go run github.com/mitranim/gow -e 'go,tsx,html' run ./cmd/cli start"
11+
}
412
}
513
}

frontend/components/AppWindow.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ function AppWindowContent({ id, setTitle }: AppWindowProps) {
5959

6060
const iframeRef = React.useRef<HTMLIFrameElement | null>(null);
6161
const [error, setError] = React.useState<string | null>(null);
62+
const subRoute = React.useMemo(
63+
() =>
64+
router.isReady
65+
? router.asPath.substring('/app/'.length + id.length)
66+
: null,
67+
[router.isReady, router.asPath, id],
68+
);
6269

6370
React.useEffect(() => {
6471
const onMessage = (message: MessageEvent) => {
@@ -128,7 +135,7 @@ function AppWindowContent({ id, setTitle }: AppWindowProps) {
128135
<RestartAppButton />
129136

130137
<Link
131-
href={`/app/${id}/settings`}
138+
href={`/app-settings/${id}`}
132139
className={cx(
133140
styles.toolbarButton,
134141
'robin-rounded robin-bg-dark-purple',
@@ -143,7 +150,7 @@ function AppWindowContent({ id, setTitle }: AppWindowProps) {
143150

144151
<iframe
145152
ref={iframeRef}
146-
src={`http://localhost:9010/api/app-resources/${id}/base.html`}
153+
src={`http://localhost:9010/api/app-resources/${id}/base${subRoute}`}
147154
style={{ border: '0', flexGrow: 1, width: '100%', height: '100%' }}
148155
/>
149156
</>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useRouter } from 'next/router';
2-
import { AppToolbar } from '../../../components/AppToolbar';
3-
import { Settings } from '../../../components/Settings';
2+
import { AppToolbar } from '../../components/AppToolbar';
3+
import { Settings } from '../../components/Settings';
44
import { z } from 'zod';
5-
import { useRpcMutation, useRpcQuery } from '../../../hooks/useRpcQuery';
6-
import { Button } from '../../../components/Button';
5+
import { useRpcMutation, useRpcQuery } from '../../hooks/useRpcQuery';
6+
import { Button } from '../../components/Button';
77
import { ArrowLeftIcon } from '@primer/octicons-react';
88
import { toast } from 'react-hot-toast';
9-
import { Alert } from '../../../components/Alert';
10-
import { Spinner } from '../../../components/Spinner';
9+
import { Alert } from '../../components/Alert';
10+
import { Spinner } from '../../components/Spinner';
1111
import { useQueryClient } from '@tanstack/react-query';
1212
import Head from 'next/head';
1313

File renamed without changes.

0 commit comments

Comments
 (0)