Filter by tag:
JavaScript
P.F. EngineeringDeployed at (Closed Source Code)
Server-side rendered website. Collaborative project with Jacob Benison featuring searchable image galleries, pages written in Markdown (CommonMark flavor) with custom rendering addons. It also features a Cart module that accepts PayPal payments.
dos2unixPublished to
Command-line utility that converts text files with \r\n line endings into \n. Gracefully interrupts upon Ctrl+C. Recognition whether a file consists of a text or not is performed using http.DetectContentType based on the algorithm described at mimesniff.
Caddy configuration injection
gRPC server that accumulates configurations to update Caddy server.
Proof of concept with minimum API.
The idea is quite simple:
  • Caddy starts with a regular conf but without routes:
    json
    1{ 2 "apps": { 3 "http": { 4 "servers": { 5 "myserver": { 6 "automatic_https": { 7 "skip": [] 8 }, 9 "listen": [ 10 ":443" 11 ], 12 "routes": [ 13 ] 14 } 15 } 16 } 17 } 18}
  • This gRPC server is started.
  • Each application uses this server by sending requests to register their routes via gRPC calls.
    gomain.go
    1package main 2 3import ( 4 "context" 5 "flag" 6 "github.com/king8fisher/caddycfginjector/lib" 7 pb "github.com/king8fisher/caddycfginjector/proto/caddycfginjector" 8 "time" 9) 10 11var ( 12 addr = flag.String("addr", "localhost:50051", "the address to connect to") 13) 14 15func main() { 16 flag.Parse() 17 fn := lib.Fn(*addr, &pb.Route{ 18 Id: "example.com", 19 Handles: []*pb.Handle{ 20 { 21 Handler: &pb.Handle_ReverseProxy{ 22 ReverseProxy: &pb.ReverseProxy{ 23 Transport: &pb.Transport{ 24 Protocol: pb.Transport_HTTP, 25 }, 26 Upstreams: []*pb.Upstream{ 27 { 28 Dial: &pb.Dial{ 29 Host: "localhost", 30 Port: uint32(8080), 31 }, 32 }, 33 }, 34 }, 35 }, 36 }, 37 }, 38 Matches: []*pb.Match{ 39 { 40 Hosts: []string{"example.com", "beta.example.com"}, 41 Paths: []string{"/*"}, 42 }, 43 }, 44 }) 45 46 t, cancel := context.WithTimeout(context.Background(), time.Second*5) 47 defer cancel() 48 go lib.Periodically(t, time.Second*2, fn) 49 <-t.Done() 50}
  • This server then notifies Caddy of a change vis Caddy API.
