Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGimbel committed Jan 23, 2018
0 parents commit a7b1654
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_files/*
20 changes: 20 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
builds:
- binary: srvc
ldflags: "-X cmd.version={{.Version}} -X cmd.buildDate={{.Date}} -X cmd.commit={{.Commit}}"
goos:
- windows
- darwin
- linux
goarch:
- amd64
archive:
replacements:
amd64: 64-bit
darwin: macOS

brew:
github:
owner: kevingimbel
name: homebrew-tap
homepage: https://github.com/kevingimbel/srvc
description: "Quick API prototyping tool"
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2018 Kevin Gimbel

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.

127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# `srvc`

`srvc` is a command line tool which allows you to quickly spin up a webserver with configurable routes. These routes can be configured to return HTML pages, XML, JSON, other files, custom headers, and inline-content directly from the config file.

## How to install

### Homebrew

On MacOS you can get the latest version from `brew`.

```sh
$ brew install kevingimbel/tap/srvc
```

### Binary

Alternatively you can grab the latest release from the [releases page](/releases) and place it somewhere in your `$PATH`.

## Usage

```sh
$ srvc
$ srvc [-port 1313]
```

`-port` is optional and takes a HTTP port to serve to. The default port is 8080.

## Config

`srvc` needs a YAML configuration file in the directory it is executed in. See the sample configuration file in the [example](/example/) directory.

The same configuration file is shown below.

```yaml
# global header config, added to each route
headers:
- key: "client"
value: "srvc-alpha1"

# route based config
routes:
# For the route "/demo/html-page" display the "index.html" file from the "html" fodler
/demo/html-page:
headers:
- key: "Content-Type"
value: "text/html"
file: "./html/index.html"

# display HTML content defined inline for /demo/html-inline
/demo/html-inline:
headers:
- key: "Content-Type"
value: "text/html"
content: |
<h1>It works!</h1>
<p>The content is defined inside the srvc.yaml config file
# Display XML on /demo/xml
/demo/xml:
headers:
- key: "Content-Type"
value: "application/xml"
content: |
<node id="12">
<meta charset="utf-8" />
<link to="#sub" lang="en">
<title>Go to sub</title>
<text>Click here</text>
</link>
</node>
# "json/from-file" returns the content of a JSON file
/json/from-file:
headers:
- key: "Content-Type"
value: "application/json"
file: "./json/from-file/dirty.json"
```

### Headers

Header can be defined on a global level or for each route. The global headers are added to each route, in the above example each route gets a `"client": "srvc-alpha1"` header.

```yaml
headers:
- key: "client"
value: "srvc-alpha1"
```

### Routes

Routes make up the second global config object. Here the different routes are defined. The "key" for each nested object is the route, for example "/hello/world" which makes `srvc` respond on `localhost:8080/hello/world`.

```yaml
routes:
/hello/world:
headers:
- key: "custom"
value: "header for route /hello/world"
- key: "Content-Type"
value: "text/html"
content: |
<h1>Hello, world!</h1>
<p>This is inline content!</p>
```

This route will respond with a HTML page containing the following code.

```html
<h1>Hello, world!</h1>
<p>This is inline content!</p>
```

A route can be configured to respond with the content of a file, as shown below.

```html
routes:
/hello/world/file:
headers:
- key: "custom"
value: "header for route /hello/world"
- key: "Content-Type"
value: "text/html"
file: "./relative/path/to/file.html"
```

The route `/hello/world/file` now responds with the contents of the file located at `./relative/path/to/file.html`.
77 changes: 77 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"

srvc "github.com/kevingimbel/srvc"
)

var port string
var versionFlag bool

var version string
var buildDate string
var commit string

var usage = `USAGE: %s [-port]
Arguments:
- port Port number, e.g. 1919, 1313, 1337
`

// osSignal captchers signals sent by the OS. This is used to close / exit the program
func osSignal(err chan<- error) {
osc := make(chan os.Signal)
signal.Notify(osc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
err <- fmt.Errorf("%s", <-osc)
}

func init() {
flag.StringVar(&port, "port", "8080", "Assign a port to serve to")
flag.BoolVar(&versionFlag, "version", false, "Show version")
}

func main() {
flag.Parse()

if versionFlag {
fmt.Printf("Version %s", version)
if buildDate != "" {
fmt.Printf("Build date: %s\n", buildDate)
}

if commit != "" {
fmt.Printf("Commit: %s", commit)
}
os.Exit(0)
}

// If we have args there's something wrong.
// The only argument is "-port XXXX" which is (removed?) from
// flag.Args() or doesn't count towards it.
if len(flag.Args()) > 0 {
// os.Args[0] is the executable name
fmt.Printf(usage, os.Args[0])
os.Exit(1)
}

p := ":" + port
srv := srvc.New(p)
srv.CreateConfiguredHandlers()

errch := make(chan error)

go osSignal(errch)

go func() {
fmt.Println("Server is running on port", port)
errch <- srv.Run()
}()

exit := <-errch
fmt.Println("Stopping Service. Reason:", exit)
}
50 changes: 50 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package srvc

import (
"fmt"
"io/ioutil"

"gopkg.in/yaml.v2"
)

var version string

// Config represents the yaml config
type Config struct {
Headers []Header `yaml:"headers"`
Routes map[string]RouteConfig `yaml:"routes"`
}

// Header defines
type Header struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}

// RouteConfig represents the config for a single route
type RouteConfig struct {
Headers []Header `yaml:"headers"`
Content string `yaml:"content"`
File string `yaml:"file"`
}

var config Config

func init() {
f, err := ioutil.ReadFile("./srvc.yaml")

if err != nil {
fmt.Println("Cannot open file srvc.yaml")
}

err = yaml.Unmarshal(f, &config)

if err != nil {
fmt.Println(err)
}
}

// GetConfig returns the config
func GetConfig() Config {
return config
}
13 changes: 13 additions & 0 deletions example/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>srvc demo</title>
</head>
<body>
<h1>It works!</h1>
<p>The <code>srvc</code> cli tool works if this website can be accessed at localhost:&lt;port|8080>/html/demo, as configured in the srvc.yaml file.</p>
</body>
</html>
9 changes: 9 additions & 0 deletions example/json/from-file/dirty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Dirty JSON",
"id": "dirty-json",
"type": "test_file",
"content": {
"id": 12,
"created_at": "22-01-2018"
}
}
44 changes: 44 additions & 0 deletions example/srvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# global header config, added to each route
headers:
-
key: "client"
value: "srvc-alpha1"

# route based config
routes:
# For the route "/demo/html-page" display the "index.html" file from the "html" fodler
/demo/html-page:
headers:
- key: "Content-Type"
value: "text/html"
file: "./html/index.html"

# display HTML content defined inline for /demo/html-inline
/demo/html-inline:
headers:
- key: "Content-Type"
value: "text/html"
content: |
<h1>It works!</h1>
<p>The content is defined inside the srvc.yaml config file
# Display XML on /demo/xml
/demo/xml:
headers:
- key: "Content-Type"
value: "application/xml"
content: |
<node id="12">
<meta charset="utf-8" />
<link to="#sub" lang="en">
<title>Go to sub</title>
<text>Click here</text>
</link>
</node>
# "json/from-file" returns the content of a JSON file
/json/from-file:
headers:
- key: "Content-Type"
value: "application/json"
file: "./json/from-file/dirty.json"
22 changes: 22 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build mage

package main

import (
"github.com/magefile/mage/sh"
)

// Create a full release with goreleaser. Builds binaries and pushes them to GitHub
func Release() error {
return sh.Run("goreleaser", "--rm-dist")
}

// Create a pre-release with goreleaser. Builds the binaries but does not push them.
func PreRelease() error {
return sh.Run("goreleaser", "--rm-dist", "--snapshot")
}

// Build a binary named "microcorn-dev" and place it in /usr/local/bin
func BuildDev() error {
return sh.Run("go", "build", "-ldflags=-X github.com/kevingimbel/srvc/cmd/cmd.version=local-dev", "-i", "-o", "/usr/local/bin/srvc-dev", "./cmd/")
}
Loading

0 comments on commit a7b1654

Please sign in to comment.