Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
solaoi committed Jul 20, 2022
0 parents commit ba84051
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and Publish Docker

on:
release:
types:
- published

jobs:
build_and_push:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
IMAGE_NAME: broly
steps:
- name: checkout
uses: actions/checkout@v2

- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
115 changes: 115 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: nimreleaser

on:
release:
types:
- published

env:
APP_NAME: 'bloly'
NIM_VERSION: 'stable'
MAINTAINER: 'solaoi'
RELEASE_FILES: APPNAME LICENSE README.*

jobs:
build-artifact:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macOS-latest
steps:
- uses: actions/checkout@v1
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: ${{ env.NIM_VERSION }}
- run: nimble build -Y -d:release --threads:on
- name: Create artifact
run: |
assets="${{ env.APP_NAME }}_$(echo "${{ runner.os }}" | tr '[:upper:]' '[:lower:]')"
echo "$assets"
mkdir -p "dist/$assets"
cp -r ${{ env.RELEASE_FILES }} "dist/$assets/"
(
cd dist
if [[ "${{ runner.os }}" == Windows ]]; then
7z a "$assets.zip" "$assets"
else
tar czf "$assets.tar.gz" "$assets"
fi
ls -lah *.*
)
shell: bash
- uses: actions/upload-artifact@v2
with:
name: artifact-${{ matrix.os }}
path: |
dist/*.tar.gz
dist/*.zip
create-release:
runs-on: ubuntu-latest
needs:
- build-artifact
steps:
- uses: actions/checkout@v1
- name: Create Release
id: create-release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: Release
draft: false
prerelease: false

- name: Write upload_url to file
run: echo '${{ steps.create-release.outputs.upload_url }}' > upload_url.txt

- uses: actions/upload-artifact@v2
with:
name: create-release
path: upload_url.txt

upload-release:
runs-on: ubuntu-latest
needs: create-release
strategy:
matrix:
include:
- os: ubuntu-latest
asset_name_suffix: linux.tar.gz
asset_content_type: application/gzip
- os: windows-latest
asset_name_suffix: windows.zip
asset_content_type: application/zip
- os: macOS-latest
asset_name_suffix: macos.tar.gz
asset_content_type: application/gzip
steps:
- uses: actions/download-artifact@v2
with:
name: artifact-${{ matrix.os }}

- uses: actions/download-artifact@v2
with:
name: create-release

- id: vars
run: |
echo "::set-output name=upload_url::$(cat upload_url.txt)"
- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.vars.outputs.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 }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
broly
*.json
!target.json
.DS_Store
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM nimlang/nim:1.6.6-ubuntu AS builder
RUN apt-get update && apt-get install -y musl-tools

WORKDIR /app

# install deps
COPY broly.nimble ./
RUN nimble install -y --depsOnly

# build
COPY ./src ./src
RUN nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static c ./src/broly.nim

# dummy json for mount
COPY target.json ./

FROM busybox
COPY --from=builder /app/src/broly /app/target.json ./

ENTRYPOINT [ "./broly" ]
CMD [ "target.json" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 solaoi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Broly

## Usage

### 1. Binary

### 2. Docker

#### RunOnly

```sh
# Specify the port you want to provide
HOST_PORT=80
# Specify the filename you want to serve
STUB_JSON=filename.json

# Run Container
docker run --init \
-p $HOST_PORT:8080 \
--mount type=bind,source="$(pwd)/$STUB_JSON",target=/target.json \
ghcr.io/solaoi/broly:latest
```

#### Local Build & Run

```sh
# Specify the port you want to provide
HOST_PORT=80

# Edit stubs for serving
vi target.json

# Build DockerImage
docker build -t broly .

# Run Container
docker run --init \
-p $HOST_PORT:8080 \
-t broly
```
14 changes: 14 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Security Policy

## Supported Versions

There is only one version track for broly so all patches will be applied to the latest version only.

| Version | Supported |
| ------- | ------------------ |
| Latest | :white_check_mark: |

## Reporting a Vulnerability

Critical vulnerabilities can be responsibly disclosed to [mail@aota.blog](mailto:mail@aota.blog).
Bugs and low level vulneratbilities can be reported via repository issues.
14 changes: 14 additions & 0 deletions broly.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Package

version = "0.1.0"
author = "solaoi"
description = "HighPerformance Stub Server"
license = "MIT"
srcDir = "src"
bin = @["broly"]


# Dependencies

requires "nim >= 1.6.6"
requires "httpbeast >= 0.4.1"
47 changes: 47 additions & 0 deletions src/broly.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import httpbeast,os,strutils,parseopt,json,asyncdispatch,options
import stub

var stubs: seq[Stub]

proc getArgs():tuple[port:int, path:string] =
result = (port:8080, path:"co-metub.json")
var opt = parseopt.initOptParser( os.commandLineParams().join(" ") )
for kind, key, val in opt.getopt():
case key
of "port", "p":
case kind
of parseopt.cmdLongOption, parseopt.cmdShortOption:
opt.next()
result.port = opt.key.parseInt()
else: discard
else:
result.path = key

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:
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)
except Exception:
let
headers = "Content-type: application/json; charset=utf-8"
response = %*{"message": "エラーが発生しました"}
req.send(Http400, $response, headers)
finally:
isSend=true
break
if not isSend:
req.send(Http404)

when isMainModule:
let (port, path) = getArgs()
stubs = getStubsOn(path)
let settings = initSettings(Port(port))
run(onRequest, settings)
56 changes: 56 additions & 0 deletions src/stub.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import httpbeast,strutils,json,options

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

proc createStub(json:JsonNode) :Stub=
result = new Stub
result.stubPath = json["path"].str
result.stubMethod = case json["method"].str
of "HEAD", "head":
some(HttpHead)
of "GET", "get":
some(HttpGet)
of "POST", "post":
some(HttpPost)
of "PUT", "put":
some(HttpPut)
of "DELETE", "delete":
some(HttpDelete)
of "TRACE", "trace":
some(HttpTrace)
of "OPTIONS", "options":
some(HttpOptions)
of "CONNECT", "connect":
some(HttpConnect)
of "PATCH", "patch":
some(HttpPatch)
else:
none(HttpMethod)
result.stubContentType = json["contentType"].str
result.stubStatus =
case json["statusCode"].str.parseInt()
of 0 .. 599:
some(HttpCode(json["statusCode"].str.parseInt()))
else:
none(HttpCode)
result.stubResponse = json["response"].str
result.stubSleep = json["sleep"].getInt

proc getStubsOn*(path:string):seq[Stub] =
let
jsonStr = readFile(path)
stubSettings = parseJson jsonStr

if stubSettings.kind == JArray:
for v in stubSettings:
let stub = createStub(v)
result.add(stub)
else:
let stub = createStub(stubSettings)
result.add(stub)
Loading

0 comments on commit ba84051

Please sign in to comment.