diff --git a/cmd/server/main.go b/cmd/server/main.go index d4ab474..469cc35 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,8 +1,36 @@ package main -import "fmt" +import ( + "log" + "net/http" + "os" + "path" + "strings" +) + +func spaHandler() http.HandlerFunc { + fs := getUIAssets() + fileServer := http.FileServer(http.FS(fs)) + + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + fileServer.ServeHTTP(w, r) + return + } + f, err := fs.Open(strings.TrimPrefix(path.Clean(r.URL.Path), "/")) + if err == nil { + defer f.Close() + } + if os.IsNotExist(err) { + r.URL.Path = "/" + } + fileServer.ServeHTTP(w, r) + } +} func main() { - frontend := getUIAssets() - fmt.Println(frontend) + + log.Println("Starting glancr on port 8080") + http.HandleFunc("/", spaHandler()) + log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/ui/src/router/index.ts b/ui/src/router/index.ts index 3e49915..a249f34 100644 --- a/ui/src/router/index.ts +++ b/ui/src/router/index.ts @@ -1,5 +1,6 @@ import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' +import NotFoundView from '../views/NotFoundView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -17,6 +18,11 @@ const router = createRouter({ // which is lazy-loaded when the route is visited. component: () => import('../views/AboutView.vue'), }, + { + path: "/:catchAll(.*)", + name: 'not-found', + component: NotFoundView, + }, ], }) diff --git a/ui/src/views/NotFoundView.vue b/ui/src/views/NotFoundView.vue new file mode 100644 index 0000000..1807080 --- /dev/null +++ b/ui/src/views/NotFoundView.vue @@ -0,0 +1,8 @@ + + + diff --git a/ui/vue.config.ts b/ui/vue.config.ts new file mode 100644 index 0000000..e69de29