Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gjerokrsteski committed Dec 20, 2018
0 parents commit 5e17ebf
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
################################################################################
# Definition for handling files
# for more detailes see
# * https://help.github.com/articles/dealing-with-line-endings/#platform-linux
# * https://git-scm.com/docs/gitattributes
################################################################################
.gitattributes text eol=lf
.gitignore text eol=lf

# Unix shell scripts
*.sh text eol=lf

# Unix shell scripts
*.yaml text eol=lf

# Golang
*.go text eol=lf
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
/dist
/tmp
/out-tsc
.idea
.DS_Store
Thumbs.db
*.iml
bin/*
pidfile
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dist: trusty

sudo: required

language: go

os:
- linux

go:
- 1.9.1
- 1.10.1
- 1.11.1
- tip

matrix:
allow_failures:
- go: tip

script:
- ./lint.sh
- ./build.sh
- ./test.sh

43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Ralph - RESTful HTTP File Server

Ralph is a lightweight RESTful HTTP file-server, that serves HTTP requests with the contents of the file.

## Use Cases
- Developers who need a quick back-end for prototyping and mocking.
- If you have dependencies to remote services like an API,
than Gin file-server will help you out to start local file-server in order to be
able to simulate the undone API.
- Simple if you want to list a files from specific directory.

## Download

Ralph is compiled for amd64 architecture and supports following operating systems: window, darwin (mac) and linux.
[Ddownload latest release](https://github.com/gjerokrsteski/ralph-file-server/tags)

### 1. Explorer the binary

```
./ralph-file-server -h
```

```
Usage of ./ralph-file-server:
-dir string
directory of static file to host (default ".")
-port string
port to serve on (default "8100")
-version
prints current version
```

### 2. Start the file-server (default port is 8100)

```
./ralph-file-server -port=5454 -dir=/path/to/files/
```


# Development
For developing and contributing you need [Golang 1.9.1](https://golang.org/) installed.

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
1 change: 1 addition & 0 deletions _fixtures/test.fixture-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions _fixtures/test.fixture-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
20 changes: 20 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

. ./ci-scripts/build-functions.sh

VERSION=$(cat VERSION)

print_info "build linux binary ..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -v -ldflags '-extldflags "-static" -w -s -X main.Version='${VERSION} -o ./bin/linux/ralph-file-server file-server.go
assert_exit_code "linux binary not compiled!"
assert_file_exists "./bin/linux/ralph-file-server"

print_info "build darwin binary ..."
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -v -ldflags '-extldflags "-static" -w -s -X main.Version='${VERSION} -o ./bin/darwin/ralph-file-server file-server.go
assert_exit_code "darwin binary not compiled!"
assert_file_exists "./bin/darwin/ralph-file-server"

print_info "build windows binary ..."
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -v -ldflags '-extldflags "-static" -w -s -X main.Version='${VERSION} -o ./bin/windows/ralph-file-server.exe file-server.go
assert_exit_code "windows binary not compiled!"
assert_file_exists "./bin/windows/ralph-file-server.exe"
39 changes: 39 additions & 0 deletions ci-scripts/build-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

date_format="+%Y-%m-%d_%H-%M-%S%z"

print_info() {
echo -e "$(date ${date_format}) ### INFO - $1"
}

print_warn() {
echo -e "$(date ${date_format}) ### WARN - $1"
}

print_error() {
echo -e "$(date ${date_format}) ### ERROR - $1"
}

assert_error() {
print_error "$1"
exit 1
}

assert_not_empty_string() {
if [[ -z $1 ]]; then
assert_error $2
fi
}

assert_file_exists() {
if [[ ! -e $1 ]]; then
assert_error "Could not find file: \"${1}\""
fi
}

assert_exit_code() {
local exit_code=$?
if [[ ${exit_code} -ne 0 ]]; then
assert_error "$1 (exit code: ${exit_code})"
fi
}
50 changes: 50 additions & 0 deletions ci-scripts/bump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

usage_info="It accepts following mutually exclusive arguments:\n
major - version bump equal to +1.0.0\n
minor - version bump equal to +0.1.0\n
patch - version bump equal to +0.0.1\n"

if [[ -z "$1" ]]; then
echo "BUMP is a semantic versioning bump script."
echo -e "Please refer to http://semver.org/ for a definition of semantic versioning.\n"
echo -e "${usage_info}"
echo -e "Please provide which part to bump as the first argument \n"
echo "Like: ./bump.sh minor"
exit 2
fi

# declare what can be bumped
declare -A version_types
version_types=( ["major"]=0 ["minor"]=1 ["patch"]=2)

# validate the given argument if is not expected
if [[ -z "${version_types[$1]+x}" ]]; then
echo -e "Argument '$1' is invalid! \n"
echo -e "${usage_info}"
exit 2
fi

# create VERSION file if it does not exist
touch VERSION

# explode version from VERSION file into array
old_version=$(cat VERSION)
IFS='.' read -r -a parts <<< "${old_version}"

# increment relevant version part
bumped=$((parts[version_types[$1]]+1))
parts[version_types[$1]]=${bumped}

# reset relevant version parts
if [[ "$1" == "major" ]]; then
parts[1]="0"
parts[2]="0"
elif [[ "$1" == "minor" ]]; then
parts[2]="0"
fi

# write new version to VERSION file
new_version=$((parts[0])).$((parts[1])).$((parts[2]))
echo ${new_version} > VERSION
echo "bumped $old_version to $new_version"
21 changes: 21 additions & 0 deletions ci-scripts/check-http-output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

if [[ -z "$1" || -z "$2" ]]; then
echo -e "The script checks the application's HTTP output.\n"
echo "Usage: ./check-http-output.sh <URI> <EXPECTED STRING>"
exit 1
fi

URI=$1
EXPECTED_STRING=$2

sleep 1

echo "CHECK if expected string \"$EXPECTED_STRING\" is at HTTP output"

if curl $URI | grep -q "$EXPECTED_STRING"; then
echo -e "\nOK - expected string \"$EXPECTED_STRING\" was found at HTTP output."
else
echo -e "\nERROR - expected string \"$EXPECTED_STRING\" was not found at HTTP output."
exit 1
fi
31 changes: 31 additions & 0 deletions ci-scripts/check-http-response.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

if [[ -z "$1" ]]; then
echo -e "The script checks if given URI (Uniform Resource Identifier) is responding HTTP status code 200\n
URI = http(s)://example.com:8042/over/there?name=ferret#nose
\_____/\_________________/\_________/\_________/\__/
| | | | |
scheme authority path query fragment
| ________________________|__
/ \ / \\
urn:example:animal:ferret:nose\n"
echo "Usage: ./check-http-response.sh <URI> <EXPECTED HTTP CODE>"
exit 1
fi

if [[ -z "$2" ]]; then
echo -e "No expected http code given!\n"
echo "Usage: ./check-http-response.sh <URI> <EXPECTED HTTP CODE>"
exit 1
fi

echo "CHECK if $1 is up"
expectedHttpCode=$2
testHttpCode=$(curl -sL -w '%{http_code}' $1 -o /dev/null 2>&1 | grep ${expectedHttpCode})

if [ "$testHttpCode" != "${expectedHttpCode}" ]; then
echo "ERROR - no HTTP status code ${expectedHttpCode} received!"
exit 1
fi

echo "HTTP status code ${expectedHttpCode} received"
29 changes: 29 additions & 0 deletions file-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"flag"
"log"
"net/http"
"fmt"
"os"
)

var (
Version string = "No Version Provided"
)

func main() {
port := flag.String("port", "8100", "port to serve on")
directory := flag.String("dir", ".", "directory of static file to host")
version := flag.Bool("version", false, "prints current version")
flag.Parse()

if *version {
fmt.Println("Gin HTTP file-server v"+Version)
os.Exit(0)
}

http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(*directory))))
log.Printf("starting Gin HTTP file-server v%s for directory %s on port: %s\n", Version, *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
3 changes: 3 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh
set -e
go vet -all -v file-server.go
3 changes: 3 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
((./bin/linux/ralph-file-server -dir=$(pwd)/_fixtures) & echo $! >./pidfile)
11 changes: 11 additions & 0 deletions stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
set -e

if [ ! -f ./pidfile ]
then
echo "File pidfile does not exist!"
exit 1
fi

kill -15 $(cat ./pidfile)

14 changes: 14 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env sh

./start.sh

./ci-scripts/check-http-response.sh http://localhost:8100 200
./ci-scripts/check-http-response.sh http://localhost:8100/bar-segment 404

./ci-scripts/check-http-response.sh http://localhost:8100/test.fixture-1.json 200
./ci-scripts/check-http-response.sh http://localhost:8100/test.fixture-2.json 200

./ci-scripts/check-http-output.sh http://localhost:8100 "test.fixture-1.json"
./ci-scripts/check-http-output.sh http://localhost:8100 "test.fixture-2.json"

./stop.sh

0 comments on commit 5e17ebf

Please sign in to comment.