Skip to content

Commit e301247

Browse files
committed
dev: add an example module and service
1 parent 82457fb commit e301247

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed

src/backend/exports.js

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const { PuterAIModule } = require("./src/modules/puterai/PuterAIModule.js");
3030
const { BroadcastModule } = require("./src/modules/broadcast/BroadcastModule.js");
3131
const { WebModule } = require("./src/modules/web/WebModule.js");
3232
const { Core2Module } = require("./src/modules/core/Core2Module.js");
33+
const { TemplateModule } = require("./src/modules/template/TemplateModule.js");
3334

3435

3536
module.exports = {
@@ -49,6 +50,7 @@ module.exports = {
4950
Core2Module,
5051
CoreModule,
5152
WebModule,
53+
TemplateModule,
5254
],
5355

5456
// Pre-built modules
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# TemplateModule
2+
3+
This is a template module that you can copy and paste to create new modules.
4+
5+
This module is also included in `EssentialModules`, which means it will load
6+
when Puter boots. If you're just testing something, you can add it here
7+
temporarily.
8+
9+
## Services
10+
11+
### TemplateService
12+
13+
This is a template service that you can copy and paste to create new services.
14+
You can also add to this service temporarily to test something.
15+
16+
#### Listeners
17+
18+
##### `install.routes`
19+
20+
TemplateService listens to this event to provide an example endpoint
21+
22+
##### `boot.consolidation`
23+
24+
TemplateService listens to this event to provide an example event
25+
26+
##### `boot.activation`
27+
28+
TemplateService listens to this event to show you that it's here
29+
30+
##### `start.webserver`
31+
32+
TemplateService listens to this event to show you that it's here
33+
34+
## Libraries
35+
36+
### hello_world
37+
38+
#### Functions
39+
40+
##### `hello_world`
41+
42+
This is a simple function that returns a string.
43+
You can probably guess what string it returns.
44+
45+
## Notes
46+
47+
### Outside Imports
48+
49+
This module has external relative imports. When these are
50+
removed it may become possible to move this module to an
51+
extension.
52+
53+
**Imports:**
54+
- `../../util/context.js`
55+
- `../../services/BaseService` (use.BaseService)
56+
- `../../util/expressutil`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2024 Puter Technologies Inc.
3+
*
4+
* This file is part of Puter.
5+
*
6+
* Puter is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
const { AdvancedBase } = require("@heyputer/putility");
21+
22+
/**
23+
* This is a template module that you can copy and paste to create new modules.
24+
*
25+
* This module is also included in `EssentialModules`, which means it will load
26+
* when Puter boots. If you're just testing something, you can add it here
27+
* temporarily.
28+
*/
29+
class TemplateModule extends AdvancedBase {
30+
async install (context) {
31+
// === LIBS === //
32+
const useapi = context.get('useapi');
33+
34+
const lib = require('./lib/__lib__.js');
35+
36+
// In extensions: use('workinprogress').hello_world();
37+
// In services classes: see TemplateService.js
38+
useapi.def(`workinprogress`, lib, { assign: true });
39+
40+
useapi.def('core.context', require('../../util/context.js').Context);
41+
42+
// === SERVICES === //
43+
const services = context.get('services');
44+
45+
const { TemplateService } = require('./TemplateService.js');
46+
services.registerService('template-service', TemplateService);
47+
}
48+
49+
}
50+
51+
module.exports = {
52+
TemplateModule
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2024 Puter Technologies Inc.
3+
*
4+
* This file is part of Puter.
5+
*
6+
* Puter is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
// TODO: import via `USE` static member
21+
const BaseService = require("../../services/BaseService");
22+
const { Endpoint } = require("../../util/expressutil");
23+
24+
/**
25+
* This is a template service that you can copy and paste to create new services.
26+
* You can also add to this service temporarily to test something.
27+
*/
28+
class TemplateService extends BaseService {
29+
static USE = {
30+
// - Defined by lib/__lib__.js,
31+
// - Exposed to `useapi` by TemplateModule.js
32+
workinprogress: 'workinprogress'
33+
}
34+
35+
_construct () {
36+
// Use this override to initialize instance variables.
37+
}
38+
39+
async _init () {
40+
// This is where you initialize the service and prepare
41+
// for the consolidation phase.
42+
this.log.info("I am the template service.");
43+
}
44+
45+
/**
46+
* TemplateService listens to this event to provide an example endpoint
47+
*/
48+
['__on_install.routes'] (_, { app }) {
49+
this.log.info("TemplateService get the event for installing endpoint.");
50+
Endpoint({
51+
route: '/example-endpoint',
52+
methods: ['GET'],
53+
handler: async (req, res) => {
54+
res.send(this.workinprogress.hello_world());
55+
}
56+
}).attach(app);
57+
// ^ Don't forget to attach the endpoint to the app!
58+
// it's very easy to forget this step.
59+
}
60+
61+
/**
62+
* TemplateService listens to this event to provide an example event
63+
*/
64+
['__on_boot.consolidation'] () {
65+
// At this stage, all services have been initialized and it is
66+
// safe to start emitting events.
67+
this.log.info("TemplateService sees consolidation boot phase.");
68+
69+
const svc_event = this.services.get('event');
70+
71+
svc_event.on('template-service.hello', (_eventid, event_data) => {
72+
this.log.info('template-service said hello to itself; this is expected', {
73+
event_data,
74+
});
75+
});
76+
77+
svc_event.emit('template-service.hello', {
78+
message: 'Hello all you other services! I am the template service.'
79+
});
80+
}
81+
/**
82+
* TemplateService listens to this event to show you that it's here
83+
*/
84+
['__on_boot.activation'] () {
85+
this.log.info("TemplateService sees activation boot phase.");
86+
}
87+
88+
/**
89+
* TemplateService listens to this event to show you that it's here
90+
*/
91+
['__on_start.webserver'] () {
92+
this.log.info("TemplateService sees it's time to start web servers.");
93+
}
94+
}
95+
96+
module.exports = {
97+
TemplateService
98+
};
99+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
hello_world: require('./hello_world.js'),
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
/**
3+
* This is a simple function that returns a string.
4+
* You can probably guess what string it returns.
5+
*/
6+
const hello_world = () => {
7+
return "Hello, world!";
8+
}
9+
10+
module.exports = hello_world;

0 commit comments

Comments
 (0)