Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Implement convert
Browse files Browse the repository at this point in the history
  • Loading branch information
a-know committed Nov 15, 2018
1 parent bac1a56 commit 03b100c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
97 changes: 97 additions & 0 deletions handlers/convert.go
@@ -0,0 +1,97 @@
package handlers

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)

// ConvertParam :
type ConvertParam struct {
Username string `json:"username"`
GraphID string `json:"graphID"`
Date string `json:"date"`
Mode string `json:"mode"`
Stage string `json:"stage"`
Hash string `json:"hash"`
}

// HandleSVGConvert :
func HandleSVGConvert(w http.ResponseWriter, r *http.Request) {
// unmarshal
var param ConvertParam
err = json.Unmarshal(b, &param)
if err != nil {
log.Printf("Failed to unmarshal params: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}

// treat params
var paramsStr string
if param.Date != "" {
paramsStr = fmt.Sprintf("?date=%s", param.Date)
}
if param.Mode != "" {
paramsStr = fmt.Sprintf("%s&mode=%s", paramsStr, param.Mode)
}

// get svg
var url string
if param.Stage == "dev" {
url = fmt.Sprintf("https://www-dev-215102.appspot.com/v1/users/%s/graphs/%s%s", param.Username, param.GraphID, paramsStr)
} else {
url = fmt.Sprintf("https://pixela/v1/users/%s/graphs/%s%s", param.Username, param.GraphID, paramsStr)
}

resp, err := http.Get(url)
if err != nil {
log.Printf("could not get svg response : %s, %v", url, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()

if resp.StatusCode > 299 {
log.Printf("something went wrong to get svg response : %d %s", resp.StatusCode, url)
w.WriteHeader(resp.StatusCode)
return
}

byteArray, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not get svg response : %s, %v", url, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
extractData := string(byteArray)

// flush to file
tmpDirname := fmt.Sprintf("/tmp/pixela-svg/%s", t.date.Format("2006-01-02"))
tmpSvgFilePath = fmt.Sprintf("%s/%s.svg", tmpDirname, param.Hash)
err := flushFile(tmpDirname, t.tmpSvgFilePath, extractData)
if err != nil {
return err
}

// convert
var size string
if param.Mode == "short" {
size = fmt.Sprintf("%sx%s", "220", "135")
} else {
size = fmt.Sprintf("%sx%s", "720", "135")
}
tmpPngDirname := fmt.Sprintf("/tmp/pixela-png/%s", t.date.Format("2006-01-02"))
tmpPngFilePath = fmt.Sprintf("%s/%s.png", tmpPngDirname, param.Hash)
err := exec.Command("convert", "-geometry", t.size, tmpSvgFilePath, tmpPngFilePath).Run()
if err != nil {
log.Printf("failed to run convert command : %s, %v", t.tmpSvgFilePath, err)
w.WriteHeader(http.StatusInternalServerError)
return
}

// response
http.ServeFile(w, r, tmpPngFilePath)
}
4 changes: 4 additions & 0 deletions main.go
Expand Up @@ -47,6 +47,10 @@ func main() {

r.Post("/knock", handlers.HandleKnock)

// for Pixela SVG convert to PNG
// /pixela/convert?username=a-know&graphID=test-graph&date=yyyyMMdd&mode=short&stage=dev&hash=xxxxx
r.Get("/pixela/convert", handlers.HandleSVGConvert)

log.Printf("grass-graph started.")
http.ListenAndServe(":8080", r)
}
2 changes: 1 addition & 1 deletion provision/files/nginx/default.conf.erb
Expand Up @@ -7,7 +7,7 @@ server {

add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";

location ~ /pixela/purge {
location ~ /pixela/convert {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down

0 comments on commit 03b100c

Please sign in to comment.