Skip to content

Commit

Permalink
Merge pull request #2 from Chi-EEE/svelte-spa-router
Browse files Browse the repository at this point in the history
Fix RPLidar & Use Svelte-SPA-Router
  • Loading branch information
Chi-EEE authored Dec 13, 2023
2 parents 1b93263 + 5a8e40e commit 3ca8850
Show file tree
Hide file tree
Showing 38 changed files with 670 additions and 573 deletions.
16 changes: 6 additions & 10 deletions app/backend/src/controllers/DynamicController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ class DynamicController : public drogon::HttpController<DynamicController>
{
public:
METHOD_LIST_BEGIN
ADD_METHOD_TO(DynamicController::load, "/room/{1}", drogon::Get);
ADD_METHOD_TO(DynamicController::loadIndex, "/", drogon::Get);
METHOD_LIST_END
void load(const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
std::string&& room_name);
void loadIndex(const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
};

void DynamicController::load(const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
std::string&& arg1)
void DynamicController::loadIndex(const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback)
{

// The 'room' page is dynamically generated locally, so we don't have a specific file to serve.
callback(drogon::HttpResponse::newFileResponse(drogon::app().getDocumentRoot() + "/dynamic.html"));
callback(drogon::HttpResponse::newFileResponse(drogon::app().getDocumentRoot() + "/index.html"));
}
2 changes: 1 addition & 1 deletion app/backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main()
spdlog::set_level(spdlog::level::debug);
auto config_json_path = boost::dll::program_location().parent_path() / "settings" / "config.json";
drogon::app().loadConfigFile(config_json_path.string());

drogon::app().setCustom404Page(drogon::HttpResponse::newFileResponse(drogon::app().getDocumentRoot() + "/404.html"));
std::vector<std::pair<std::string, int>> ip_addresses = {
{"0.0.0.0", 8848},
{"127.0.0.1", 8848},
Expand Down
2 changes: 1 addition & 1 deletion app/backend/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ target("backend")
os.execv("pnpm", {"--prefix", frontend_dir, "run", "build"})

print("Copying svelte files...")
os.cp(path.join(os.scriptdir(), "..", "frontend", "build"), static)
os.cp(path.join(os.scriptdir(), "..", "frontend", "dist"), static)
print("Completed copying svelte files...")
end)

Expand Down
34 changes: 25 additions & 9 deletions app/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
.DS_Store
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

.svelte-kit
1 change: 0 additions & 1 deletion app/frontend/.npmrc

This file was deleted.

3 changes: 3 additions & 0 deletions app/frontend/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}
55 changes: 35 additions & 20 deletions app/frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
# create-svelte
<table>
<tr>
<td>
<a href="https://www.amazon.com/dp/B08D6T6BKS/"><img src="https://static.packt-cdn.com/products/9781839213625/cover/smaller" width="120" /></a>
</td>
<td>
<h3>Svelte 3 Up and Running</h3>
<p>Want to learn Svelte 3 and how to build a Single-Page App (SPA) with it (and with this router)? Check out my book <a href="https://www.amazon.com/dp/B08D6T6BKS/">Svelte 3 Up and Running</a> on Amazon.</p>
</td>
</table>

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
# Svelte app with svelte-spa-router

## Creating a project
This is a project template for [Svelte 4](https://svelte.dev) apps that includes [svelte-spa-router](https://github.com/italypaleale/svelte-spa-router) for client-side routing, TypeScript, and Vite as bundler.

If you're seeing this, you've probably already done this step. Congrats!
## About svelte-spa-router

```bash
# create a new project in the current directory
npm create svelte@latest
svelte-spa-router is a client-side router for Svelte 4 apps that leverages hash-based routing (i.e. stores the current view in the URL after the `#` symbol).

# create a new project in my-app
npm create svelte@latest my-app
```
You can read more about the router, and the reasons why you might want to use hash-based routing (or not), in the [documentation](https://github.com/italypaleale/svelte-spa-router).

## Developing
## Get started

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
Install the dependencies

```bash
npm run dev
cd svelte-app
npm install
```

…then start a dev server using [Vite](https://vite.dev):

# or start the server and open the app in a new browser tab
npm run dev -- --open
```bash
npm run dev
```

## Building
Navigate to [http://localhost:5050](http://localhost:5050). You should see your app running. Edit a component file in `src`, save it, and reload the page to see your changes.

You can add more routes by defining them in the [`src/routes.ts`](./src/routes.ts) file. More information can be found on the [documentation for svelte-spa-router](https://github.com/ItalyPaleAle/svelte-spa-router/blob/main/README.md).

By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the `vite` commands in package.json to include the option `--host 0.0.0.0`.

To create a production version of your app:
If you're using [Visual Studio Code](https://code.visualstudio.com/) we recommend installing the official extension [Svelte for VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). If you are using other editors you may need to install a plugin in order to get syntax highlighting and intellisense.

## Building and running in production mode

To create an optimized version of the app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
You can run the newly-built app with `npm run preview`.
13 changes: 13 additions & 0 deletions app/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/svelte.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte 4 + svelte-spa-router + TypeScript</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
46 changes: 22 additions & 24 deletions app/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
{
"name": "frontend",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.1.1",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.27.7",
"@types/node": "^20.10.4",
"@types/ws": "^8.5.10",
"svelte": "^4.2.8",
"svelte-check": "^3.6.2",
"tslib": "^2.6.2",
"typescript": "^5.3.3",
"vite": "^4.5.1",
"ws": "^8.14.2"
},
"type": "module"
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 5050",
"build": "vite build",
"preview": "vite preview --port 5050",
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@tsconfig/svelte": "^5.0.2",
"svelte": "^4.2.3",
"svelte-check": "^3.6.0",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^5.0.0"
},
"dependencies": {
"svelte-spa-router": "^4.0.0"
}
}
Loading

0 comments on commit 3ca8850

Please sign in to comment.