Skip to content

Commit

Permalink
Basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Diez committed Jan 18, 2018
0 parents commit b3d2819
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"os"
"io/ioutil"
"net/http"
"net/url"
"encoding/json"
"sync"
"io"
)

const baseUrl = "http://www.websequencediagrams.com"

type Response struct {
ImageUrl string `json:"img"`
Errors []string `json:"errors"`
}

func main() {
var wg sync.WaitGroup

for _, filename := range(os.Args[1:]) {
sourceBytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err);
}
wg.Add(1)
fmt.Printf("%s --> %s.png\n", filename, filename)
go sequence(string(sourceBytes), fmt.Sprintf("%s.png", filename), &wg)
}

wg.Wait()
}

func sequence(source string, destination string, wg *sync.WaitGroup) (err error){
var jsonResponse = Response{}

defer wg.Done()

res, err := http.PostForm(fmt.Sprintf("%s/index.php", baseUrl), url.Values{"style": {"default"}, "message": {source}, "apiVersion": {"1"}, "format": {"png"}})
if err != nil {
return err;
}

err = json.NewDecoder(res.Body).Decode(&jsonResponse)
if err != nil {
return err;
}

res, err = http.Get(fmt.Sprintf("%s/%s", baseUrl, jsonResponse.ImageUrl))
if err != nil {
return err;
}

defer res.Body.Close()
file, err := os.Create(destination)
if err != nil {
return err
}

_, err = io.Copy(file, res.Body)
if err != nil {
return err
}
file.Close()

return nil
}

0 comments on commit b3d2819

Please sign in to comment.