Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Sep 13, 2017
2 parents b1e40a0 + 6859378 commit 61e31c2
Show file tree
Hide file tree
Showing 34 changed files with 2,095 additions and 113 deletions.
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FROM scratch

ENV K8SAPP_LOCAL_HOST 0.0.0.0
ENV K8SAPP_LOCAL_PORT 8080
ENV K8SAPP_LOG_LEVEL 0

EXPOSE $K8SAPP_LOCAL_PORT

COPY certs /etc/ssl/
COPY bin/linux-amd64/k8sapp /

Expand Down
12 changes: 11 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.3"

[[constraint]]
name = "github.com/julienschmidt/httprouter"
revision = "975b5c4c7c21c0e3d2764200bf2aa8e34657ae6e"

[[constraint]]
name = "github.com/kelseyhightower/envconfig"
revision = "70f0258d44cbaa3b6a2581d82f58da01a38e4de4"
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
APP=k8sapp
PROJECT=github.com/takama/k8sapp
REGISTRY?=docker.io/takama
CONTAINER_IMAGE?=${REGISTRY}/${APP}
CONTAINER_NAME?=${APP}
CA_DIR?=certs

# Use the 0.0.0 tag for testing, it shouldn't clobber any release builds
RELEASE?=0.2.2
RELEASE?=0.3.0
GOOS?=linux
GOARCH?=amd64

K8SAPP_LOCAL_HOST?=0.0.0.0
K8SAPP_LOCAL_PORT?=8080
K8SAPP_LOG_LEVEL?=0

CONTAINER_IMAGE?=${REGISTRY}/${APP}
CONTAINER_NAME?=${APP}

REPO_INFO=$(shell git config --get remote.origin.url)

