Skip to content

Commit

Permalink
luci-app-example: rewrite RPC side using ucode
Browse files Browse the repository at this point in the history
Signed-off-by: George Sapkin <[email protected]>
  • Loading branch information
GeorgeSapkin committed Feb 3, 2025
1 parent e845441 commit b27b82a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 266 deletions.
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

'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

0 comments on commit b27b82a

Please sign in to comment.