Skip to content

Instantly share code, notes, and snippets.

View ulexxander's full-sized avatar
💪
Workin hard

Alexander Ustyugov ulexxander

💪
Workin hard
  • Limitlex d.o.o.
  • Slovenia
View GitHub Profile
@ulexxander
ulexxander / createConfirmation.ts
Last active January 7, 2022 15:08
Effector factory - Confirmation of some action
import { createEvent, restore, sample } from "effector";
export function createConfirmation<Require, Allow>() {
type Confirmed = { require: Require; allow: Allow };
const require = createEvent<Require>();
const $payload = restore(require, null);
const allow = createEvent<Allow>();
const confirmed = createEvent<Confirmed>();
@ulexxander
ulexxander / reactCaptchaPlaceholder.ts
Last active September 28, 2021 10:23
Component that embeds ReCAPTCHA from react-google-recaptcha and saves your time in develpment because it can be replaced with placeholder
import React, { forwardRef, useEffect } from "react";
import ReCAPTCHA, { ReCAPTCHAProps } from "react-google-recaptcha";
import { setting } from "../config";
type CaptchaProps = Omit<ReCAPTCHAProps, "sitekey">;
const CaptchaPlaceholder: React.FC<{ onClick?: () => void }> = ({
onClick,
}) => {
return (
@ulexxander
ulexxander / effector-services.tsx
Last active October 1, 2021 18:31
Testing approach to write effector business logic only using factories.
import {
combine,
createEffect,
createEvent,
createStore,
Effect,
Event,
forward,
sample,
Store,
const intSize = unsafe.Sizeof(int(0))
func intToUUID(n int) uuid.UUID {
var u uuid.UUID
intPtr := unsafe.Pointer(&n)
for i := 0; i < int(intSize); i++ {
byteptr := (*byte)(unsafe.Add(intPtr, i))
u[i] = *byteptr
}
return u
func splitEscaped(s string, sep, esc rune) []string {
var pos int
a := strings.FieldsFunc(s, func(r rune) bool {
escaped := pos > 0 && rune(s[pos-1]) == esc
pos++
return r == sep && !escaped
})
for i, s := range a {
a[i] = strings.ReplaceAll(s, string(esc)+string(sep), string(sep))
}
/*
basename prop has '/' by deafult
it works as a prefix for all of routes, pushes, links and so on
*/
export const App: React.FC = () => {
return (
<Router basename="/admin">
{/* ... */}
</Router>
);
@ulexxander
ulexxander / prometheus-docker-sd.yml
Created November 15, 2021 09:00
Prometheus scrape config for automatic docker service discovery
# NOTE: containers must be on the same network as Prometheus
# And port 80 with /metrics endpoint must be exposed
scrape_configs:
- job_name: "docker_sd"
docker_sd_configs:
# Do not forget to mount docker socket into container
- host: "unix:///var/run/docker.sock"
relabel_configs:
- source_labels:
- "__meta_docker_container_id"
@ulexxander
ulexxander / token_hex.go
Created November 27, 2021 09:46
Generate random hex token
package random
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
func TokenHex(length int) (string, error) {
// Base 16 is 2x longer than Base 256
@ulexxander
ulexxander / json_time_duration.go
Created January 19, 2022 08:43
Make Go time.Duration JSON marshalable and unmarshalable as strings.
// Duration embeds time.Duration and makes it more JSON-friendly.
// Instead of marshaling and unmarshaling as int64 it uses strings, like "5m" or "0.5s".
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(data []byte) error {
var str string
@ulexxander
ulexxander / twofactorunmarshal_test.go
Created February 21, 2022 14:38
Example of using Go json.RawMessage to unmarshal JSON field in struct based on another field determining its expected type.
package twofactorunmarshal
import (
"encoding/json"
"fmt"
"testing"
"github.com/pkg/errors"
)