ifndef COMMIT
Expand Down Expand Up @@ -55,7 +60,10 @@ push: build
.PHONY: run
run: build
@echo "+ $@"
@docker run --name ${CONTAINER_NAME} \
@docker run --name ${CONTAINER_NAME} -p ${K8SAPP_LOCAL_PORT}:${K8SAPP_LOCAL_PORT} \
-e "K8SAPP_LOCAL_HOST=${K8SAPP_LOCAL_HOST}" \
-e "K8SAPP_LOCAL_PORT=${K8SAPP_LOCAL_PORT}" \
-e "K8SAPP_LOG_LEVEL=${K8SAPP_LOG_LEVEL}" \
-d $(CONTAINER_IMAGE):$(RELEASE)
@sleep 1
@docker logs ${CONTAINER_NAME}
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ type Logger interface {
Just make your choice

```go
func New(cfg *Config) Logger {
// return newLogrus(cfg)
// return newXLog(cfg)
return newStdLog(cfg)
func Run() (err error) {
// log := xlog.New()
// log := logrus.New()
log := stdlog.New(&logger.Config{
Level: logger.LevelDebug,
Time: true,
UTC: true,
})
...
}
```

Expand Down
15 changes: 14 additions & 1 deletion cmd/k8sapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
package main

import (
"fmt"
"log"

"github.com/takama/k8sapp/pkg/config"
"github.com/takama/k8sapp/pkg/service"
)

func main() {
if err := service.Run(); err != nil {
// Load ENV configuration
cfg := new(config.Config)
if err := cfg.Load(config.SERVICENAME); err != nil {
log.Fatal(err)
}

// Configure service and get router
router, err := service.Setup(cfg)
if err != nil {
log.Fatal(err)
}

// Listen and serve handlers
router.Listen(fmt.Sprintf("%s:%d", cfg.LocalHost, cfg.LocalPort))
}
18 changes: 17 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# Version 0.2.2
# Version 0.3.0

[Documentation](README.md)

## Changelog since 0.2.2

### Documentation

- Added usage description of the loggers: [xlog](https://github.com/rs/xlog), [logrus](https://github.com/sirupsen/logrus)

### Codebase

- Added routers interface ([#22](https://github.com/takama/k8sapp/pull/22), [@takama](https://github.com/takama))
- Implemented Bit-Route interface ([#23](https://github.com/takama/k8sapp/pull/23), [@takama](https://github.com/takama))
- Implemented httprouter interface ([#24](https://github.com/takama/k8sapp/pull/24), [@takama](https://github.com/takama))
- Added environment configuration ([#26](https://github.com/takama/k8sapp/pull/26), [@takama](https://github.com/takama))
- Refactoring of the packages relations ([#25](https://github.com/takama/k8sapp/pull/25), [@takama](https://github.com/takama))
- Added health/ready handlers ([#27](https://github.com/takama/k8sapp/pull/27), [@takama](https://github.com/takama))


## Changelog since 0.2.1

### Tests
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@

package config

import (
"github.com/kelseyhightower/envconfig"
"github.com/takama/k8sapp/pkg/logger"
)

const (
// SERVICENAME contains a service name prefix which used in ENV variables
SERVICENAME = "K8SAPP"
)

// Config contains ENV variables
type Config struct {
// Local service host
LocalHost string `split_words:"true"`
// Local service port
LocalPort int `split_words:"true"`
// Logging level in logger.Level notation
LogLevel logger.Level `split_words:"true"`
}

// Load settles ENV variables into Config structure
func (c *Config) Load(serviceName string) error {
return envconfig.Process(serviceName, c)
}
11 changes: 11 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

import "testing"

func TestLoadConfig(t *testing.T) {
config := new(Config)
err := config.Load(SERVICENAME)
if err != nil {
t.Error("Expected loading of environment vars, got", err)
}
}
41 changes: 41 additions & 0 deletions pkg/handlers/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package handlers

import (
"fmt"
"net/http"

"github.com/takama/k8sapp/pkg/config"
"github.com/takama/k8sapp/pkg/logger"
"github.com/takama/k8sapp/pkg/router"
"github.com/takama/k8sapp/pkg/version"
)

// Handler defines common part for all handlers
type Handler struct {
logger logger.Logger
config *config.Config
}

// New returns new instance of the Handler
func New(logger logger.Logger, config *config.Config) *Handler {
return &Handler{
logger: logger,
config: config,
}
}

// Base handler implements middleware logic
func (h *Handler) Base(handle func(router.Control)) func(router.Control) {
return func(c router.Control) {

// TODO: Add custom logic here

handle(c)
}
}

// Root handler shows version
func (h *Handler) Root(c router.Control) {
c.Code(http.StatusOK)
c.Write(fmt.Sprintf("%s v%s", config.SERVICENAME, version.RELEASE))
}
40 changes: 40 additions & 0 deletions pkg/handlers/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handlers

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/takama/k8sapp/pkg/config"
"github.com/takama/k8sapp/pkg/logger"
"github.com/takama/k8sapp/pkg/logger/standard"
"github.com/takama/k8sapp/pkg/router/bitroute"
"github.com/takama/k8sapp/pkg/version"
)

func TestRoot(t *testing.T) {
h := New(standard.New(&logger.Config{}), new(config.Config))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.Base(h.Root)(bitroute.NewControl(w, r))
})

testHandler(t, handler, http.StatusOK, fmt.Sprintf("%s v%s", config.SERVICENAME, version.RELEASE))
}

func testHandler(t *testing.T, handler http.HandlerFunc, code int, body string) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}

trw := httptest.NewRecorder()
handler.ServeHTTP(trw, req)

if trw.Code != code {
t.Error("Expected status code:", code, "got", trw.Code)
}
if trw.Body.String() != body {
t.Error("Expected body", body, "got", trw.Body.String())
}
}
13 changes: 13 additions & 0 deletions pkg/handlers/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlers

import (
"net/http"

"github.com/takama/k8sapp/pkg/router"
)

// Health returns "OK" if service is alive
func (h *Handler) Health(c router.Control) {
c.Code(http.StatusOK)
c.Write(http.StatusText(http.StatusOK))
}
20 changes: 20 additions & 0 deletions pkg/handlers/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package handlers

import (
"net/http"
"testing"

"github.com/takama/k8sapp/pkg/config"
"github.com/takama/k8sapp/pkg/logger"
"github.com/takama/k8sapp/pkg/logger/standard"
"github.com/takama/k8sapp/pkg/router/bitroute"
)

func TestHealth(t *testing.T) {
h := New(standard.New(&logger.Config{}), new(config.Config))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.Base(h.Health)(bitroute.NewControl(w, r))
})

testHandler(t, handler, http.StatusOK, http.StatusText(http.StatusOK))
}
16 changes: 16 additions & 0 deletions pkg/handlers/ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handlers

import (
"net/http"

"github.com/takama/k8sapp/pkg/router"
)

// Ready returns "OK" if service is ready to serve traffic
func (h *Handler) Ready(c router.Control) {
// TODO: possible use cases:
// load data from a database, a message broker, any external services, etc

c.Code(http.StatusOK)
c.Write(http.StatusText(http.StatusOK))
}
20 changes: 20 additions & 0 deletions pkg/handlers/ready_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package handlers

import (
"net/http"
"testing"

"github.com/takama/k8sapp/pkg/config"
"github.com/takama/k8sapp/pkg/logger"
"github.com/takama/k8sapp/pkg/logger/standard"
"github.com/takama/k8sapp/pkg/router/bitroute"
)

func TestReady(t *testing.T) {
h := New(standard.New(&logger.Config{}), new(config.Config))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.Base(h.Ready)(bitroute.NewControl(w, r))
})

testHandler(t, handler, http.StatusOK, http.StatusText(http.StatusOK))
}
8 changes: 0 additions & 8 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,3 @@ type Config struct {
// Use UTC time
UTC bool
}

// New returns new logger
func New(cfg *Config) Logger {
// There should be any implementation which compatible with logger interface
// return newLogrus(cfg)
// return newXLog(cfg)
return newStdLog(cfg)
}
Loading

0 comments on commit 61e31c2

Please sign in to comment.