protoc-gen-go-micro/cmd/web-editor/main.go

122 lines
2.8 KiB
Go
Raw Normal View History

2017-10-26 17:48:58 +03:00
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
2017-12-25 11:17:09 +03:00
"log"
2017-10-26 17:48:58 +03:00
"net/http"
"os"
"os/exec"
"path/filepath"
2020-05-20 14:22:18 +03:00
packr "github.com/gobuffalo/packr/v2"
2017-10-26 17:48:58 +03:00
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func generate(w http.ResponseWriter, r *http.Request) {
// read input
decoder := json.NewDecoder(r.Body)
type Input struct {
Protobuf string `json:"protobuf"`
Template string `json:"template"`
}
var input Input
if err := decoder.Decode(&input); err != nil {
returnError(w, err)
return
}
// create workspace
dir, err := ioutil.TempDir("", "pggt")
if err != nil {
returnError(w, err)
}
2017-12-25 11:17:09 +03:00
// clean up
defer func() {
if err = os.RemoveAll(dir); err != nil {
log.Printf("error: failed to remove temporary directory: %v", err)
}
}()
if err = ioutil.WriteFile(filepath.Join(dir, "example.proto"), []byte(input.Protobuf), 0644); err != nil {
2017-10-26 17:48:58 +03:00
returnError(w, err)
return
}
2017-12-25 11:17:09 +03:00
if err = ioutil.WriteFile(filepath.Join(dir, "example.output.tmpl"), []byte(input.Template), 0644); err != nil {
2017-10-26 17:48:58 +03:00
returnError(w, err)
return
}
// generate
2017-12-25 11:17:09 +03:00
cmd := exec.Command("protoc", "-I"+dir, "--gotemplate_out=template_dir="+dir+",debug=true:"+dir, filepath.Join(dir, "example.proto")) // #nosec
2017-10-26 17:48:58 +03:00
out, err := cmd.CombinedOutput()
if err != nil {
returnError(w, errors.New(string(out)))
return
}
// read output
2018-09-13 02:54:44 +03:00
content, err := ioutil.ReadFile(filepath.Join(dir, "example.output")) // #nosec
2017-10-26 17:48:58 +03:00
if err != nil {
returnError(w, err)
return
}
returnContent(w, content)
}
func returnContent(w http.ResponseWriter, output interface{}) {
payload := map[string]interface{}{
"output": fmt.Sprintf("%s", output),
}
2017-12-25 11:17:09 +03:00
response, err := json.Marshal(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2017-10-26 17:48:58 +03:00
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
2017-12-25 11:17:09 +03:00
if _, err := w.Write(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2017-10-26 17:48:58 +03:00
}
func returnError(w http.ResponseWriter, err error) {
payload := map[string]interface{}{
"error": fmt.Sprintf("%v", err),
}
2017-12-25 11:17:09 +03:00
response, err := json.Marshal(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2017-10-26 17:48:58 +03:00
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
2017-12-25 11:17:09 +03:00
if _, err := w.Write(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2017-10-26 17:48:58 +03:00
}
func main() {
r := mux.NewRouter()
2020-05-20 14:22:18 +03:00
box := packr.New("static", "./static")
r.Handle("/", http.FileServer(box))
2017-10-26 17:48:58 +03:00
r.HandleFunc("/generate", generate)
addr := fmt.Sprintf(":%s", os.Getenv("PORT"))
if addr == ":" {
addr = ":8080"
}
fmt.Printf("Listening on %s...\n", addr)
h := handlers.LoggingHandler(os.Stderr, r)
h = handlers.CompressHandler(h)
h = handlers.RecoveryHandler()(h)
2017-12-25 11:17:09 +03:00
if err := http.ListenAndServe(addr, h); err != nil {
panic(err)
}
2017-10-26 17:48:58 +03:00
}