|
| 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 | + |
0 commit comments