├── .gitignore ├── docs ├── assets │ └── favicon.png ├── index.md ├── ubuntu-server │ ├── cloudflare-nginx.md │ ├── remove-packages.md │ └── setting-up.md ├── postgresql │ └── setting-up.md ├── sql │ ├── standard-data-types.md │ └── standard-functions.md ├── dotnet │ ├── manipulating-files.md │ └── cli-commands.md ├── patterns │ ├── observer-pattern.md │ └── finite-state-machines.md └── resources.md ├── .editorconfig ├── Dockerfile ├── .vscode ├── extensions.json └── settings.json ├── .markdownlint.json ├── package.json ├── .github ├── dependabot.yml └── workflows │ └── ci.yaml ├── Makefile ├── README.md └── mkdocs.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | site/ 2 | .cache/ 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /docs/assets/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/casantosmu/code-notes/HEAD/docs/assets/favicon.png -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | charset = utf-8 3 | insert_final_newline = true 4 | end_of_line = lf 5 | indent_style = space 6 | indent_size = 2 7 | max_line_length = 80 8 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM squidfunk/mkdocs-material:9.5.34 2 | RUN pip install mkdocs-git-revision-date-localized-plugin 3 | RUN pip install mkdocs-meta-descriptions-plugin 4 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "esbenp.prettier-vscode", 4 | "DavidAnson.vscode-markdownlint", 5 | "streetsidesoftware.code-spell-checker" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "MD013": false, 3 | "MD046": false, 4 | "MD025": { 5 | "front_matter_title": "" 6 | }, 7 | "MD024": { 8 | "siblings_only": true 9 | }, 10 | "MD030": false 11 | } 12 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.codeActionsOnSave": { 3 | "source.fixAll.markdownlint": "explicit" 4 | }, 5 | "editor.formatOnSave": true, 6 | "editor.defaultFormatter": "esbenp.prettier-vscode", 7 | "cSpell.language": "en", 8 | "cSpell.words": [] 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "code-notes", 3 | "scripts": { 4 | "format": "prettier . --write && markdownlint-cli2 \"**/*.md\" \"#node_modules\" --fix", 5 | "lint": "prettier . --check && markdownlint-cli2 \"**/*.md\" \"#node_modules\"" 6 | }, 7 | "devDependencies": { 8 | "markdownlint-cli2": "^0.13.0", 9 | "prettier": "3.3.3" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "npm" 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | 8 | - package-ecosystem: "docker" 9 | directory: "/" 10 | schedule: 11 | interval: "monthly" 12 | 13 | - package-ecosystem: "github-actions" 14 | directory: "/" 15 | schedule: 16 | interval: "monthly" 17 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Welcome to Code Notes 2 | 3 | Welcome to a curated collection of insights and practical tips on various programming topics. 4 | 5 | Here, you'll find in-depth notes and guides on subjects ranging from security best practices in web applications to optimizing database performance and configuring modern development tools. 6 | 7 | Feel free to browse and make the most out of these notes. 8 | 9 | Happy coding! 🚀 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DOCKER_IMAGE = code-notes 2 | 3 | DEV_PORT = 8000 4 | 5 | PWD_DIR = $(shell pwd) 6 | 7 | .DEFAULT_GOAL := help 8 | 9 | dev: 10 | @echo "Starting the development server on port $(DEV_PORT)..." 11 | @docker build -t $(DOCKER_IMAGE) . 12 | @docker run --rm -it -p $(DEV_PORT):$(DEV_PORT) -v $(PWD_DIR):/docs $(DOCKER_IMAGE) 13 | 14 | build: 15 | @echo "Building the documentation..." 16 | @docker build -t $(DOCKER_IMAGE) . 17 | @docker run --rm -v $(PWD_DIR):/docs $(DOCKER_IMAGE) build 18 | 19 | help: 20 | @echo "Usage: make [target]" 21 | @echo 22 | @echo "Available targets:" 23 | @echo " dev - Start the development server" 24 | @echo " build - Build the documentation" 25 | @echo " help - Show this help message (default)" 26 | 27 | .PHONY: dev build help 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Notes 2 | 3 | Welcome to **Code Notes**, a personal collection of insights and practical tips on various programming topics. 4 | 5 | 👉 **Explore the Docs**: [Code Notes](https://code-notes.casantosmu.com) 6 | 7 | ## Run Locally 8 | 9 | To view the documentation on your local machine: 10 | 11 | 1. **Start the development server**: 12 | 13 | ```sh 14 | make dev 15 | ``` 16 | 17 | 2. **Build the documentation**: 18 | 19 | ```sh 20 | make build 21 | ``` 22 | 23 | ## Built with MkDocs 24 | 25 | This documentation is created using [MkDocs](https://www.mkdocs.org) with the Material theme. 26 | 27 | ## Contributing 28 | 29 | This is a personal project, but suggestions and improvements are welcome. Feel free to submit issues or pull requests. 30 | 31 | Happy coding! 🚀 32 | -------------------------------------------------------------------------------- /docs/ubuntu-server/cloudflare-nginx.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Setting Up Cloudflare and Nginx 3 | description: Step-by-step guide to hosting a website with Cloudflare and Nginx on Ubuntu, including visitor IP restoration and automating Cloudflare IP updates. 4 | --- 5 | 6 | # Setting Up Cloudflare and Nginx 7 | 8 | To host a website using Cloudflare and Nginx on Ubuntu 22.04, refer to this [tutorial](https://www.digitalocean.com/community/tutorials/how-to-host-a-website-using-cloudflare-and-nginx-on-ubuntu-22-04). 9 | 10 | To be able to see the IP of the visitors, refer to the [official Cloudflare documentation](https://developers.cloudflare.com/support/troubleshooting/restoring-visitor-ips/restoring-original-visitor-ips/). To automate the process of updating the Cloudflare IP addresses, use the script on [nginx-cloudflare-real-ip](https://github.com/ergin/nginx-cloudflare-real-ip). 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | lint: 11 | name: Run Prettier and MarkdownLint 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout Code 16 | uses: actions/checkout@v4 17 | 18 | - name: Set Up Node.js 19 | uses: actions/setup-node@v4 20 | with: 21 | node-version: "20.x" 22 | cache: "npm" 23 | 24 | - name: Install Dependencies 25 | run: npm ci 26 | 27 | - name: Run lint script 28 | run: npm run lint 29 | 30 | test-build: 31 | name: Test build 32 | runs-on: ubuntu-latest 33 | 34 | steps: 35 | - name: Checkout code 36 | uses: actions/checkout@v4 37 | with: 38 | fetch-depth: 0 39 | 40 | - name: Build Docker image 41 | run: docker build -t code-notes . 42 | 43 | - name: Build MkDocs site 44 | run: docker run --rm -v $(pwd):/docs code-notes build --strict 45 | -------------------------------------------------------------------------------- /docs/ubuntu-server/remove-packages.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Removing Packages on Ubuntu/Debian 3 | description: Guide to removing DEB and Snap packages on Ubuntu/Debian, including purging packages, cleaning dependencies, and removing residual files. 4 | --- 5 | 6 | # Removing Packages on Ubuntu/Debian 7 | 8 | ## Removing DEB Packages 9 | 10 | **Remove or purge a package**: 11 | 12 | To remove the package (but keep config files): 13 | 14 | ```bash 15 | sudo apt remove package_name 16 | ``` 17 | 18 | To purge the package (remove everything, including config files): 19 | 20 | ```bash 21 | sudo apt purge package_name 22 | ``` 23 | 24 | **Clean up unnecessary dependencies**: 25 | 26 | ```bash 27 | sudo apt autoremove 28 | ``` 29 | 30 | ## Removing Snap Packages 31 | 32 | To remove the Snap package 33 | 34 | ```bash 35 | sudo snap remove package_name 36 | ``` 37 | 38 | To remove and purge all traces 39 | 40 | ```bash 41 | sudo snap remove --purge package_name 42 | ``` 43 | 44 | ## Finding and Removing Residual Files 45 | 46 | Search for remaining configuration files: 47 | 48 | ```bash 49 | sudo find / -type d -iname '*package_name*' 2>/dev/null 50 | ``` 51 | 52 | Manually delete found directories (use with caution): 53 | 54 | ```bash 55 | sudo rm -rf /path/to/directory 56 | ``` 57 | -------------------------------------------------------------------------------- /docs/postgresql/setting-up.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Setting Up PostgreSQL: Installation & Configuration" 3 | description: "Guide to securely setting up PostgreSQL: download, install, configure access, create databases and users, and manage schema security for robust data protection." 4 | --- 5 | 6 | # Setting Up PostgreSQL 7 | 8 | ## Installation 9 | 10 | First [download PostgreSQL](https://www.postgresql.org/download/) on your operating system. 11 | 12 | ## Secure Database Configuration 13 | 14 | To ensure your database configuration is secure, do not allow access from all internet sources. Avoid settings such as `0.0.0.0/0` in `pg_hba.conf` and `listen_addresses='*'` in `postgresql.conf`. 15 | 16 | For remote connections, use an SSH tunnel. 17 | 18 | **Example**: 19 | 20 | ```bash 21 | ssh -L 5432:localhost:5432 user@remote_host 22 | ``` 23 | 24 | ## Creating Database and User 25 | 26 | Create a new database and user by executing the following commands: 27 | 28 | ```bash 29 | sudo -u postgres psql 30 | ``` 31 | 32 | ```sql 33 | CREATE USER WITH PASSWORD 'securepassword'; 34 | CREATE DATABASE OWNER ; 35 | ``` 36 | 37 | ## Secure Schema Usage 38 | 39 | To enhance security, constrain ordinary users to user-private schemas. Ensure that no schemas have public CREATE privileges. For each user who needs to create non-temporary objects, create a schema with the same name as the user. 40 | 41 | **Example**: 42 | 43 | ```sql 44 | CREATE SCHEMA AUTHORIZATION ; 45 | ``` 46 | 47 | This setup ensures that users access their own schemas by default. 48 | 49 | !!! info "PostgreSQL 14 or earlier" 50 | 51 | For PostgreSQL 14, earlier versions or upgraded databases to PostgreSQL 15 and later, remove the public CREATE privilege from the public schema: 52 | 53 | ```sql 54 | REVOKE CREATE ON SCHEMA public FROM PUBLIC; 55 | ``` 56 | 57 | **Documentation**: 58 | 59 | > [PostgreSQL Documentation on Schemas Patterns](https://www.postgresql.org/docs/16/ddl-schemas.html#DDL-SCHEMAS-PATTERNS) 60 | -------------------------------------------------------------------------------- /mkdocs.yaml: -------------------------------------------------------------------------------- 1 | site_name: Code Notes 2 | site_description: A personal collection of insights and practical tips on various programming topics. 3 | site_author: Carlos Santos 4 | site_url: https://code-notes.casantosmu.com 5 | repo_url: https://github.com/casantosmu/code-notes 6 | 7 | nav: 8 | - Home: index.md 9 | - .NET: 10 | - CLI Commands: dotnet/cli-commands.md 11 | - Manipulating Files: dotnet/manipulating-files.md 12 | - Patterns: 13 | - "State Machines: Example": patterns/finite-state-machines.md 14 | - "The Observer Pattern: Example": patterns/observer-pattern.md 15 | - SQL: 16 | - Standard Data Types: sql/standard-data-types.md 17 | - Standard Functions: sql/standard-functions.md 18 | - PostgreSQL: 19 | - Setting Up: postgresql/setting-up.md 20 | - Ubuntu Server: 21 | - Setting Up: ubuntu-server/setting-up.md 22 | - Cloudflare and Nginx: ubuntu-server/cloudflare-nginx.md 23 | - Remove Packages: ubuntu-server/remove-packages.md 24 | - Resources: resources.md 25 | 26 | theme: 27 | name: material 28 | favicon: assets/favicon.png 29 | icon: 30 | logo: material/file-document 31 | palette: 32 | - media: "(prefers-color-scheme: light)" 33 | scheme: default 34 | toggle: 35 | icon: material/brightness-7 36 | name: Switch to dark mode 37 | - media: "(prefers-color-scheme: dark)" 38 | scheme: slate 39 | toggle: 40 | icon: material/brightness-4 41 | name: Switch to light mode 42 | features: 43 | - content.code.copy 44 | 45 | plugins: 46 | - privacy 47 | - search 48 | - git-revision-date-localized 49 | - minify: 50 | minify_html: true 51 | - meta-descriptions: 52 | enable_checks: true 53 | 54 | markdown_extensions: 55 | - toc: 56 | permalink: true 57 | 58 | # Admonitions 59 | - admonition 60 | - pymdownx.details 61 | - pymdownx.superfences 62 | 63 | # Code blocks 64 | - pymdownx.highlight: 65 | anchor_linenums: true 66 | line_spans: __span 67 | pygments_lang_class: true 68 | - pymdownx.inlinehilite 69 | - pymdownx.snippets 70 | - pymdownx.superfences 71 | 72 | validation: 73 | omitted_files: warn 74 | absolute_links: warn 75 | unrecognized_links: warn 76 | anchors: warn 77 | -------------------------------------------------------------------------------- /docs/ubuntu-server/setting-up.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Setting Up an Ubuntu Server 3 | description: "Guide to setting up an Ubuntu server: server access, updates, user creation, SSH configuration, firewall setup, disabling root login, and installing Fail2ban." 4 | --- 5 | 6 | # Setting Up an Ubuntu Server 7 | 8 | ## 1. Accessing the Server 9 | 10 | Access the server using an SSH client, substituting `server_ip` with the server IP address: 11 | 12 | ```bash 13 | ssh root@server_ip 14 | ``` 15 | 16 | ## 2. Updating the Server 17 | 18 | Update the server to ensure all packages are up to date: 19 | 20 | ```bash 21 | apt update && apt upgrade -y 22 | ``` 23 | 24 | ## 3. Creating a New User 25 | 26 | Create a new user with sudo privileges to avoid using the root account: 27 | 28 | ```bash 29 | adduser newuser 30 | usermod -aG sudo newuser 31 | ``` 32 | 33 | ## 4. Configuring SSH Access 34 | 35 | Configure SSH access by copying SSH keys from the root user to the new user: 36 | 37 | ```bash 38 | rsync --archive --chown=newuser:newuser ~/.ssh /home/newuser 39 | ``` 40 | 41 | Update SSH configuration to disable root login and password authentication: 42 | 43 | ```bash 44 | nano /etc/ssh/sshd_config 45 | ``` 46 | 47 | Make the following changes: 48 | 49 | ```plaintext 50 | PermitRootLogin no 51 | PasswordAuthentication no 52 | ``` 53 | 54 | ???+ note 55 | 56 | In Ubuntu 22.04.1 LTS, the `/etc/ssh/sshd_config.d/50-cloud-init.conf` file might override settings from `sshd_config`, including the `PasswordAuthentication` setting. To ensure that `PasswordAuthentication` is disabled, review and adjust the settings in this file. 57 | 58 | Restart the SSH service: 59 | 60 | ```bash 61 | systemctl restart ssh 62 | ``` 63 | 64 | ## 5. Setting Up a Firewall 65 | 66 | Configure the firewall to allow essential services: 67 | 68 | ```bash 69 | ufw allow OpenSSH 70 | ufw enable 71 | ``` 72 | 73 | ## 6. Disabling Root Login 74 | 75 | Disable root login for additional security: 76 | 77 | ```bash 78 | passwd -l root 79 | ``` 80 | 81 | ## 7. Fail2ban 82 | 83 | Install Fail2ban to protect the server from brute-force attacks: 84 | 85 | ```bash 86 | apt install fail2ban -y 87 | ``` 88 | 89 | Start and enable Fail2ban: 90 | 91 | ```bash 92 | systemctl start fail2ban 93 | systemctl enable fail2ban 94 | ``` 95 | 96 | Check the status of Fail2ban to ensure it is running correctly: 97 | 98 | ```bash 99 | fail2ban-client status 100 | ``` 101 | 102 | ???+ note 103 | 104 | If SSH protection is not enabled, follow these steps: 105 | 106 | Create and edit the `jail.local` file: 107 | 108 | ```bash 109 | nano /etc/fail2ban/jail.local 110 | ``` 111 | 112 | Add the following lines: 113 | 114 | ```plaintext 115 | [sshd] 116 | enabled = true 117 | ``` 118 | 119 | Restart and check the status of Fail2ban: 120 | 121 | ```bash 122 | systemctl restart fail2ban 123 | fail2ban-client status 124 | ``` 125 | -------------------------------------------------------------------------------- /docs/sql/standard-data-types.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: SQL Standard Data Types 3 | description: A detailed overview of SQL standard data types, including descriptions and use cases for each type, such as BIGINT, BOOLEAN, CHAR, DATE, and more. 4 | --- 5 | 6 | # SQL Standard Data Types 7 | 8 | | **Data Type** | **Description** | 9 | | ------------------------------------------- | --------------------------------------------------------------------------------- | 10 | | **BIGINT** | An 8-byte integer. Used for large numerical values. | 11 | | **BIT** | A fixed-length bit string. | 12 | | **BIT VARYING** | A variable-length bit string. | 13 | | **BOOLEAN** | A logical Boolean value (TRUE or FALSE). | 14 | | **CHAR(n)** | A fixed-length character string, padded with spaces if shorter than n characters. | 15 | | **CHARACTER(n)** | Synonym for CHAR(n). | 16 | | **CHARACTER VARYING(n)** | A variable-length character string with a limit of n characters. | 17 | | **VARCHAR(n)** | Synonym for CHARACTER VARYING(n). | 18 | | **DATE** | A calendar date (year, month, day). | 19 | | **DOUBLE PRECISION** | A double-precision floating-point number (approximate numerical values). | 20 | | **INTEGER** | A standard 4-byte integer. | 21 | | **INTERVAL** | A time span or duration (e.g., interval '1 day'). | 22 | | **NUMERIC(p, s)** | A number with a fixed precision (p) and scale (s). Used for exact arithmetic. | 23 | | **DECIMAL(p, s)** | Synonym for NUMERIC(p, s). | 24 | | **REAL** | A single-precision floating-point number. | 25 | | **SMALLINT** | A 2-byte integer. | 26 | | **TIME [ (p) ] [ WITHOUT TIME ZONE ]** | A time of day (hours, minutes, seconds) with optional precision (p). | 27 | | **TIME [ (p) ] WITH TIME ZONE** | A time of day with time zone information and optional precision (p). | 28 | | **TIMESTAMP [ (p) ] [ WITHOUT TIME ZONE ]** | A date and time without time zone information, with optional precision (p). | 29 | | **TIMESTAMP [ (p) ] WITH TIME ZONE** | A date and time with time zone information, with optional precision (p). | 30 | | **XML** | XML data. Used to store XML documents or fragments. | 31 | -------------------------------------------------------------------------------- /docs/dotnet/manipulating-files.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Manipulating Files in .NET | C# Cheat Sheet 3 | description: Quick reference guide for working with files and directories in .NET Framework using C#. 4 | --- 5 | 6 | # Manipulating Files in .NET | C# Cheat Sheet 7 | 8 | ## Enumerating Directories 9 | 10 | ### List All Directories 11 | 12 | To list all directories in a given folder: 13 | 14 | ```csharp 15 | IEnumerable listOfDirectories = Directory.EnumerateDirectories("dir"); 16 | 17 | foreach (var dir in listOfDirectories) { 18 | Console.WriteLine(dir); 19 | } 20 | ``` 21 | 22 | ### List All Files in a Directory 23 | 24 | To list all files within a specific directory: 25 | 26 | ```csharp 27 | IEnumerable files = Directory.EnumerateFiles("dir"); 28 | 29 | foreach (var file in files) { 30 | Console.WriteLine(file); 31 | } 32 | ``` 33 | 34 | ### Recursively Enumerate Files in All Subdirectories 35 | 36 | To search through a directory and its subdirectories for specific files: 37 | 38 | ```csharp 39 | IEnumerable allFilesInAllFolders = Directory.EnumerateFiles("dir", "*.txt", SearchOption.AllDirectories); 40 | 41 | foreach (var file in allFilesInAllFolders) { 42 | Console.WriteLine(file); 43 | } 44 | ``` 45 | 46 | ## Working with Directories 47 | 48 | ### Get Current Directory 49 | 50 | To get the path of the current working directory: 51 | 52 | ```csharp 53 | Console.WriteLine(Directory.GetCurrentDirectory()); 54 | ``` 55 | 56 | ### Accessing Special Folders 57 | 58 | To retrieve paths to special system folders in a cross-platform way: 59 | 60 | ```csharp 61 | Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); 62 | ``` 63 | 64 | ### Creating Directories 65 | 66 | To create a directory and any necessary subdirectories: 67 | 68 | ```csharp 69 | Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "dir", "subDir", "newDir")); 70 | ``` 71 | 72 | ### Check If Directory Exists 73 | 74 | To check if a specific directory exists: 75 | 76 | ```csharp 77 | Console.WriteLine(Directory.Exists("dir")); // outputs: "true" 78 | ``` 79 | 80 | ## Working with File Paths 81 | 82 | ### Combining Paths 83 | 84 | To correctly combine multiple path segments: 85 | 86 | ```csharp 87 | Console.WriteLine(Path.Combine("dir", "subDir")); // outputs: dir/subDir 88 | ``` 89 | 90 | ### Get File Extension 91 | 92 | To retrieve the file extension of a given filename: 93 | 94 | ```csharp 95 | Console.WriteLine(Path.GetExtension("greeting.txt")); // outputs: .txt 96 | ``` 97 | 98 | ## Reading and Writing Files 99 | 100 | ### Reading a File 101 | 102 | To read all text from a file: 103 | 104 | ```csharp 105 | Console.WriteLine(File.ReadAllText("greeting.txt")); 106 | ``` 107 | 108 | ### Writing to a File 109 | 110 | To create or overwrite a file with specified content: 111 | 112 | ```csharp 113 | File.WriteAllText("greeting.txt", "Hello World Kim!"); 114 | ``` 115 | 116 | ### Appending to a File 117 | 118 | To append data to an existing file, or create it if it doesn’t exist: 119 | 120 | ```csharp 121 | File.AppendAllText("greeting.txt", $"Hello World Lucas!{Environment.NewLine}"); 122 | ``` 123 | 124 | ## Working with File Information 125 | 126 | ### Getting File Info 127 | 128 | To get detailed information about a file: 129 | 130 | ```csharp 131 | FileInfo info = new FileInfo("greeting.txt"); 132 | 133 | Console.WriteLine($"Full Name: {info.FullName}"); 134 | Console.WriteLine($"Directory: {info.Directory}"); 135 | Console.WriteLine($"Extension: {info.Extension}"); 136 | Console.WriteLine($"Create Date: {info.CreationTime}"); 137 | ``` 138 | -------------------------------------------------------------------------------- /docs/patterns/observer-pattern.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Understanding the Observer Pattern: A Counter UI Example" 3 | description: "Discover the Observer Pattern in software design. Understand its concepts and benefits through a practical JavaScript example." 4 | --- 5 | 6 | # Understanding the Observer Pattern: A Counter UI Example 7 | 8 | The Observer Pattern is a behavioral design pattern that establishes a one-to-many relationship between objects. When the state of one object (the subject) changes, all its dependents (observers) are notified and updated automatically. This pattern is particularly useful for designing systems where changes in one component need to be reflected in other components without tightly coupling them. 9 | 10 | ## Key Concepts 11 | 12 | - **Observable (Subject)**: The object that maintains the state and notifies observers of any changes. 13 | - **Observer**: The object that receives updates from the observable. 14 | 15 | ## Benefits 16 | 17 | - **Loose Coupling**: The subject and observers are loosely coupled, allowing for more flexible and maintainable code. 18 | - **Dynamic Relationships**: Observers can be added or removed at runtime, allowing for dynamic relationships between objects. 19 | 20 | ## Example: Counter UI in JavaScript 21 | 22 | Let's illustrate the Observer Pattern with a simple example of a counter UI. We will create a counter that updates its display whenever the value changes. 23 | 24 | ### HTML 25 | 26 | ```html 27 | 28 | 29 | 30 | 31 | 32 | Counter UI 33 | 34 | 35 | 36 |
0
37 | 38 | 39 | 40 | ``` 41 | 42 | ### JavaScript (counter.js) 43 | 44 | ```javascript 45 | class Observable { 46 | constructor() { 47 | this.observers = []; 48 | } 49 | 50 | subscribe(func) { 51 | this.observers.push(func); 52 | } 53 | 54 | unsubscribe(func) { 55 | this.observers = this.observers.filter((observer) => observer !== func); 56 | } 57 | 58 | notify(data) { 59 | for (const observer of this.observers) { 60 | observer(data); 61 | } 62 | } 63 | } 64 | 65 | class Counter extends Observable { 66 | constructor() { 67 | super(); 68 | this.count = 0; 69 | } 70 | 71 | increment() { 72 | this.count++; 73 | this.notify(this.count); 74 | } 75 | } 76 | 77 | const counter = new Counter(); 78 | const displayElement = document.querySelector("#counterDisplay"); 79 | 80 | counter.subscribe((data) => { 81 | displayElement.textContent = data; 82 | }); 83 | 84 | document.querySelector("#incrementButton").addEventListener("click", () => { 85 | counter.increment(); 86 | }); 87 | ``` 88 | 89 | ## Pub/Sub Pattern for Better Decoupling and Performance 90 | 91 | In scenarios where less coupling and better performance are desired, the **Pub/Sub (Publish/Subscribe) pattern** can be used. This pattern involves publishers and subscribers connected by channels. Channels are managed outside the code, in infrastructure, libraries, other processes, cloud services, etc., and are potentially asynchronous. 92 | 93 | ### Key Concepts of Pub/Sub 94 | 95 | - **Publisher**: Sends messages to a channel. 96 | - **Subscriber**: Receives messages from a channel. 97 | - **Channel**: The medium through which messages are sent and received. It is typically managed externally. 98 | 99 | ### Benefits 100 | 101 | - **Less Coupling**: Publishers and subscribers do not need to know about each other, leading to lower coupling. 102 | - **Scalability**: Since channels can be managed independently and can be asynchronous, the system can scale more effectively. 103 | -------------------------------------------------------------------------------- /docs/sql/standard-functions.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: SQL Standard Functions 3 | description: Overview of SQL standard functions, covering aggregation, text, mathematical, date/time, type conversion, flow control, and analytical functions with examples. 4 | --- 5 | 6 | # SQL Standard Functions 7 | 8 | | Category | Function | Description | 9 | | ------------------- | ------------------- | --------------------------------------------------------------------------------------------- | 10 | | **Aggregation** | `AVG()` | Calculates the average of a set of values. | 11 | | | `COUNT()` | Counts the number of rows in a result set. | 12 | | | `MAX()` | Returns the maximum value in a set of values. | 13 | | | `MIN()` | Returns the minimum value in a set of values. | 14 | | | `SUM()` | Sums up the values in a set of data. | 15 | | **Text** | `CHAR_LENGTH()` | Returns the length of a string in characters. | 16 | | | `LOWER()` | Converts a string to lowercase. | 17 | | | `UPPER()` | Converts a string to uppercase. | 18 | | | `SUBSTRING()` | Extracts a substring from a string. | 19 | | | `TRIM()` | Removes leading and trailing spaces or specified characters from a string. | 20 | | **Mathematical** | `ABS()` | Returns the absolute value of a number. | 21 | | | `CEILING()` | Rounds a number up to the nearest integer. | 22 | | | `FLOOR()` | Rounds a number down to the nearest integer. | 23 | | | `MOD()` | Returns the remainder of a division operation. | 24 | | **Date and Time** | `CURRENT_DATE` | Returns the current date. | 25 | | | `CURRENT_TIME` | Returns the current time. | 26 | | | `CURRENT_TIMESTAMP` | Returns the current date and time. | 27 | | | `EXTRACT()` | Extracts a specific part of a date, such as year, month, day, etc. | 28 | | **Type Conversion** | `CAST()` | Converts a value from one data type to another. | 29 | | | `CONVERT()` | Similar to `CAST()`, converts a value from one data type to another, with variations by DBMS. | 30 | | **Flow Control** | `CASE` | Conditional expression that returns a value based on different conditions. | 31 | | | `COALESCE()` | Returns the first non-null value in a list of arguments. | 32 | | **Analytical** | `ROW_NUMBER()` | Assigns a unique row number to each row in a result set, based on the specified order. | 33 | | | `RANK()` | Assigns a rank to each row in a result set, with possible ties. | 34 | | | `DENSE_RANK()` | Similar to `RANK()`, but without gaps in the rank for tied values. | 35 | 36 | Reference: [List of SQL reserved words - Wikipedia](https://en.wikipedia.org/wiki/List_of_SQL_reserved_words) 37 | -------------------------------------------------------------------------------- /docs/resources.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Resources for Development and DevOps 3 | description: "DevOps and development resources: React, Node.js, Go, testing, Docker, SQL, NoSQL, AWS, DevOps, security, NGINX, Git, shell scripting, HTML, CSS." 4 | --- 5 | 6 | # Resources 7 | 8 | ## React 9 | 10 | - **[Learn React - Escape Hatches](https://react.dev/learn/escape-hatches)** 11 | - **[Bulletproof React](https://github.com/alan2207/bulletproof-react)** 12 | - **[Tao of React - Software Design, Architecture & Best Practices](https://alexkondov.com/tao-of-react)** 13 | - **[Build your own React](https://pomb.us/build-your-own-react)** 14 | - **[Application State Management with React](https://kentcdodds.com/blog/application-state-management-with-react)** 15 | - **[Advanced React component composition](https://frontendmastery.com/posts/advanced-react-component-composition-guide)** 16 | 17 | ## Node 18 | 19 | - **[What the heck is the event loop anyway? | Philip Roberts](https://www.youtube.com/watch?v=8aGhZQkoFbQ)** (Video) 20 | - **[Tao of Node - Design, Architecture & Best Practices](https://alexkondov.com/tao-of-node)** 21 | - **[Node.js Best Practices](https://github.com/goldbergyoni/nodebestpractices)** 22 | - **[Node.js CLI Apps Best Practices](https://github.com/lirantal/nodejs-cli-apps-best-practices)** 23 | - **[Scaling Node.js applications](https://punits.dev/jargon-free-intros/scaling-nodejs-applications)** 24 | - **[Node.js Design Patterns](https://www.nodejsdesignpatterns.com)** (Book) 25 | 26 | ## Go 27 | 28 | - **[Effective Go](https://go.dev/doc/effective_go)** 29 | - **[Practical Go](https://dave.cheney.net/practical-go)** 30 | - **[Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments#go-code-review-comments)** 31 | 32 | ## Testing 33 | 34 | - **[JavaScript & Node.js Testing Best practices](https://github.com/goldbergyoni/javascript-testing-best-practices)** 35 | - **[Integration Test Best Practices](https://github.com/testjavascript/nodejs-integration-tests-best-practices)** 36 | - **[Unit Testing Principles, Practices, and Patterns](https://www.manning.com/books/unit-testing)** (Book) 37 | 38 | ## Docker 39 | 40 | - **[Docker and Node.js Best Practices](https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md)** 41 | - **[10 best practices to containerize Node.js web applications with Docker](https://snyk.io/blog/10-best-practices-to-containerize-nodejs-web-applications-with-docker)** 42 | 43 | ## SQL 44 | 45 | - **[SQL Anti-Patterns](https://github.com/boralp/sql-anti-patterns)** 46 | - **[SQL Antipatterns, Volume 1](https://pragprog.com/titles/bksap1/sql-antipatterns-volume-1)** (Book) 47 | 48 | ## NoSQL 49 | 50 | - **[7 Common DynamoDB Patterns for Modeling and Building an App with Alex De Brie](https://www.youtube.com/watch?v=Q6-qWdsa8a4)** (Video) 51 | - **[MongoDB - Building with Patterns: A Summary](https://www.mongodb.com/blog/post/building-with-patterns-a-summary)** 52 | - **[MongoDB - Data Model and Patterns](https://www.mongodb.com/docs/manual/core/data-modeling-introduction)** 53 | 54 | ## AWS 55 | 56 | - **[The Open Guide to Amazon Web Services](https://github.com/open-guides/og-aws)** 57 | - **[CDK Patterns](https://cdkpatterns.com)** 58 | 59 | ## DevOps 60 | 61 | - **[The Twelve Factors](https://12factor.net)** 62 | - **[Cloud-Native Schema Migrations](https://cloud-native-schema-migrations.github.io)** 63 | 64 | ## Security 65 | 66 | - **[OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/index.html)** 67 | - **[CS 253 Web Security](https://www.youtube.com/playlist?list=PL1y1iaEtjSYiiSGVlL1cHsXN_kvJOOhu-)** 68 | - **[JWT should not be your default for sessions](https://evertpot.com/jwt-is-a-bad-default)** 69 | - **[JWT are Dangerous for User Sessions](https://redis.com/blog/json-web-tokens-jwt-are-dangerous-for-user-sessions)** 70 | - **[Understanding CSRF](https://github.com/pillarjs/understanding-csrf)** 71 | 72 | ## NGINX 73 | 74 | - **[NGINX Pitfalls and Common Mistakes](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls)** 75 | - **[Nginx Admin's Handbook](https://github.com/trimstray/nginx-admins-handbook)** 76 | 77 | ## Git 78 | 79 | - **[Conventional Commits](https://www.conventionalcommits.org)** 80 | 81 | ## Shell 82 | 83 | - **[ShellCheck](https://github.com/koalaman/shellcheck)** (Tool) 84 | 85 | ## HTML 86 | 87 | - **[ARIA Authoring Practices Guide - Patterns](https://www.w3.org/WAI/ARIA/apg/patterns)** 88 | - **[How to Section Your HTML](https://css-tricks.com/how-to-section-your-html)** 89 | 90 | ## CSS 91 | 92 | - **[The Surprising Truth About Pixels and Accessibility](https://www.joshwcomeau.com/css/surprising-truth-about-pixels-and-accessibility/)** 93 | - **[BEM — Block Element Modifier](https://getbem.com)** 94 | - **[Sass Guidelines](https://sass-guidelin.es)** 95 | -------------------------------------------------------------------------------- /docs/patterns/finite-state-machines.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Understanding State Machines: A Video Game Menu Example" 3 | description: "Explore finite state machines (FSM) with this practical guide. Learn how to implement robust state transitions for a video game menu using JavaScript." 4 | --- 5 | 6 | # Understanding State Machines: A Video Game Menu Example 7 | 8 | State machines are powerful tools used to manage the state of an application or system. They are especially useful in scenarios where the system can be in one of many states, and events trigger transitions between these states. We'll explore the concept of state machines using a practical example: a video game menu. 9 | 10 | ## What is a State Machine? 11 | 12 | A state machine consists of: 13 | 14 | - **States**: Different conditions or modes in which the system can exist. 15 | - **Events**: Inputs or triggers that cause the system to transition from one state to another. 16 | - **Transitions**: The movement from one state to another, triggered by an event. 17 | - **Actions**: Tasks performed during the transition from one state to another. 18 | 19 | ## Example: Video Game Menu State Machine 20 | 21 | Let's consider a simple video game menu with the following states: 22 | 23 | - **MenuMain**: The main menu of the game. 24 | - **MenuOptions**: The options menu where settings can be adjusted. 25 | - **Playing**: The state where the game is actively being played. 26 | - **Paused**: The state where the game is paused. 27 | - **Exiting**: The state where the game is preparing to exit. 28 | - **Closing**: The final state where the game is closed. 29 | 30 | ### States, Events, and Actions 31 | 32 | Here is a table outlining the states, events, and actions for our video game menu: 33 | 34 | | Current State | Event | New State | Action | 35 | | ------------- | ------------- | ----------- | ----------------- | 36 | | MenuMain | SelectPlay | Playing | Start the game | 37 | | MenuMain | SelectOptions | MenuOptions | Show options | 38 | | MenuMain | SelectExit | Exiting | Exit the game | 39 | | MenuOptions | SelectBack | MenuMain | Back to main menu | 40 | | Playing | Pause | Paused | Pause the game | 41 | | Paused | Resume | Playing | Resume the game | 42 | | Paused | Exit | MenuMain | Back to main menu | 43 | | Exiting | ConfirmExit | Closing | Close the game | 44 | | Exiting | CancelExit | MenuMain | Back to main menu | 45 | 46 | ### JavaScript Implementation 47 | 48 | Now, let's see how we can implement this state machine in JavaScript. 49 | 50 | ```javascript 51 | class StateMachine { 52 | constructor() { 53 | this.state = "MenuMain"; 54 | this.states = { 55 | MenuMain: { 56 | SelectPlay: { newState: "Playing", action: this.startGame }, 57 | SelectOptions: { newState: "MenuOptions", action: this.showOptions }, 58 | SelectExit: { newState: "Exiting", action: this.exitGame }, 59 | }, 60 | MenuOptions: { 61 | SelectBack: { newState: "MenuMain", action: this.backToMainMenu }, 62 | }, 63 | Playing: { 64 | Pause: { newState: "Paused", action: this.pauseGame }, 65 | }, 66 | Paused: { 67 | Resume: { newState: "Playing", action: this.resumeGame }, 68 | Exit: { newState: "MenuMain", action: this.backToMainMenu }, 69 | }, 70 | Exiting: { 71 | ConfirmExit: { newState: "Closing", action: this.closeGame }, 72 | CancelExit: { newState: "MenuMain", action: this.backToMainMenu }, 73 | }, 74 | }; 75 | } 76 | 77 | transition(event) { 78 | const stateConfig = this.states[this.state]; 79 | const eventConfig = stateConfig[event]; 80 | 81 | if (eventConfig) { 82 | this.state = eventConfig.newState; 83 | eventConfig.action(); 84 | } else { 85 | console.error(`Event "${event}" is not valid from state "${this.state}"`); 86 | } 87 | } 88 | 89 | startGame() { 90 | console.log("Starting the game..."); 91 | } 92 | 93 | showOptions() { 94 | console.log("Showing options..."); 95 | } 96 | 97 | exitGame() { 98 | console.log("Exiting the game..."); 99 | } 100 | 101 | backToMainMenu() { 102 | console.log("Returning to main menu..."); 103 | } 104 | 105 | pauseGame() { 106 | console.log("Pausing the game..."); 107 | } 108 | 109 | resumeGame() { 110 | console.log("Resuming the game..."); 111 | } 112 | 113 | closeGame() { 114 | console.log("Closing the game..."); 115 | } 116 | } 117 | 118 | // Example usage 119 | const menu = new StateMachine(); 120 | menu.transition("SelectPlay"); // Starting the game... 121 | menu.transition("Pause"); // Pausing the game... 122 | menu.transition("SelectOptions"); // Event "SelectOptions" is not valid from state "Paused" 123 | ``` 124 | -------------------------------------------------------------------------------- /docs/dotnet/cli-commands.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: .NET CLI (dotnet) Commands 3 | description: A guide to useful .NET CLI (dotnet) commands, including where to run them and their purposes. 4 | --- 5 | 6 | # .NET CLI (dotnet) Commands 7 | 8 | ## Cheat Sheet 9 | 10 | | **Command** | **Run from** | **Description** | 11 | | ------------------------------------------------ | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | 12 | | `dotnet restore` | Solution folder | Restores the NuGet packages for all the projects in the solution. | 13 | | `dotnet build` | Solution folder | Builds all the projects in the solution. Implicitly calls `dotnet restore` before building. To build in `Release` mode, use the `-c` switch. | 14 | | `dotnet run` | Project folder | Runs the project in the current folder. Implicitly calls `dotnet restore` and `dotnet build` before running the app. Use during development to run your app. | 15 | | `dotnet watch run` | Project folder | Runs the project and watches for file changes. Automatically rebuilds and reruns the project when source files change. Ideal for rapid development. | 16 | | `dotnet publish -c Release -o ` | Project folder | Publishes the project to the provided folder. Copies all the required files to the provided output folder so it can be deployed. | 17 | | `dotnet test` | Solution folder | Restores packages, builds, and executes any unit tests found in the solution. Requires the .NET Test SDK and a testing framework adapter. | 18 | | `dotnet format` | Solution or project folder | Formats code to follow the .NET coding style conventions. Supports C#, VB, and F# codebases. | 19 | | `dotnet add package ` | Project folder | Install the NuGet package with the provided name in the current project. Optionally specify a package version—e.g., `-v 2.1.0`. | 20 | | `dotnet new --list` | Anywhere | View all the installed templates for creating ASP.NET Core apps, libraries, test projects, solution files, and many more. | 21 | | `dotnet new