Skip to content

Commit 079e25a

Browse files
committed
feat: add /show urls
1 parent 5d214c7 commit 079e25a

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

packages/backend/src/routers/_default.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ router.all('*', async function(req, res, next) {
266266
else{
267267
let canonical_url = config.origin + path;
268268
let app_name, app_title, description;
269+
let launch_options = {
270+
on_initialized: []
271+
};
269272

270273
// default title
271274
app_title = config.title;
@@ -290,6 +293,18 @@ router.all('*', async function(req, res, next) {
290293

291294
path = '/';
292295
}
296+
else if (path.startsWith('/show/')) {
297+
const filepath = path.slice('/show'.length);
298+
launch_options.on_initialized.push({
299+
$: 'window-call',
300+
fn_name: 'launch_app',
301+
args: [{
302+
name: 'explorer',
303+
path: filepath,
304+
}],
305+
});
306+
path = '/';
307+
}
293308

294309
const manifest =
295310
_fs.existsSync(_path.join(config.assets.gui, 'puter-gui.json'))
@@ -308,7 +323,7 @@ router.all('*', async function(req, res, next) {
308323
short_description: config.short_description,
309324
company: 'Puter Technologies Inc.',
310325
canonical_url: canonical_url,
311-
});
326+
}, launch_options);
312327
}
313328

314329
// /dist/...

packages/backend/src/services/PuterHomepageService.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PuterHomepageService extends BaseService {
3232
this.service_scripts.push(url);
3333
}
3434

35-
async send (res, meta) {
35+
async send (res, meta, launch_options) {
3636
const config = this.global_config;
3737
return res.send(this.generate_puter_page_html({
3838
env: config.env,
@@ -47,6 +47,9 @@ class PuterHomepageService extends BaseService {
4747
// page meta
4848
meta,
4949

50+
// launch options
51+
launch_options,
52+
5053
// gui parameters
5154
gui_params: {
5255
app_name_regex: config.app_name_regex,
@@ -80,6 +83,7 @@ class PuterHomepageService extends BaseService {
8083
api_origin,
8184

8285
meta,
86+
launch_options,
8387

8488
gui_params,
8589
}) {
@@ -101,6 +105,7 @@ class PuterHomepageService extends BaseService {
101105
gui_params = {
102106
...meta,
103107
...gui_params,
108+
launch_options,
104109
app_origin,
105110
api_origin,
106111
gui_origin: app_origin,

src/definitions.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919
export class Service {
20-
construct () {
20+
construct (o) {
21+
this.$puter = {};
22+
for ( const k in o ) this.$puter[k] = o[k];
2123
if ( ! this._construct ) return;
2224
return this._construct();
2325
}

src/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* it under the terms of the GNU Affero General Public License as published
88
* by the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* This program is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Affero General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Affero General Public License
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
@@ -21,21 +21,21 @@ window.puter_gui_enabled = true;
2121

2222
/**
2323
* Initializes and configures the GUI (Graphical User Interface) settings based on the provided options.
24-
*
24+
*
2525
* The function sets global variables in the window object for various settings such as origins and domain names.
2626
* It also handles loading different resources depending on the environment (development or production).
27-
*
27+
*
2828
* @param {Object} options - Configuration options to initialize the GUI.
2929
* @param {string} [options.gui_origin='https://puter.com'] - The origin URL for the GUI.
3030
* @param {string} [options.api_origin='https://api.puter.com'] - The origin URL for the API.
3131
* @param {number} [options.max_item_name_length=500] - Maximum allowed length for an item name.
3232
* @param {boolean} [options.require_email_verification_to_publish_website=true] - Flag to decide whether email verification is required to publish a website.
33-
*
33+
*
3434
* @property {string} [options.app_domain] - Extracted domain name from gui_origin. It's derived automatically if not provided.
3535
* @property {string} [window.gui_env] - The environment in which the GUI is running (e.g., "dev" or "prod").
36-
*
36+
*
3737
* @returns {Promise<void>} Returns a promise that resolves when initialization and resource loading are complete.
38-
*
38+
*
3939
* @example
4040
* window.gui({
4141
* gui_origin: 'https://myapp.com',
@@ -59,7 +59,7 @@ window.gui = async function(options){
5959
await window.loadScript('/sdk/puter.dev.js');
6060
await window.loadScript(`${options.asset_dir}/initgui.js`, {isModule: true});
6161
}
62-
62+
6363
// PROD: load the minified bundles if we are in production mode
6464
// note: the order of the bundles is important
6565
// note: Build script will prepend `window.gui_env="prod"` to the top of the file
@@ -71,13 +71,13 @@ window.gui = async function(options){
7171
}
7272

7373
// 🚀 Launch the GUI 🚀
74-
window.initgui();
74+
window.initgui(options);
7575
}
7676

7777
/**
7878
* Dynamically loads an external JavaScript file.
7979
* @param {string} url The URL of the external script to load.
80-
* @param {Object} [options] Optional configuration for the script.
80+
* @param {Object} [options] Optional configuration for the script.
8181
* @param {boolean} [options.isModule] Whether the script is a module.
8282
* @param {boolean} [options.defer] Whether the script should be deferred.
8383
* @param {Object} [options.dataAttributes] An object containing data attributes to add to the script element.

src/initgui.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ import { SettingsService } from './services/SettingsService.js';
4343

4444
import UIComponentWindow from './UI/UIComponentWindow.js';
4545
import update_mouse_position from './helpers/update_mouse_position.js';
46+
import { LaunchOnInitService } from './services/LaunchOnInitService.js';
4647

4748

48-
const launch_services = async function () {
49+
const launch_services = async function (options) {
4950
// === Services Data Structures ===
5051
const services_l_ = [];
5152
const services_m_ = {};
@@ -78,14 +79,17 @@ const launch_services = async function () {
7879
register('process', new ProcessService());
7980
register('locale', new LocaleService());
8081
register('settings', new SettingsService());
82+
register('__launch-on-init', new LaunchOnInitService());
8183

8284
// === Service-Script Services ===
8385
for (const [name, script] of service_script_deferred.services) {
8486
register(name, script);
8587
}
8688

8789
for (const [_, instance] of services_l_) {
88-
await instance.construct();
90+
await instance.construct({
91+
gui_params: options,
92+
});
8993
}
9094

9195
for (const [_, instance] of services_l_) {
@@ -135,7 +139,7 @@ if(jQuery){
135139
};
136140
}
137141

138-
window.initgui = async function(){
142+
window.initgui = async function(options){
139143
let url = new URL(window.location);
140144
url = url.href;
141145

@@ -222,7 +226,7 @@ window.initgui = async function(){
222226

223227

224228
// Launch services before any UI is rendered
225-
await launch_services();
229+
await launch_services(options);
226230

227231
//--------------------------------------------------------------------------------------
228232
// Is GUI embedded in a popup?

src/services/LaunchOnInitService.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import UIAlert from "../UI/UIAlert.js";
2+
3+
import { Service } from "../definitions.js";
4+
5+
export class LaunchOnInitService extends Service {
6+
_construct () {
7+
this.commands = {
8+
'window-call': ({ fn_name, args }) => {
9+
window[fn_name](...args);
10+
}
11+
};
12+
}
13+
async _init () {
14+
const launch_options = this.$puter.gui_params.launch_options;
15+
if ( ! launch_options ) return;
16+
17+
if ( launch_options.on_initialized ) {
18+
for ( const command of launch_options.on_initialized ) {
19+
console.log('running', command)
20+
this.run_(command);
21+
}
22+
}
23+
}
24+
25+
run_ (command) {
26+
const args = { ...command };
27+
delete args.$;
28+
this.commands[command.$](args);
29+
}
30+
}

0 commit comments

Comments
 (0)