Skip to content

Commit e4d1aeb

Browse files
committed
Initial commit
0 parents  commit e4d1aeb

File tree

11 files changed

+417
-0
lines changed

11 files changed

+417
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Created by https://www.gitignore.io/api/go
3+
# Edit at https://www.gitignore.io/?templates=go
4+
5+
### Go ###
6+
# Binaries for programs and plugins
7+
*.exe
8+
*.exe~
9+
*.dll
10+
*.so
11+
*.dylib
12+
13+
# Test binary, built with `go test -c`
14+
*.test
15+
16+
# Output of the go coverage tool, specifically when used with LiteIDE
17+
*.out
18+
19+
bin/
20+
out/
21+
22+
### Go Patch ###
23+
/vendor/
24+
/Godeps/
25+
26+
## Editor
27+
.idea/
28+
29+
# End of https://www.gitignore.io/api/go

Gopkg.lock

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/urfave/cli"
30+
version = "1.20.0"
31+
32+
[prune]
33+
go-tests = true
34+
unused-packages = true

Makefile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.PHONY: help
2+
help: ## Prints help (only for targets with comments)
3+
@grep -E '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4+
5+
APP=dobby
6+
SRC_PACKAGES=$(shell go list ./... | grep -v "vendor")
7+
VERSION?=1.0
8+
BUILD?=$(shell git describe --always --dirty 2> /dev/null)
9+
DEP:=$(shell command -v dep 2> /dev/null)
10+
GOLINT:=$(shell command -v golint 2> /dev/null)
11+
APP_EXECUTABLE="./out/$(APP)"
12+
RICHGO=$(shell command -v richgo 2> /dev/null)
13+
GOMETA_LINT=$(shell command -v golangci-lint 2> /dev/null)
14+
GOLANGCI_LINT_VERSION=v1.12.5
15+
GO111MODULE=off
16+
SHELL=/bin/bash -o pipefail
17+
18+
ifeq ($(GOMETA_LINT),)
19+
GOMETA_LINT=$(shell command -v $(PWD)/bin/golangci-lint 2> /dev/null)
20+
endif
21+
22+
ifeq ($(RICHGO),)
23+
GO_BINARY=go
24+
else
25+
GO_BINARY=richgo
26+
endif
27+
28+
ifeq ($(BUILD),)
29+
BUILD=dev
30+
endif
31+
32+
ifdef CI_COMMIT_SHORT_SHA
33+
BUILD=$(CI_COMMIT_SHORT_SHA)
34+
endif
35+
36+
all: setup build
37+
38+
ensure-build-dir:
39+
mkdir -p out
40+
41+
build-deps: ## Install dependencies
42+
dep ensure -v
43+
44+
compile: ensure-build-dir ## Compile dobby
45+
$(GO_BINARY) build -ldflags "-X main.majorVersion=$(VERSION) -X main.minorVersion=${BUILD}" -o $(APP_EXECUTABLE) ./main.go
46+
47+
run: compile ## Run dobby
48+
./out/dobby server
49+
50+
compile-linux: ensure-build-dir ## Compile dobby for linux
51+
GOOS=linux GOARCH=amd64 $(GO_BINARY) build -ldflags "-X main.majorVersion=$(VERSION) -X main.minorVersion=${BUILD}" -o $(APP_EXECUTABLE) ./main.go
52+
53+
build: build-deps fmt vet lint-all compile ## Build the application
54+
55+
compress: compile ## Compress the binary
56+
upx $(APP_EXECUTABLE)
57+
58+
fmt:
59+
$(GO_BINARY) fmt $(SRC_PACKAGES)
60+
61+
vet:
62+
$(GO_BINARY) vet $(SRC_PACKAGES)
63+
64+
setup-golangci-lint:
65+
ifeq ($(GOMETA_LINT),)
66+
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s $(GOLANGCI_LINT_VERSION)
67+
endif
68+
69+
setup: setup-golangci-lint ensure-build-dir ## Setup environment
70+
ifeq ($(DEP),)
71+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
72+
endif
73+
ifeq ($(GOLINT),)
74+
$(GO_BINARY) get -u golang.org/x/lint/golint
75+
endif
76+
ifeq ($(RICHGO),)
77+
$(GO_BINARY) get -u github.com/kyoh86/richgo
78+
endif
79+
80+
lint-all: lint setup-golangci-lint
81+
$(GOMETA_LINT) run
82+
83+
lint:
84+
./scripts/lint $(SRC_PACKAGES)
85+
86+
test-all: test test.integration
87+
88+
test: ensure-build-dir ## Run tests
89+
ENVIRONMENT=test $(GO_BINARY) test $(SRC_PACKAGES) -p=1 -coverprofile ./out/coverage -short -v | grep -vi "start" | grep -vi "no test files"
90+
91+
test-cover-html: ## Run tests with coverage
92+
mkdir -p ./out
93+
@echo "mode: count" > coverage-all.out
94+
$(foreach pkg, $(SRC_PACKAGES),\
95+
ENVIRONMENT=test $(GO_BINARY) test -coverprofile=coverage.out -covermode=count $(pkg);\
96+
tail -n +2 coverage.out >> coverage-all.out;)
97+
$(GO_BINARY) tool cover -html=coverage-all.out -o out/coverage.html

