Skip to content

Commit 6feaff8

Browse files
committed
docs: update readme
1 parent a759f2b commit 6feaff8

File tree

3 files changed

+31
-94
lines changed

3 files changed

+31
-94
lines changed

README.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Elaina ![Go](https://github.com/wuhan005/Elaina/workflows/Go/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/wuhan005/Elaina)](https://goreportcard.com/report/github.com/wuhan005/Elaina) ![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/wuhan005/elaina) ![Docker Image Version (latest by date)](https://img.shields.io/docker/v/wuhan005/elaina)
22

33
<img align="right" src="elaina.gif" width="40%"/>
4-
Docker-based remote code runner.
4+
Container-based remote code runner.
55

66
[简体中文](https://github.com/wuhan005/Elaina/blob/master/README_zh.md)
77

@@ -10,20 +10,19 @@ Docker-based remote code runner.
1010
### Step 1: Install dependencies
1111

1212
* [Docker](https://docs.docker.com/get-docker/) (v20.10.0 or higher)
13+
* [Postgres](https://www.postgresql.org/download/) (v13.1 or higher)
1314

1415
### Step 2: Pull internal docker images
1516

16-
The [`docker/images/`](https://github.com/wuhan005/Elaina/tree/master/docker/images) folder provides the Dockerfile of
17-
the programming language runtime environment that Elaina supports.
18-
1917
Use `docker pull` command to pull the images from DockerHub before you start running the Elaina. This operation only
2018
needs to be performed once.
2119

2220
```bash
23-
docker pull elainaruntime/golang
24-
docker pull elainaruntime/php
25-
docker pull elainaruntime/python
26-
docker pull elainaruntime/javascript
21+
docker pull glot/php:latest
22+
docker pull glot/python:latest
23+
docker pull glot/golang:latest
24+
docker pull glot/javascript:latest
25+
docker pull glot/c:latest
2726
```
2827

2928
### Step 3: Build and start the Elaina server
@@ -42,30 +41,22 @@ go build .
4241

4342
#### Set environment variables.
4443

45-
Postgres database configuration.
46-
4744
```bash
48-
export PGPORT=5432
49-
export PGHOST=<REDACTED>
50-
export PGUSER=<REDACTED>
51-
export PGPASSWORD=<REDACTED>
52-
export PGDATABASE=<REDACTED>
53-
export PGSSLMODE=disable
45+
export APP_PASSWORD=<REDACTED>
46+
export RUNTIME_MODE=docker
47+
export POSTGRES_DSN=postgres://postgres:<REDACTED>@127.0.0.1:5432/elaina
5448
```
5549

5650
#### Run the Elaina server.
5751

5852
```bash
59-
# Set the web manager panel password.
60-
export APP_PASSWORD=<REDACTED>
61-
6253
./Elaina
6354
```
6455

6556
### Step 4: Have fun!
6657

67-
Visit `http://<your-host>:8080/m/` to login to the manager panel.
58+
Visit `http://<your-host>:8080/` to login to the manager panel.
6859

6960
## License
7061

71-
MIT
62+
MIT License

README_zh.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# Elaina ![Go](https://github.com/wuhan005/Elaina/workflows/Go/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/wuhan005/Elaina)](https://goreportcard.com/report/github.com/wuhan005/Elaina) ![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/wuhan005/elaina) ![Docker Image Version (latest by date)](https://img.shields.io/docker/v/wuhan005/elaina)
22

33
<img align="right" src="elaina.gif" width="40%"/>
4-
基于 Docker 的远程代码运行器
4+
基于容器的远程代码运行器
55

66
## 开始使用
77

88
### 步骤 1: 安装依赖
99

1010
* [Docker](https://docs.docker.com/get-docker/) (v20.10.0 或更高)
11-
* [Docker Compose](https://docs.docker.com/compose/install/) (v1.27.4 或更高)
11+
* [Postgres](https://www.postgresql.org/download/) (v13.1 或更高)
1212

1313
### 步骤 2: 拉取内置 Docker 镜像
1414

15-
[`docker/images/`](https://github.com/wuhan005/Elaina/tree/master/docker/images) 文件夹内提供的 Elaina 所支持的编程语言运行环境。
16-
1715
在运行 Elaina 前,请使用 `docker pull` 命令从 DockerHub 拉取这些镜像。该操作只需执行一次即可。
1816

1917
```bash
20-
docker pull elainaruntime/golang
21-
docker pull elainaruntime/php
22-
docker pull elainaruntime/python
23-
docker pull elainaruntime/javascript
18+
docker pull glot/php:latest
19+
docker pull glot/python:latest
20+
docker pull glot/golang:latest
21+
docker pull glot/javascript:latest
22+
docker pull glot/c:latest
2423
```
2524

2625
### Step 3: 编译并启动 Elaina
@@ -39,30 +38,22 @@ go build .
3938

4039
#### 设置环境变量
4140

42-
Postgres 数据库配置。
43-
4441
```bash
45-
export PGPORT=5432
46-
export PGHOST=<REDACTED>
47-
export PGUSER=<REDACTED>
48-
export PGPASSWORD=<REDACTED>
49-
export PGDATABASE=<REDACTED>
50-
export PGSSLMODE=disable
42+
export APP_PASSWORD=<REDACTED>
43+
export RUNTIME_MODE=docker
44+
export POSTGRES_DSN=postgres://postgres:<REDACTED>@127.0.0.1:5432/elaina
5145
```
5246

5347
#### 运行 Elaina
5448

5549
```bash
56-
# 设置 Web 管理面板的密码
57-
export APP_PASSWORD=<REDACTED>
58-
5950
./Elaina
6051
```
6152

6253
### 步骤 4: 走你!
6354

64-
浏览器访问 `http://<your-host>:8080/m/` 来登录管理员面板。
55+
浏览器访问 `http://<your-host>:8080/` 来登录管理员面板。
6556

6657
## 开源协议
6758

68-
MIT
59+
MIT License

internal/form/form.go

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,36 @@ import (
1010
"reflect"
1111

1212
"github.com/flamego/flamego"
13-
"github.com/sirupsen/logrus"
1413
"github.com/wuhan005/govalid"
15-
"golang.org/x/text/language"
16-
)
17-
18-
type ErrorCategory string
1914

20-
const (
21-
ErrorCategoryDeserialization ErrorCategory = "deserialization"
22-
ErrorCategoryValidation ErrorCategory = "validation"
15+
"github.com/wuhan005/Elaina/internal/context"
2316
)
2417

25-
type Error struct {
26-
Category ErrorCategory
27-
Error error
28-
}
29-
3018
func Bind(model interface{}) flamego.Handler {
3119
// Ensure not pointer.
3220
if reflect.TypeOf(model).Kind() == reflect.Ptr {
3321
panic("form: pointer can not be accepted as binding model")
3422
}
3523

36-
return flamego.ContextInvoker(func(c flamego.Context) {
24+
return func(ctx context.Context) {
3725
obj := reflect.New(reflect.TypeOf(model))
38-
r := c.Request().Request
26+
r := ctx.Request().Request
3927
if r.Body != nil {
4028
defer func() { _ = r.Body.Close() }()
4129

4230
if err := json.NewDecoder(r.Body).Decode(obj.Interface()); err != nil {
43-
c.Map(Error{Category: ErrorCategoryDeserialization, Error: err})
44-
if _, err := c.Invoke(errorHandler); err != nil {
45-
panic("form: " + err.Error())
46-
}
31+
_ = ctx.Error(http.StatusBadRequest, "Failed to parse request body")
4732
return
4833
}
4934
}
5035

51-
acceptLanguage := r.Header.Get("Accept-Language")
52-
languageTags, _, _ := language.ParseAcceptLanguage(acceptLanguage)
53-
languageTag := language.Chinese
54-
if len(languageTags) > 0 {
55-
languageTag = languageTags[0]
56-
}
57-
58-
errors, ok := govalid.Check(obj.Interface(), languageTag)
36+
errors, ok := govalid.Check(obj.Interface())
5937
if !ok {
60-
c.Map(Error{Category: ErrorCategoryValidation, Error: errors[0]})
61-
if _, err := c.Invoke(errorHandler); err != nil {
62-
panic("form: " + err.Error())
63-
}
38+
_ = ctx.Error(http.StatusBadRequest, errors[0].Error())
6439
return
6540
}
6641

6742
// Validation passed.
68-
c.Map(obj.Elem().Interface())
69-
})
70-
}
71-
72-
func errorHandler(c flamego.Context, error Error) {
73-
c.ResponseWriter().WriteHeader(http.StatusBadRequest)
74-
c.ResponseWriter().Header().Set("Content-Type", "application/json; charset=utf-8")
75-
76-
var msg string
77-
if error.Category == ErrorCategoryDeserialization {
78-
msg = "invalid request body"
79-
} else {
80-
msg = error.Error.Error()
81-
}
82-
83-
body := map[string]interface{}{
84-
"msg": msg,
85-
}
86-
err := json.NewEncoder(c.ResponseWriter()).Encode(body)
87-
if err != nil {
88-
logrus.WithError(err).Error("Failed to encode response")
43+
ctx.Map(obj.Elem().Interface())
8944
}
9045
}

0 commit comments

Comments
 (0)