Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
solaoi committed Jul 21, 2022
1 parent 8e543ee commit 8bf7af6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ${{ env.APP_NAME }}_${{ matrix.asset_name_suffix }}
asset_name: ${{ env.APP_NAME }}_${{ matrix.asset_name_suffix }}
asset_content_type: ${{ matrix.asset_content_type }}
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@

This is a high performance stub server.

## Benchmark

## Usage

```sh
broly target.json -p 9999
```

JSON format is below.
see a sample [here](https://raw.githubusercontent.com/solaoi/broly/main/target.json).

| Field | Type | Required | Default | Sample |
| ----------- | ----------------------- | -------- | ---------------- | ----------------------- |
| - (parent) | JSONArray or JSONObject | True | - | - |
| path | string | True | - | "/hello" |
| method | string | True | - | "GET" |
| contentType | string | False | application/json | "application/json" |
| statusCode | string | True | - | "200" |
| response | string | True | - | "{\"name\": \"hello\"}" |
| sleep | number | False | 0 | 1000 (milliseconds) |

Option is below.

| Option | Description |
| --------- | ---------------------------------- |
| -p,--port | specify the port you want to serve |

### 1. Binary

### 2. Docker
Expand Down Expand Up @@ -42,3 +63,5 @@ docker run --init \
-p $HOST_PORT:8080 \
-t broly
```

## Benchmark
2 changes: 1 addition & 1 deletion broly.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.3"
version = "0.1.4"
author = "solaoi"
description = "HighPerformance Stub Server"
license = "MIT"
Expand Down
20 changes: 11 additions & 9 deletions src/broly.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import stub
var stubs: seq[Stub]

proc getArgs():tuple[port:int, path:string] =
result = (port:8080, path:"co-metub.json")
result = (port:8080, path:"target.json")
var opt = parseopt.initOptParser( os.commandLineParams().join(" ") )
for kind, key, val in opt.getopt():
case key
Expand All @@ -21,18 +21,20 @@ proc onRequest(req: Request): Future[void]{.async.} =
var isSend:bool
{.cast(gcsafe).}:
for v in stubs:
if req.httpMethod == v.stubMethod and req.path.get() == v.stubPath:
if req.httpMethod == v.stubMethod and req.path == v.stubPath:
try:
if v.stubSleep > 0:
await sleepAsync(v.stubSleep)
let
headers = "Content-Type: " & v.stubContentType
stauts = v.stubStatus.get
req.send(stauts, v.stubResponse, headers)
if v.stubSleepMs > 0:
await sleepAsync(v.stubSleepMS)
let status = v.stubStatus
if v.stubContentType.isSome:
let headers = v.stubContentType.get
req.send(status, v.stubResponse, headers)
else:
req.send(status, v.stubResponse)
except Exception:
let
headers = "Content-type: application/json; charset=utf-8"
response = %*{"message": "エラーが発生しました"}
response = %*{"message": "Error occurred."}
req.send(Http400, $response, headers)
finally:
isSend=true
Expand Down
48 changes: 36 additions & 12 deletions src/stub.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import httpbeast,strutils,json,options
import httpbeast,strutils,json,options,strformat

type Stub* = ref object
stubPath*:string
stubPath*: Option[string]
stubMethod*: Option[HttpMethod]
stubContentType*: string
stubStatus*: Option[HttpCode]
stubContentType*: Option[string]
stubStatus*: HttpCode
stubResponse*: string
stubSleep*: int
stubSleepMs*: int

type
StubLoadError* = object of IOError

proc createStub(json:JsonNode) :Stub=
result = new Stub
result.stubPath = json["path"].str
try:
result.stubPath = some(json["path"].str)
except:
raise newException(StubLoadError, fmt"path is not set on this json")

result.stubMethod = case json["method"].str
of "HEAD", "head":
some(HttpHead)
Expand All @@ -31,16 +38,33 @@ proc createStub(json:JsonNode) :Stub=
of "PATCH", "patch":
some(HttpPatch)
else:
none(HttpMethod)
result.stubContentType = json["contentType"].str
raise newException(StubLoadError, fmt"method is not set on {$result.stubPath}")

try:
var contentType = json["contentType"].str
if contentType == "":
result.stubContentType = none(string)
else:
result.stubContentType = some(fmt"Content-Type: {contentType}")
except:
result.stubContentType = some("Content-Type: application/json")

result.stubStatus =
case json["statusCode"].str.parseInt()
of 0 .. 599:
some(HttpCode(json["statusCode"].str.parseInt()))
HttpCode(json["statusCode"].str.parseInt())
else:
none(HttpCode)
result.stubResponse = json["response"].str
result.stubSleep = json["sleep"].getInt
raise newException(StubLoadError, fmt"statusCode is not set on {$result.stubPath}")

try:
result.stubResponse = json["response"].str
except:
raise newException(StubLoadError, fmt"response is not set on {$result.stubPath}")

try:
result.stubSleepMs = json["sleep"].getInt
except:
result.stubSleepMs = 0

proc getStubsOn*(path:string):seq[Stub] =
let
Expand Down

0 comments on commit 8bf7af6

Please sign in to comment.