Skip to content

Instantly share code, notes, and snippets.

View AndreiTelteu's full-sized avatar

Andrei Telteu AndreiTelteu

View GitHub Profile
@AndreiTelteu
AndreiTelteu / MySQL dynamic max_connections setting .md
Created May 8, 2023 19:44
MySQL Dynamic max_connections based on active threads

Put the following script in a .sh file, for example in /root/watch-mysql.sh

#!/usr/bin/bash
trap "exit" 0

MAXCONN=1000 # set this to whatever base max_connections you have defined in your my.conf setting
while true; do
  #mysql -e "SHOW VARIABLES LIKE 'max_connections';"
 THREADS=`mysqladmin status | awk '{print $4}'`
@AndreiTelteu
AndreiTelteu / React useBetterState with mutable, callback, and promise .md
Last active April 7, 2023 09:15
React useBetterState with mutable state, callback after change, and promise. Just like the old this.setState() Class Component days. DEMO: https://stackblitz.com/edit/react-usebetterstate

useBetterState

I'm trying to bring back the magic of setState(value, callback) from React Class Component days, while adding more functionality on top, like promises and mutable state.

To start using it, initialize it with:

const [state, setState] = useBetterState({
  count: 0,
@AndreiTelteu
AndreiTelteu / Solidjs full year calendar .tsx
Created February 9, 2023 16:55
Solidjs full year calendar build with momentjs
import { Component, For } from "solid-js";
import moment from "moment";
import "moment/dist/locale/ro";
moment.locale("ro");
const App: Component = () => {
const weekdays = moment.weekdaysMin();
const months = moment.months();
const fullYear = Object.fromEntries(
months.map((monthName, monthIndex) => {
@AndreiTelteu
AndreiTelteu / Laravel stream large file with buffer .php
Created February 7, 2023 21:50
Laravel stream large file from disk with buffer.
<?php
// inspired by https://github.com/imanghafoori1/laravel-video
// if you need this for video, this does not support seek.
// laravel-video supports seek and chunks, so use that for video.
Route::get('/big-file-stream', function () {
$filePath = \Storage::disk('local')->path('video.mp4');
$fileStream = \Storage::disk('local')->readStream('video.mp4');
@AndreiTelteu
AndreiTelteu / Backpack export operation (excel and csv) .md
Last active October 4, 2022 17:58
Export operation for backpack admin panel, that exports the entire database table, not just the entries shown on page
  1. First install maatwebsite/excel package:
composer require maatwebsite/excel
  1. Copy export.blade.php from this gist in resources/views/vendor/backpack/crud/buttons/export.blade.php

  2. Copy ExportOperation.php from this gist in app/Traits/Operations/ExportOperation.php

You can follow microsoft's official instalation steps, however if you have multiple php instalations (I have php7.4 and 7.3 installed on my laravel forge server) you need to specify what version to build the extension with.

# make sure you have this installed:
sudo apt-get install -y unixodbc-dev

# elevate as root
sudo su
pecl -d php_suffix=7.3 install sqlsrv
pecl -d php_suffix=7.3 install pdo_sqlsrv
@AndreiTelteu
AndreiTelteu / aapanel install mysql
Last active July 20, 2022 16:07
aapanel instal mysql
cd /www/server/panel/install
bash ./install_soft.sh 1 install mysql mariadb_10.4
bash ./install_soft.sh 1 install openlitespeed 1.7
@AndreiTelteu
AndreiTelteu / How to install vs code server and setup supervisor with fnm .md
Last active February 6, 2022 15:44
Install vs code-server with fnm and yarn, and keep it runing always with supervisor (on ubuntu)

Install build requirements (python3 and build essential)

sudo apt-get install -y build-essential pkg-config python3

Install fnm to manage multiple nodejs versions (uninstall nodejs or nvm if you have it installed)

curl -fsSL https://fnm.vercel.app/install | bash
@AndreiTelteu
AndreiTelteu / Manual export inline with maatwebsite excel laravel package .md
Last active February 6, 2022 12:22
How to export/import in excel format with maatwebsite/excel using inline anonymous class, without any external files #laravel

Using this plugin: maatwebsite/excel

composer require maatwebsite/excel

You can use this anywhere in your app.

<?php
@AndreiTelteu
AndreiTelteu / Mark a word .md
Last active January 25, 2022 22:50
Mark a word with <mark>

You can easily mark a specified word (or array of words) in a string.

<?php
function mark($pattern, $source) {
    if (is_array($pattern)) {
        $finalPatterns = array_map(function ($value) { return "/(".preg_quote($value).")(?![^<]*>|[^<>]*<\/)/i"; }, $pattern);
    } else {
        $finalPatterns = "/(".preg_quote($pattern).")(?![^<]*>|[^<>]*<\/)/i";
 }