Skip to content

Commit

Permalink
feat: add /show urls
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jun 16, 2024
1 parent 5d214c7 commit 079e25a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
17 changes: 16 additions & 1 deletion packages/backend/src/routers/_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ router.all('*', async function(req, res, next) {
else{
let canonical_url = config.origin + path;
let app_name, app_title, description;
let launch_options = {
on_initialized: []
};

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

path = '/';
}
else if (path.startsWith('/show/')) {
const filepath = path.slice('/show'.length);
launch_options.on_initialized.push({
$: 'window-call',
fn_name: 'launch_app',
args: [{
name: 'explorer',
path: filepath,
}],
});
path = '/';
}

const manifest =
_fs.existsSync(_path.join(config.assets.gui, 'puter-gui.json'))
Expand All @@ -308,7 +323,7 @@ router.all('*', async function(req, res, next) {
short_description: config.short_description,
company: 'Puter Technologies Inc.',
canonical_url: canonical_url,
});
}, launch_options);
}

// /dist/...
Expand Down
7 changes: 6 additions & 1 deletion packages/backend/src/services/PuterHomepageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PuterHomepageService extends BaseService {
this.service_scripts.push(url);
}

async send (res, meta) {
async send (res, meta, launch_options) {
const config = this.global_config;
return res.send(this.generate_puter_page_html({
env: config.env,
Expand All @@ -47,6 +47,9 @@ class PuterHomepageService extends BaseService {
// page meta
meta,

// launch options
launch_options,

// gui parameters
gui_params: {
app_name_regex: config.app_name_regex,
Expand Down Expand Up @@ -80,6 +83,7 @@ class PuterHomepageService extends BaseService {
api_origin,

meta,
launch_options,

gui_params,
}) {
Expand All @@ -101,6 +105,7 @@ class PuterHomepageService extends BaseService {
gui_params = {
...meta,
...gui_params,
launch_options,
app_origin,
api_origin,
gui_origin: app_origin,
Expand Down
4 changes: 3 additions & 1 deletion src/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export class Service {
construct () {
construct (o) {
this.$puter = {};
for ( const k in o ) this.$puter[k] = o[k];
if ( ! this._construct ) return;
return this._construct();
}
Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Expand All @@ -21,21 +21,21 @@ window.puter_gui_enabled = true;

/**
* Initializes and configures the GUI (Graphical User Interface) settings based on the provided options.
*
*
* The function sets global variables in the window object for various settings such as origins and domain names.
* It also handles loading different resources depending on the environment (development or production).
*
*
* @param {Object} options - Configuration options to initialize the GUI.
* @param {string} [options.gui_origin='https://puter.com'] - The origin URL for the GUI.
* @param {string} [options.api_origin='https://api.puter.com'] - The origin URL for the API.
* @param {number} [options.max_item_name_length=500] - Maximum allowed length for an item name.
* @param {boolean} [options.require_email_verification_to_publish_website=true] - Flag to decide whether email verification is required to publish a website.
*
*
* @property {string} [options.app_domain] - Extracted domain name from gui_origin. It's derived automatically if not provided.
* @property {string} [window.gui_env] - The environment in which the GUI is running (e.g., "dev" or "prod").
*
*
* @returns {Promise<void>} Returns a promise that resolves when initialization and resource loading are complete.
*
*
* @example
* window.gui({
* gui_origin: 'https://myapp.com',
Expand All @@ -59,7 +59,7 @@ window.gui = async function(options){
await window.loadScript('/sdk/puter.dev.js');
await window.loadScript(`${options.asset_dir}/initgui.js`, {isModule: true});
}

// PROD: load the minified bundles if we are in production mode
// note: the order of the bundles is important
// note: Build script will prepend `window.gui_env="prod"` to the top of the file
Expand All @@ -71,13 +71,13 @@ window.gui = async function(options){
}

// 🚀 Launch the GUI 🚀
window.initgui();
window.initgui(options);
}

/**
* Dynamically loads an external JavaScript file.
* @param {string} url The URL of the external script to load.
* @param {Object} [options] Optional configuration for the script.
* @param {Object} [options] Optional configuration for the script.
* @param {boolean} [options.isModule] Whether the script is a module.
* @param {boolean} [options.defer] Whether the script should be deferred.
* @param {Object} [options.dataAttributes] An object containing data attributes to add to the script element.
Expand Down
12 changes: 8 additions & 4 deletions src/initgui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ import { SettingsService } from './services/SettingsService.js';

import UIComponentWindow from './UI/UIComponentWindow.js';
import update_mouse_position from './helpers/update_mouse_position.js';
import { LaunchOnInitService } from './services/LaunchOnInitService.js';


const launch_services = async function () {
const launch_services = async function (options) {
// === Services Data Structures ===
const services_l_ = [];
const services_m_ = {};
Expand Down Expand Up @@ -78,14 +79,17 @@ const launch_services = async function () {
register('process', new ProcessService());
register('locale', new LocaleService());
register('settings', new SettingsService());
register('__launch-on-init', new LaunchOnInitService());

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

for (const [_, instance] of services_l_) {
await instance.construct();
await instance.construct({
gui_params: options,
});
}

for (const [_, instance] of services_l_) {
Expand Down Expand Up @@ -135,7 +139,7 @@ if(jQuery){
};
}

window.initgui = async function(){
window.initgui = async function(options){
let url = new URL(window.location);
url = url.href;

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


// Launch services before any UI is rendered
await launch_services();
await launch_services(options);

//--------------------------------------------------------------------------------------
// Is GUI embedded in a popup?
Expand Down
30 changes: 30 additions & 0 deletions src/services/LaunchOnInitService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import UIAlert from "../UI/UIAlert.js";

import { Service } from "../definitions.js";

export class LaunchOnInitService extends Service {
_construct () {
this.commands = {
'window-call': ({ fn_name, args }) => {
window[fn_name](...args);
}
};
}
async _init () {
const launch_options = this.$puter.gui_params.launch_options;
if ( ! launch_options ) return;

if ( launch_options.on_initialized ) {
for ( const command of launch_options.on_initialized ) {
console.log('running', command)
this.run_(command);
}
}
}

run_ (command) {
const args = { ...command };
delete args.$;
this.commands[command.$](args);
}
}

0 comments on commit 079e25a

Please sign in to comment.