Skip to content

Commit

Permalink
Merge pull request #2 from gentee/beta
Browse files Browse the repository at this point in the history
version 1.1.0
  • Loading branch information
gentee authored Sep 28, 2020
2 parents 96f05e4 + 1ff267f commit e6f11b3
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 215 deletions.
50 changes: 37 additions & 13 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type CompileResponse struct {
Error string `json:"error,omitempty"`
}

type RunResponse struct {
Success bool `json:"success"`
ID uint32 `json:"id"`
Error string `json:"error,omitempty"`
}

type TaskStatus struct {
TaskID uint32 `json:"taskid"`
Status int `json:"status"`
Expand Down Expand Up @@ -98,7 +104,7 @@ func runHandle(c echo.Context) error {
)
open := true
name := c.QueryParam(`name`)
if len(c.QueryParam(`silent`)) > 0 {
if len(c.QueryParam(`silent`)) > 0 || cfg.HTTP.Host != Localhost {
open = false
}
if len(c.QueryParam(`console`)) > 0 {
Expand Down Expand Up @@ -127,23 +133,28 @@ func runHandle(c echo.Context) error {
}
}
header := script.Header{
Name: name,
Title: title,
AssetsDir: cfg.AssetsDir,
LogDir: cfg.Log.Dir,
Console: console,
UserID: c.(*Auth).User.ID,
Constants: storage.Settings.Constants,
Lang: langCode,
TaskID: lib.RndNum(),
ServerPort: cfg.HTTP.Port,
Name: name,
Title: title,
AssetsDir: cfg.AssetsDir,
LogDir: cfg.Log.Dir,
Console: console,
IsPlayground: cfg.playground,
UserID: c.(*Auth).User.ID,
Constants: storage.Settings.Constants,
Lang: langCode,
TaskID: lib.RndNum(),
ServerPort: cfg.HTTP.Port,
HTTP: &lib.HTTPConfig{
Host: cfg.HTTP.Host,
Port: port,
Open: open,
Theme: cfg.HTTP.Theme,
Access: cfg.HTTP.Access,
},
}
if header.IsPlayground {
header.Playground = &cfg.Playground
}
if src, err = GenSource(item, &header); err != nil {
return jsonError(c, err)
}
Expand All @@ -162,7 +173,7 @@ func runHandle(c echo.Context) error {
if console {
return c.Blob(http.StatusOK, ``, data.Bytes())
}
return c.JSON(http.StatusOK, Response{Success: true})
return c.JSON(http.StatusOK, RunResponse{Success: true, ID: header.TaskID})
}

func pingHandle(c echo.Context) error {
Expand All @@ -188,6 +199,19 @@ func taskStatusHandle(c echo.Context) error {
Message: taskStatus.Message,
Time: finish,
}
if taskStatus.Status == TaskActive {
task := tasks[taskStatus.TaskID]
cmd.Task = &Task{
ID: task.ID,
Status: task.Status,
Name: task.Name,
StartTime: task.StartTime,
FinishTime: task.FinishTime,
UserID: task.UserID,
Port: task.Port,
}
}

for id, client := range clients {
err := client.Conn.WriteJSON(cmd)
if err != nil {
Expand Down Expand Up @@ -220,7 +244,7 @@ func sysTaskHandle(c echo.Context) error {

for _, item := range tasks {
if item.ID == uint32(taskid) {
url := fmt.Sprintf("http://localhost:%d/sys?cmd=%s&taskid=%d", item.Port, cmd, taskid)
url := fmt.Sprintf("http://%s:%d/sys?cmd=%s&taskid=%d", Localhost, item.Port, cmd, taskid)
go func() {
resp, err := http.Get(url)
if err == nil {
Expand Down
336 changes: 179 additions & 157 deletions assets.go

Large diffs are not rendered by default.

37 changes: 27 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"gopkg.in/yaml.v2"
)

const (
ModeDefault = `default`
ModeDevelop = `develop`
ModePlayground = `playground`
)

// LogConfig stores config settings
type LogConfig struct {
Dir string `yaml:"dir"` // Directory for log files. If it is empty - dir of cfg file
Expand All @@ -29,29 +35,34 @@ type UsersConfig struct {

// Config stores application's settings
type Config struct {
Version string `yaml:"version"` // Version of the application
Develop bool `yaml:"develop"` // Developer's mode
AssetsDir string `yaml:"assetsdir"` // Directory for assets file. empty - dir of cfg file
Log LogConfig `yaml:"log"` // Log settings
Users UsersConfig `yaml:"users"` // Users settings
HTTP lib.HTTPConfig `yaml:"http"` // Web-server settings

path string // path to cfg file
Version string `yaml:"version"` // Version of the application
Mode string `yaml:"mode"` // Mode: default, develop, playground
AssetsDir string `yaml:"assetsdir"` // Directory for assets file. empty - dir of cfg file
Log LogConfig `yaml:"log"` // Log settings
Users UsersConfig `yaml:"users"` // Users settings
HTTP lib.HTTPConfig `yaml:"http"` // Web-server settings
Playground lib.PlaygroundConfig `yaml:"playground"` // Playground settings

path string // path to cfg file
develop bool
playground bool
}

const (
AccessLocalhost = `localhost`
AccessLocalhost = Localhost
AccessPrivate = `private`
)

var (
cfg = Config{
Version: Version,
Mode: ModeDefault,
Log: LogConfig{
Mode: logModeFile,
Level: logLevelInfo,
},
HTTP: lib.HTTPConfig{
Host: Localhost,
Port: DefPort,
Open: true,
Theme: `default`,
Expand Down Expand Up @@ -106,7 +117,11 @@ func LoadConfig() {
cfg.Log.Dir = defDir(cfg.Log.Dir, DefLog)
cfg.Users.Dir = defDir(cfg.Users.Dir, DefUsers)
// dataFile := defDir(cfg.DataDir)

if len(cfg.HTTP.Host) == 0 {
cfg.HTTP.Host = Localhost
} else if cfg.HTTP.Host != Localhost {
cfg.HTTP.Open = false
}
if cfg.HTTP.Port == 0 {
cfg.HTTP.Port = DefPort
}
Expand All @@ -118,6 +133,8 @@ func LoadConfig() {
default:
cfg.HTTP.Access = AccessLocalhost
}
cfg.develop = cfg.Mode == ModeDevelop
cfg.playground = cfg.Mode == ModePlayground
SetLogging(basename)
if err = InitTaskManager(); err != nil {
golog.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func request(url string) ([]byte, error) {
err error
)
buf := core.NewBuffer()
res, err = http.Get(`http://localhost:` + url)
res, err = http.Get(fmt.Sprintf(`http://%s:%s`, Localhost, url))
if err == nil {
buf.Data, err = ioutil.ReadAll(res.Body)
res.Body.Close()
Expand Down
3 changes: 2 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

const (
// Version of the application
Version = "1.0.0+1"
Version = "1.1.0+1"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand All @@ -32,6 +32,7 @@ const (
DefLang = 0
// ConsolePrefix is the prefix of eonza console version
ConsolePrefix = `ez`
Localhost = `localhost`
)

// AppInfo contains information about the application
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.12
require (
github.com/alecthomas/chroma v0.7.3
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gentee/gentee v1.13.0
github.com/gentee/gentee v1.14.0
github.com/gorilla/websocket v1.4.2
github.com/kataras/golog v0.1.2
github.com/labstack/echo/v4 v4.1.16
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/gentee/gentee v1.12.0 h1:QUlXUb9o9lLqpbg76DP0pALA545dd27UVe+2IWZtGRM=
github.com/gentee/gentee v1.12.0/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/gentee/gentee v1.13.0 h1:i51xgicLVPVGNsgiY3r06ZDOJc3wRrAGq8P5wE/rDuw=
github.com/gentee/gentee v1.13.0/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/gentee/gentee v1.14.0 h1:zbVoUamorzitXjdmLVbqHXBiOn7030Ki1qJ6ISaiyUs=
github.com/gentee/gentee v1.14.0/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gorilla/csrf v1.6.0/go.mod h1:7tSf8kmjNYr7IWDCYhd3U8Ck34iQ/Yw5CJu7bAkHEGI=
Expand Down
9 changes: 9 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ import (

// HTTPConfig stores web-server settings
type HTTPConfig struct {
Host string `yaml:"host"` // if empty, then localhost
Port int `yaml:"port"` // if empty, then DefPort
Open bool `yaml:"open"` // if true then host is opened
Theme string `yaml:"theme"` // theme of web interface. if it is empty - DefTheme
Access string `yaml:"access"` // Access level - localhost, private - DefAccess == localhost
JWTKey string `yaml:"jwtkey"` // Secret key for JWT token
}

// PlaygroundConfig stores the config of playgroundmode
type PlaygroundConfig struct {
Dir string `yaml:"dir"` // path to the temporary folder if it's empty then TempDir is used.
Summary int64 `yaml:"summary"` // all files size limit. By default, 10MB
Files int64 `yaml:"files"` // count of files limit. By default, 100
Size int64 `yaml:"size"` // file size limit. By default, 5MB
}

var (
privateIPBlocks []*net.IPNet
ipPrivateList = []string{
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {
setStatus(TaskFailed, err)
}
<-chFinish
if scriptTask.Header.HTTP.Open {
if scriptTask.Header.HTTP.Open || scriptTask.Header.HTTP.Host != Localhost {
if duration := time.Since(start).Milliseconds(); duration < TimeoutOpen {
time.Sleep(time.Duration(TimeoutOpen-duration) * time.Millisecond)
}
Expand Down
18 changes: 11 additions & 7 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
)

type Render struct {
App AppInfo
Version string
Develop bool
Langs map[string]string
Lang string
Login bool
App AppInfo
Version string
Develop bool
Playground bool
Langs map[string]string
Lang string
Login bool
Localhost bool
// Port int
/* Params map[string]string
Url string
Expand Down Expand Up @@ -115,13 +117,15 @@ func RenderPage(c echo.Context, url string) (string, error) {
} else {
render.App = appInfo
render.Version = Version
render.Develop = cfg.Develop
render.Develop = cfg.develop
render.Playground = cfg.playground
render.Lang = GetLangCode(c.(*Auth).User)
render.Langs = make(map[string]string)
for i, lang := range langs {
render.Langs[lang] = Lang(i, `native`)
}
render.Login = len(storage.Settings.PasswordHash) > 0
render.Localhost = cfg.HTTP.Host == Localhost
data = render
}

Expand Down
28 changes: 15 additions & 13 deletions script/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ import (
)

type Header struct {
Name string
Title string
AssetsDir string
LogDir string
Theme string
Console bool
SourceCode []byte
Constants map[string]string
Lang string
UserID uint32
TaskID uint32
ServerPort int
HTTP *lib.HTTPConfig
Name string
Title string
AssetsDir string
LogDir string
Theme string
Console bool
IsPlayground bool
SourceCode []byte
Constants map[string]string
Lang string
UserID uint32
TaskID uint32
ServerPort int
HTTP *lib.HTTPConfig
Playground *lib.PlaygroundConfig
}

func Encode(header Header, source string) (*bytes.Buffer, error) {
Expand Down
8 changes: 7 additions & 1 deletion script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (script *Script) Run(options Settings) (interface{}, error) {
options.ChStdout <- buf
}
}(script.Header.Console)

if script.Header.IsPlayground {
settings.IsPlayground = true
settings.Playground.Path = script.Header.Playground.Dir
settings.Playground.AllSizeLimit = script.Header.Playground.Summary
settings.Playground.FilesLimit = int(script.Header.Playground.Files)
settings.Playground.SizeLimit = script.Header.Playground.Size
}
return script.Exec.Run(settings)
}
10 changes: 3 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ const (

// WebSettings contains web-server parameters
type WebSettings struct {
Domain string // Domain, localhost if it sempty
Port int
Open bool // if true then webpage is opened
Port int
Open bool // if true then webpage is opened
}

type Response struct {
Expand Down Expand Up @@ -200,9 +199,6 @@ func reloadHandle(c echo.Context) error {
func RunServer(options WebSettings) *echo.Echo {
InitLang()
InitTemplates()
if len(options.Domain) == 0 {
options.Domain = `localhost`
}
e := echo.New()

e.HideBanner = true
Expand Down Expand Up @@ -270,7 +266,7 @@ func RunServer(options WebSettings) *echo.Echo {
var (
body []byte
)
url := fmt.Sprintf("http://%s:%d", options.Domain, options.Port)
url := fmt.Sprintf("http://%s:%d", Localhost, options.Port)
for string(body) != Success {
time.Sleep(100 * time.Millisecond)
resp, err := http.Get(url + `/ping`)
Expand Down
4 changes: 2 additions & 2 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type WsCmd struct {
Status int `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Time string `json:"finish,omitempty"`
Task *Task `json:"task,omitempty"`
}

type StdinForm struct {
Expand Down Expand Up @@ -388,7 +389,7 @@ func sendCmdStatus(status int, timeStamp int64, message string) {
Time: timeStamp,
})
if err == nil {
resp, err := http.Post(fmt.Sprintf("http://localhost:%d/api/taskstatus",
resp, err := http.Post(fmt.Sprintf("http://%s:%d/api/taskstatus", Localhost,
scriptTask.Header.ServerPort), "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
golog.Error(err)
Expand Down Expand Up @@ -434,7 +435,6 @@ func sysHandle(c echo.Context) error {

func setStatus(status int, pars ...interface{}) {
var message string
// cmd := WsCmd{TaskID: task.ID, Cmd: WcStatus, Status: status}
if len(pars) > 0 {
message = fmt.Sprint(pars...)
task.Message = message
Expand Down
Loading

0 comments on commit e6f11b3

Please sign in to comment.