№ 12000 В разделе
Programming
от May 27th, 2023,
В подшивках: Go
Мой собственный генератор QR кодов, создавался для моих сайтов и сервисов. Отлично работает в облаках и прочих средах. Написан на Golang.
Репозиторий с кодом и полными инструкциями https://git.blindage.org/21h/qr-generator-service
Сервис умеет в ограничение списка доменов и принимает GET и POST запросы по урлу /qr:
$ curl -vs -X POST -d '{"URL":"http://blindage.org/"}' http://localhost:8080/qr -o qr.png * Trying 127.0.0.1:8080... * Connected to localhost (127.0.0.1) port 8080 (#0) > POST /qr HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.81.0 > Accept: */* > Content-Length: 30 > Content-Type: application/x-www-form-urlencoded > } [30 bytes data] * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Content-Type: image/png < Date: Fri, 26 May 2023 21:25:17 GMT < Content-Length: 430 < { [430 bytes data] * Connection #0 to host localhost left intact
№ 11467 В разделе
Programming
от December 12th, 2021,
В подшивках: Go
Useful to set version or build number. Also you can use embed.
Code:
package main import "fmt" var MyVariable = "jopa" func main() { fmt.Printf("%s\n", MyVariable) }
Build script:
#!/usr/bin/bash ENVVAR="jopa i piska" LDFLAGS=( "-X 'main.MyVariable=${ENVVAR}'" ) go build -o testvar -ldflags="${LDFLAGS[*]}" main.go ./testvar rm -f ./testvar
Output:
$ ./build.sh jopa i piska
№ 11253 В разделах: Programming
Sysadmin
от March 21st, 2021,
В подшивках: Python, Security, Selectel
Получает токен по логину и паролю и отдает в виде строки без переносов. Крайне удобно использовать в bash скриптах. Пример использования в моем наборе скриптов для получения сертификатов LetsEncrypt для хранилища Селектел https://git.blindage.org/21h/certbot-for-selectel
import argparse import requests parser = argparse.ArgumentParser("get_selectel_token") parser.add_argument("account", help="Your master account or subaccount, i.e. 83522 or 43544_somename", type=str) parser.add_argument("password", help="Password for this account", type=str) args = parser.parse_args() headers = {'X-Auth-User': str(args.account), 'X-Auth-Key': str(args.password)} response = requests.get("https://api.selcdn.ru/auth/v1.0", headers=headers) if response.status_code == 204: print(response.headers['X-Auth-Token'], end="") else: print("Error:", response.status_code)
№ 11244 В разделах: Electronics
ESP8266
Programming
от March 8th, 2021,
В подшивках: Arduino, ESP8266
Human eye can’t see led switching because its too fast, same effect used in old TV and displays with CRT, most of 7-segment indicators and other things. Change timings between on and off state to change brightness and effect duration.
int sv_max=20; int sv_min=0; void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { int pause_on=sv_min; int pause_off=sv_max; // Smooth turn on while(pause_on < sv_max) { pause_on++; pause_off--; digitalWrite(LED_BUILTIN, HIGH); delay(pause_on); digitalWrite(LED_BUILTIN, LOW); delay(pause_off); } pause_on=sv_max; pause_off=sv_min; // Smooth turn off while(pause_on > sv_min) { pause_on--; pause_off++; digitalWrite(LED_BUILTIN, HIGH); delay(pause_on); digitalWrite(LED_BUILTIN, LOW); delay(pause_off); } }
№ 11183 В разделах: Programming
Sysadmin
от January 2nd, 2021,
В подшивках: Docker, Go, Kubernetes, Security, Vault
What if you stored your database credentials in Vault and want to make ENV variables with them for your application at container startup? You can do it for Kubernetes deployments or plain Docker containers with my small program vault-envs.
Add to your Dockerfile additional steps:
Add to your Dockerfile steps:
... ... # add Ubuntu\Debian repo and install vault-envs with fresh certificates RUN curl http://deb.blindage.org/gpg-key.asc | apt-key add - && \ echo "deb http://deb.blindage.org bionic main" | tee /etc/apt/sources.list.d/21h.list && \ apt update RUN apt install -y ca-certificates vault-envs # copy entrypoint script COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"]
Your entrypoint script will look like:
#!/bin/bash ... ... export eval `vault-envs -token "$VAULT_TOKEN" \ -vault-url https://vault.blindage.org \ -vault-path /prod/crm/connection_postgres -envs-prefix "PG_"` export eval `vault-envs -token "$VAULT_TOKEN" \ -vault-url https://vault.blindage.org \ -vault-path /prod/crm/connection_mysql -envs-prefix "MYSQL_"` export eval `vault-envs -token "$VAULT_TOKEN" \ -vault-url https://vault.blindage.org \ -vault-path /prod/crm/connection_api` ... ... exec "$@"
If some vars names is identical they will be overwritten at next vault-envs call, so I used prefix.
Now build image and run
docker run --rm -e VAULT_TOKEN=s.QQmLlqnHnRAEO9eUeoggeK1n crm printenv
and see results at container console:
... VAULT_RETRIEVER=vault-envs PG_DB_PASS=postgres PG_DB_PORT=5432 PG_DB_USER=postgres PG_DB_HOST=db-postgres PG_DB_NAME=crm MYSQL_DB_HOST=mysql.wordpress MYSQL_DB_PASS= MYSQL_DB_PORT=3306 MYSQL_DB_USER=root MYSQL_DB_NAME=wordpress API_HOST=http://crm/api API_TOKEN=giWroufpepfexHyentOnWebBydHojGhokEpAnyibnipNirryesaccasayls4 ...
Wooh! You did it.
Fortune cookie: I'd like to meet the man who invented sex and see what he's working on now.