Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luci-app-example: rewrite RPC side using ucode #7602

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ listed by the shell command

$ ubus list

Custom scripts can be placed in /usr/libexec/rpcd, and must emit JSON. The name of the file
in that directory will be the value for the object key in the declared map.
Custom ucode scripts can be placed in /usr/share/rpcd/ucode, and must emit JSON.

Permissions to make these calls must be granted in /usr/share/rpcd/acl.d
via a file named the same as the application package name (luci-app-example)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ listed by the shell command

$ ubus list

Custom scripts can be placed in /usr/libexec/rpcd, and must emit JSON. The name of the file
in that directory will be the value for the object key in the declared map.
Custom ucode scripts can be placed in /usr/share/rpcd/ucode, and must emit JSON.

Permissions to make these calls must be granted in /usr/share/rpcd/acl.d
via a file named the same as the application package name (luci-app-example)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ listed by the shell command

$ ubus list

Custom scripts can be placed in /usr/libexec/rpcd, and must emit JSON. The name of the file
in that directory will be the value for the object key in the declared map.
Custom ucode scripts can be placed in /usr/share/rpcd/ucode, and must emit JSON.

Permissions to make these calls must be granted in /usr/share/rpcd/acl.d
via a file named the same as the application package name (luci-app-example)
Expand Down Expand Up @@ -104,9 +103,7 @@ return view.extend({
// return is used to modify the DOM that the browser shows.
render: function (data) {
// data[0] will be the result from load_sample1
var sample1 = data[0] || {};
// data[1] will be the result from load_sample_yaml
var sample_yaml = data[1] || {};
const sample1 = data[0] || {};

// Render the tables as individual sections.
return E('div', {}, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ touch /etc/config/example
uci set example.first=first
uci set example.second=second
uci set example.third=third
uci commit
uci set example.animals=animals
uci set example.animals.num_cats=1
uci set example.animals.num_dogs=2
uci set example.animals.num_parakeets=4
uci commit example

return 0
248 changes: 0 additions & 248 deletions applications/luci-app-example/root/usr/libexec/rpcd/luci.example

This file was deleted.

48 changes: 48 additions & 0 deletions applications/luci-app-example/root/usr/share/rpcd/ucode/example.uc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env ucode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, if we're to use the shebang and be able to call this uc file externally, set the +x bit on it also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do ucode files under rpcd actually need a shebang? I can only find one example in luci-app-ddns which has it, but all ucode files outside of rpcd don't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They rpcd ones don't IIUC, but if the shebang is there, it's just good style and intention, so +x should likely also apply. The shebang likely aids testing also :)


'use strict';

import { cursor } from 'uci';

// Rather than parse files in /etc/config, we can use `cursor`.
const uci = cursor();

const methods = {
get_sample1: {
call: function() {
const num_cats = uci.get('example', 'animals', 'num_cats');
const num_dogs = uci.get('example', 'animals', 'num_dogs');
const num_parakeets = uci.get('example', 'animals', 'num_parakeets');
const result = {
num_cats,
num_dogs,
num_parakeets,
is_this_real: false,
not_found: null,
};

uci.unload();
return result;
}
},

get_sample2: {
call: function() {
const result = {
option_one: {
name: "Some string value",
value: "A value string",
parakeets: ["one", "two", "three"],
},
option_two: {
name: "Another string value",
value: "And another value",
parakeets: [3, 4, 5],
},
};
return result;
}
}
};

return { 'luci.example': methods };
15 changes: 7 additions & 8 deletions applications/luci-app-example/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
β”‚ └── example
β”‚ β”œβ”€β”€ form.js
β”‚ β”œβ”€β”€ htmlview.js
β”‚ β”œβ”€β”€ rpc-jsonmap-tablesection.js
β”‚ β”œβ”€β”€ rpc-jsonmap-typedsection.js
β”‚ └── rpc.js
β”œβ”€β”€ Makefile
β”œβ”€β”€ po
Expand All @@ -21,16 +23,15 @@
β”‚ └── uci-defaults
β”‚ └── 80_example
└── usr
β”œβ”€β”€ libexec
β”‚ └── rpcd
β”‚ └── luci.example
└── share
β”œβ”€β”€ luci
β”‚ └── menu.d
β”‚ └── luci-app-example.json
└── rpcd
└── acl.d
└── luci-app-example.json
β”œβ”€β”€ acl.d
β”‚ └── luci-app-example.json
└── ucode
└── example.uc

```

Expand Down Expand Up @@ -68,9 +69,7 @@ LuCI apps do not have to have any additional files such as Lua scripts or UCI de

### Installing additional files

Any additional files needed by this application should be placed in `root/` using the directory tree that applies. This example application needs a RPCd script to be installed, so it places a file in `root/usr/libexec/rpcd/` and calls it `luci.example`. Scripts must have their execution bit set, and committed to the git repository with the bit set.

This example application also installs a file in `/etc/` by putting it in `root/etc/luci.example.yaml`.
Any additional files needed by this application should be placed in `root/` using the directory tree that applies. This example application needs a ucode RPCd script to be installed, so it places a file in `root/usr/share/rpcd/ucode` and called `example.uc`.

The OpenWrt packaging system will install these files automatically.

Expand Down