cmd/root.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"github.com/urfave/cli"
5+
"log"
6+
)
7+
8+
func dieIf(err error) {
9+
if err != nil {
10+
log.Fatal(err)
11+
}
12+
}
13+
14+
// Run is the entry point for dobby app
15+
func Run(args []string) error {
16+
app := cli.NewApp()
17+
app.Description = "Web app which obey's invokers action"
18+
app.Usage = "Waiting to serve the order"
19+
app.Name = "dobby"
20+
app.Version = BuildVersion()
21+
22+
app.Commands = []cli.Command{
23+
serverCmd(),
24+
}
25+
26+
return app.Run(args)
27+
}

cmd/server.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"github.com/thecasualcoder/dobby/pkg"
5+
"github.com/urfave/cli"
6+
)
7+
8+
func serverCmd() cli.Command {
9+
return cli.Command{
10+
Name: "server",
11+
Description: "run dobby in server mode",
12+
Usage: "run dobby server",
13+
Action: runServer,
14+
Flags: serverFlags(),
15+
}
16+
}
17+
18+
func serverFlags() []cli.Flag {
19+
return []cli.Flag{
20+
cli.StringFlag{
21+
Name: "bind-address, a",
22+
Value: "127.0.0.1",
23+
Usage: "Address of dobby server.",
24+
},
25+
cli.StringFlag{
26+
Name: "port, p",
27+
Value: "4444",
28+
Usage: "Port which will be used by dobby server.",
29+
},
30+
}
31+
}
32+
33+
func runServer(context *cli.Context) {
34+
bindAddress := context.String("bind-address")
35+
port := context.String("port")
36+
err := pkg.Run(bindAddress, port)
37+
dieIf(err)
38+
}

cmd/version.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import "fmt"
4+
5+
var majorVersion string
6+
var minorVersion string
7+
8+
// BuildVersion returns build version specified at compile time
9+
// by default it will be 1.0.0-dev
10+
func BuildVersion() string {
11+
major := majorVersion
12+
minor := minorVersion
13+
if major == "" {
14+
major = "1.0"
15+
}
16+
if minor == "" {
17+
minor = "0-dev"
18+
}
19+
return fmt.Sprintf("%s.%s", major, minor)
20+
}
21+
22+
// SetBuildVersion use this to override the major and minor version
23+
// of the application
24+
func SetBuildVersion(major string, minor string) {
25+
majorVersion = major
26+
minorVersion = minor
27+
}

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"github.com/thecasualcoder/dobby/cmd"
5+
"log"
6+
"os"
7+
)
8+
9+
var majorVersion string
10+
var minorVersion string
11+
12+
func main() {
13+
cmd.SetBuildVersion(majorVersion, minorVersion)
14+
err := cmd.Run(os.Args)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
}

pkg/handler/ping.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package handler
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func Health(c *gin.Context) {
6+
c.JSON(200, gin.H{"healthy": true})
7+
}

0 commit comments

Comments
 (0)