npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

service-config

v0.1.0

Published

Service Configuration Utility

Downloads

17

Readme

service-config

This module was designed for use in NodeJS Raspberry Pi projects to be able to install NodeJS code as a 'systemctl' service using 'parameterized' systemctl service file where only a single parameter is allowed during service install. Prior to development, I experimented with the 'commander' NPM module. However, I found it to be overkill for what I needed. I wanted a simple fly-weight approach to loading a set of named properties that are considered a "profile".

Configuration

Runtime configuration achieved using is a JSON file defining all named profiles plus a 'static' or common profile.

yahooWeatherRSS_Config.json

{
	"static":{
		"appId":"<appid>",
		"key":"<apikey>",
		"secret":"<apisecret>",
		"debug":false
	},
	"profiles":{
		"8050":{
			"port":8050
		},
		"8060":{
			"port":8060
		},
		"8070":{
			"port":8070
		},
		"8080":{
			"port":8080
		},
		"8080_DEBUG":{
			"port":8080,
			"debug":true
		}
	}
}

Example Usage

This module provides for the use of a single profile name such that it will load and assemble the necessary "options" for running the program.

Here's a snippet of code illustrating usage.

yahooWeatherRSS.js

#!/usr/bin/env node
'using strict';

const PROFILE_NAME_DEFAULT = "8080";
const CONFIG_LOCATION_DEFAULT = "yahooWeatherRSS_Config.json";
const CACHE_TTL_SECS_DEFAULT = 60;

//=======================================
// Other omitted for brevity.
//=======================================

var _yahooWeatherRssRequestOAuth;
var _debug = false;
var _server = null;
var _cache = null;


function main() {
    let config = new ServiceConfigModule.Configuration(PROFILE_NAME_DEFAULT, __dirname, CONFIG_LOCATION_DEFAULT);
    config.load();
    let requestHeaders = {
        "X-Yahoo-App-Id": config.read("appId","")
    };
    _yahooWeatherRssRequestOAuth = new OAuth.OAuth(
    null,
    null,
    config.read("key",""),
    config.read("secret",""),
    '1.0',
    null,
    'HMAC-SHA1',
    null,
    requestHeaders
    );
    //create a server object:
    _debug = config.read("debug",false);
    let cacheTTLSecs = config.read("cacheTTLSecs", CACHE_TTL_SECS_DEFAULT);
    let cacheCheckPeriod = config.read("cacheCheckPeriod", cacheTTLSecs * 2);
    let cacheUseClones = config.read("cacheUseClones", false);

    // code omitted for brevity    
    _server = Http.createServer(requestHandler);
    _server.listen(config.read("port", PORT_DEFAULT));
}

main();

This module uses process.argv[2] to determine the profile name to use. Next, the object properties (fields) of the 'static' (or common) profile are added followed by any properties of the named profile.

So, using the above example, if profile '8080_DEBUG' is selected the following properties are assembled for use.

{
	"appId":"<appid>",
	"key":"<apikey>",
	"secret":"<apisecret>",
	"debug":true,
	"port":8080
},

Creating the service file

The service file and install / removal template examples.

[email protected]

[Unit]
Description=Yahoo Weather RSS Service
After=network.target

[Service]
ExecStart=/home/pi/node/yahooWeatherRSS/yahooWeatherRSS.js %i
Restart=always
User=pi

# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'

Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/pi/node/yahooWeatherRSS
StandardOutput=null
StandardError=journal
#StandardOutput=inherit
#StandardError=inherit

[Install]
WantedBy=multi-user.target

install-service.sh

#!/bin/bash
sudo cp [email protected] /etc/systemd/system
sudo systemctl enable yahooWeatherRSS@$1.service
sudo systemctl start yahooWeatherRSS@$1.service

remove-service.sh

#!/bin/bash

sudo systemctl stop yahooWeatherRSS@$1.service
sudo systemctl disable yahooWeatherRSS@$1.service
#sudo rm /etc/systemd/system/[email protected]
sudo systemctl daemon-reload

Service Management

From the linux BASH terminal shell you install and remove the service using the parameter name.

./install-service 8080_DEBUG

./remove-service 8080_DEBUG

Practical uses

I have multiple projects which I configure and deploy across various Raspberry Pi machines in my home. I must admit that the use cases for installing multiple instances of a Yahoo Weather RSS Proxy service on a single machine on different ports likely does not exist, but the functionality provided by this module allows me to quickly create and deploy any project using a standardized service framework.

Other Notes

I'm sure there are other ways to do this but this worked quite well for my needs. Here are some of the links I reviewed during R&D of this project.

Basic Concepts

https://fedoramagazine.org/systemd-template-unit-files/

Detailed linux man information about template units (from above link)

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Other links

https://askubuntu.com/questions/1077778/how-do-i-pass-flags-when-starting-a-systemd-service