-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
2,095 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.