Go run SASS
A simple command line tool wrapper to generate css using go-libsass, which otherwise recompiles the whole library when simply imported as github.com/wellington/go-libsass.
The command line can be called either manually or as part of application rebuilding routine. It can also be run from within the go web server responsible for compiling the resulted css.
go
1ifchanged.ExecuteCommand("go-run-sass", 2 "-i", "./web/global.scss", 3 "-o", "./web/generated.css")
The example uses ifchanged to minimize the call to .scss file changes.
JSON Walk
jsonwalk.Walk walks arbitrary JSON nodes, unmarshalled with the standard library json.Unmarshall call.
gowalk_docker_inspect.go
1jsonwalk.Walk(&v, jsonwalk.Callback(func(path jsonwalk.WalkPath, key interface{}, value interface{}, tp jsonwalk.NodeValueType) { 2 if path.Path() == "[0].Config.Env" && tp == jsonwalk.Array { 3 for _, v := range value.([]interface{}) { 4 env = append(env, v.(string)) 5 } 6 } else if (path.Path() == "[0].Config.Hostname") && tp == jsonwalk.String { 7 hostName = value.(string) 8 } else if (path.Path() == "[0].Name") && tp == jsonwalk.String { 9 name = value.(string) 10 } else if (path.Path() == "[0].State.Status") && tp == jsonwalk.String { 11 status = value.(string) 12 } else if (path.Path() == "[0].Config.WorkingDir") && tp == jsonwalk.String { 13 workingDir = value.(string) 14 } else if (path.Path() == "[0].Config.Image") && tp == jsonwalk.String { 15 image = value.(string) 16 } else if strings.HasPrefix(path.Path(), "[1]") { 17 warning = "--- warning: [1] found in the array" 18 } 19}))
Splicego1.18+ generics implementation of JavaScript's array.splice function for []T where T is constrained to 'any'.
go
1func ExampleSplice() { 2 var months = []string{"Jan", "March", "April", "June"} 3 Splice(&months, 1, 0, "Feb") // inserts at index 1 4 fmt.Println(months) 5 deleted := Splice(&months, 4, 1, "May") // replaces 1 element at index 4 6 fmt.Println(months) 7 fmt.Println(deleted) 8 // Output: 9 // [Jan Feb March April June] 10 // [Jan Feb March April May] 11 // [June] 12}
FreshFresh is a command-line tool that (re)builds and (re)starts a Go application every time the source code is modified.
1$ fresh -help 2Usage of fresh: 3 -c string 4 config file path (default "./.fresh.yaml") 5 -e string 6 environment variables prefix. "RUNNER_" is a default prefix 7 -g alias for -generate 8 -generate 9 generate a sample settings file either at "./.fresh.yaml" or at specified by -c location 10 -h print help page 11 -v alias for -version 12 -version 13 print current version and exit
TerminalCollection of utilities to output to colored terminal in a unified way.
go
1var t terminal.Terminal 2t.StartAlternativeBuffer() 3w, h := t.GetSize() 4t.SetCursorVisible(false) 5t.Printf("Start (%d,%d)", w, h) 6end := fmt.Sprintf(terminal.SetBright(true)+"End (%d,%d)"+terminal.SetBright(false), w, h) 7t.MoveToY(h - 1) 8// 01234 w = 5 9// | | 10// | xx| l = 2. 5-2= 3 11t.MoveToX(w - len(end)) 12t.SavePos() 13t.Printf(end) 14time.Sleep(time.Second) 15t.RestorePos() 16t.MoveByY(-1) 17t.Printf("Nice!") 18t.MoveByX(0) 19time.Sleep(time.Second) 20t.EraseRestOfScreen()
Voronoi Diagrams in Go
An implementation of Steven J. Fortune's algorithm to efficient Voronoi diagrams computing in Go language. It is based on a Raymond Hill's JavaScript implementation, forked from an excellent work by Przemyslaw Szczepaniak at github.com/pzsz/voronoi.
gomain.go
1import "github.com/zzwx/voronoi" 2 3func useVoronoi() { 4 // Sites of voronoi diagram 5 sites := []voronoi.Vertex{ 6 voronoi.Vertex{4, 5}, 7 voronoi.Vertex{6, 5}, 8 //... 9 } 10 11 // Create bounding box 12 bbox := NewBBox(0, 0, 20, 10) 13 14 // Compute diagram and close cells (add half edges from bounding box) 15 diagram := NewVoronoi().Compute(sites, bbox, true) 16 17 // Iterate over cells & their halfedges 18 for _, cell := diagram.Cells { 19 for _, hedge := cell.Halfedges { 20 //... 21 } 22 } 23 24 // Iterate over all edges 25 for _, edge := diagram.Edge { 26 //... 27 } 28}
Interval
Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates. It's exploring go 1.x code generation. It is a clone of a JavaScript project by James Talmage.
go
1func main() { 2 fmt.Println(interval.WrapInt(0, 360, 400)) //=> 40 3 fmt.Println(interval.WrapInt(0, 360, -90)) //=> 270 4 fmt.Println(interval.ClampInt(0, 100, 500)) //=> 100 5 fmt.Println(interval.ClampInt(0, 100, -20)) //=> 0 6 7 r := interval.NewRangeFloat64(0, 100, false, false) 8 fmt.Println(r.Wrap(120)) //=> 20 9 fmt.Println(r.Validate(120)) //=> 0, error(120 is outside of range [0,100]) 10 fmt.Println(r.Test(120)) //=> false 11 fmt.Println(r) //=> [0,100] (uses Stringer interface) 12}
Ifchanged
Package ifchanged is a collection of Go functions to perform callbacks in case of file changes (using sha256 hash) and / or missing files.
go
1err = ifchanged.NewIf(). 2 Changed(fileName, fileName+".sha256"). 3 Missing("somefile.txt"). 4 Execute(func() error { 5 fmt.Printf("This has been called because \"somefile.txt\" is missing or %v has changed\n", fileName) 6 return nil 7 })
Iffound
A simple go library to chain reading of a file if it is found.
Current
An attempt to make modules with resource files act as if they are embedded instead of actually embedding resources. Works in environments when it's known that the module will be built from source.
A module uses current.NewPath() to declare a variable or simply current.Path as an embedded type to make .Join(...) method work from the physical location on the drive of the .go source file even when it becomes a downloaded module. The .Path() method returns the exact position of the .go file on the drive.
The returned paths are always absolute.
Gamut Mask
Gamut mask tools implemented in Go. Can be used either as executable or as a library.
OpenStreetMap Map Tiles
OSM tiles tools and webapp. The purpose:
  • Arranging OpenStreetMap Map Tiles
    • The resulting tiles are suitable for printing
    • Producing a big PNG-file made of tiles
Dictionary ExplorerEnglish dictionary explorer. Parses and validates WordNet XML dictionary using Zod and prepares data for the work-in-progress web application.

Zod Test Coverage

1📂 LexicalResource [-] (basic test for a parent) 2 📂 Lexicon [-] (basic test for a parent) 3 📂 LexicalEntry [x] 4 📄 Form [x] 5 📂 Lemma [x] 6 📄 Pronunciation [x] 7 📂 Sense [x] 8 📄 SenseRelation [x] 9 📂 Synset [x] 10 📄 Definition [x] 11 📄 Example [x] 12 📄 ILIDefinition [x] 13 📄 SynsetRelation [x] 14 📄 SyntacticBehaviour [x]
av
Anton VeretennikovPersonal Landing Page