Saturday, November 30, 2024

Node.js

 ブラウザーを使わないでJavaScriptを実行できる環境(だと理解してますが、、、間違っているかも・・・)Node.js というのを試してみた。

最近はWeb開発や自動化などに利用されることが多いとか。

Node.js is a run-time engine that executes JavaScript code outside a browser. Originally intended as a web server, but also commonly used for web development tools and automation.

https://nodejs.org/en/からWindows版をダウンロードして全てディフォルトでインストール:


インストール後、コマンドラインでインストールされているか確認(バージョン情報がでました)。

簡単なWebサーバーを立ち上げ:

import http from 'http';

http.createServer((req,res) => {

    res.writeHead(200, {'Content-Type': 'text/plain'});

    res.end('Aloha world');

}).listen(process.env.PORT || 3000);

node.js をインストールすると、npm というパッケージ化ユーティリティ(だと思う。。。)が入っていたので、npm init でパッケージも作成してみた。

package.json

{
  "name": "ほげほげ",
  "version": "1.0.0",
  "description": "demo node application",
  "main": "index.js",
  "type": "module",
  "scripts": {
  "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/tou-shiro/"
  },
  "author": "ほげ~",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/tou-shiro/"
  },
  "homepage": "https://github.com/tou-shiro/"
}

index.js

import http from 'http';
http.createServer((req,res) => {
    var path = req.url.toLowerCase();    
    switch(path) {
        case '/':
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Home page');
            break;
        case '/about':
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('About page');
            break;
        default:
            res.writeHead(404, {'Content-Type': 'text/plain'});
            res.end('Not found');
            break;
    }    
}).listen(process.env.PORT || 3000);

http://localhost:3000でつないでみたら、正しく表示されたので動いていることを確認できました。

Monday, November 25, 2024

systemd vs initd

昔はサービス再起動は service restart httpd のようにしていたが、いつのまにか sudo systemctl restart httpd という風に変わっていたので、いつ変わったのか AI に聞いてみた。


The change from the service command to systemctl in Linux occurred with the adoption of systemd as the default init system, replacing the older SysVinit system.

Timeline of Change:

  • Systemd Introduction: systemd was first introduced in 2010 by Lennart Poettering and Kay Sievers as an alternative to SysVinit.
  • Adoption in Major Distributions:
    • Fedora became the first major Linux distribution to adopt systemd as its default init system in Fedora 15 (released in May 2011).
    • Debian switched to systemd as the default init system starting with Debian 8 (Jessie), released in April 2015.
    • Ubuntu started using systemd as the default init system with Ubuntu 15.04 (released in April 2015).

Why the Change?

The change to systemctl was made to take advantage of the features and improvements that systemd brings over the older SysVinit system, including:

  • Parallel service startup (faster boot times).
  • More powerful service management with systemctl.
  • Better tracking and logging with journalctl.
  • Better handling of dependencies between services.

While service was part of the older SysVinit system, systemctl is part of the systemd system and is now the recommended tool for managing services on most modern Linux distributions.

Compatibility:

  • In many distributions that have switched to systemd, the service command is still available as a wrapper around systemctl for backward compatibility. However, using systemctl directly is the preferred method now.

Saturday, November 23, 2024

LeetCode 567 Permutation in String

コミュカレでイントロ Java の授業を取っていてだされた宿題に LeetCode 567 Permutation in String に近いものがあったのでトライしてみた。

11.2.1: LAB: All permutations of names

Write a program that lists all ways people can line up for a photo (all permutations of a list of Strings). The program will read a list of one word names into ArrayList nameList (until -1), and use a recursive method to create and output all possible orderings of those names separated by a comma, one ordering per line.

import java.util.Scanner;
import java.util.ArrayList;

public class PhotoLineups {

   // Method to create and output all permutations of the list of names
   public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) {
      // Base case: when nameList is empty, print the current permutation
      if (nameList.size() == 0) {
         System.out.println(String.join(", ", permList));
         return;
      }

      // Iterate through the list and recursively generate all permutations
      for (int i = 0; i < nameList.size(); i++) {
         // Select the current name
         String currentName = nameList.get(i);

         // Create a new list of remaining names by removing the current name
         ArrayList<String> remainingNames = new ArrayList<>(nameList);
         remainingNames.remove(i);

         // Add the current name to the permutation list
         permList.add(currentName);

         // Recurse with the remaining names and current permutation list
         printAllPermutations(permList, remainingNames);

         // Backtrack: remove the last added name to try the next permutation
         permList.remove(permList.size() - 1);
      }
   }

   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      ArrayList<String> nameList = new ArrayList<>();
      ArrayList<String> permList = new ArrayList<>();
      String name;

      // Read names from the user until "-1" is entered
      while (true) {
         name = scnr.next().trim();
         if (name.equals("-1")) {
            break;  // Stop reading names when "-1" is encountered
         }
         nameList.add(name);  // Add name to the list
      }

      // Generate and print all permutations of the list of names
      printAllPermutations(permList, nameList);
   }
}