Skip to content

Instantly share code, notes, and snippets.

@corebug
Last active May 29, 2018 12:42
Show Gist options
  • Save corebug/9ab8250b67359672ad212260d5475231 to your computer and use it in GitHub Desktop.
Save corebug/9ab8250b67359672ad212260d5475231 to your computer and use it in GitHub Desktop.
Domains generator with JSON output in Go
package main
import (
"fmt"
"encoding/json"
"sync"
"errors"
"time"
)
type LocableDomainsDatabase struct {
lock sync.RWMutex
DB Domains `json:"domains"`
}
type Domains map[string]Domain
type Domain struct {
Dom string `json:"dom"`
Path string `json:"path"`
Cgi string `json:"cgi"`
Alias string `json:"alias"`
}
func (l *LocableDomainsDatabase) Init() {
l.lock.Lock()
defer l.lock.Unlock()
if l.DB == nil {
l.DB = Domains{}
}
}
func (l *LocableDomainsDatabase) Add(d Domain) {
l.lock.Lock()
defer time.Sleep(time.Duration(3 * time.Second))
defer l.lock.Unlock()
l.DB[d.Dom] = d
}
func (l *LocableDomainsDatabase) Get(d string) (Domain, error) {
l.lock.RLock()
defer l.lock.RUnlock()
domain, ok := l.DB[d];
if !ok {
return Domain{"", "", "", ""}, errors.New(fmt.Sprintf("Domain %s not found", d))
}
return domain, nil
}
func (l *LocableDomainsDatabase) Domains() Domains {
l.lock.RLock()
defer l.lock.RUnlock()
return l.DB
}
func main() {
var pieceOfShit LocableDomainsDatabase
routinesCount := 100
wg := new(sync.WaitGroup)
pieceOfShit.Init()
for i := 0; i < routinesCount; i++ {
go func(i int){
wg.Add(1)
pieceOfShit.Add(Domain{
fmt.Sprintf("x%d.com", i),
fmt.Sprintf("/var/www/x%d.com", i),
fmt.Sprintf("x%d-someFuckingCGI", i),
fmt.Sprintf("alias-x%d.com", i),
})
wg.Done()
}(i)
}
wg.Wait()
resultJSON, _ := json.Marshal(pieceOfShit)
fmt.Println(string(resultJSON))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment