2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
2015-02-13 04:59:51 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2015-02-13 04:59:51 +03:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package playground
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-01 15:21:00 +03:00
|
|
|
"github.com/88250/gulu"
|
|
|
|
"github.com/88250/wide/conf"
|
|
|
|
"github.com/88250/wide/session"
|
2015-02-13 04:59:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuildHandler handles request of Playground building.
|
|
|
|
func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-24 16:04:25 +03:00
|
|
|
result := gulu.Ret.NewResult()
|
|
|
|
defer gulu.Ret.RetResult(w, r, result)
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
|
2015-02-13 04:59:51 +03:00
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
fileName := args["fileName"].(string)
|
2019-05-16 20:41:04 +03:00
|
|
|
filePath := filepath.Clean(conf.Wide.Data + "/playground/" + fileName)
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
suffix := ""
|
2019-05-24 16:04:25 +03:00
|
|
|
if gulu.OS.IsWindows() {
|
2015-02-13 04:59:51 +03:00
|
|
|
suffix = ".exe"
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
data := map[string]interface{}{}
|
2015-11-27 11:55:28 +03:00
|
|
|
result.Data = &data
|
2015-11-24 12:39:35 +03:00
|
|
|
|
2019-05-16 20:41:04 +03:00
|
|
|
executable := filepath.Clean(conf.Wide.Data + "/playground/" + strings.Replace(fileName, ".go", suffix, -1))
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
cmd := exec.Command("go", "build", "-o", executable, filePath)
|
|
|
|
out, err := cmd.CombinedOutput()
|
2015-11-24 12:39:35 +03:00
|
|
|
|
2015-02-13 04:59:51 +03:00
|
|
|
data["output"] = template.HTML(string(out))
|
|
|
|
|
|
|
|
if nil != err {
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data["executable"] = executable
|
|
|
|
}
|