Skip to content

Commit

Permalink
+start graphql servicer. +separate route registry.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicerobot committed Oct 29, 2016
1 parent 507be78 commit 6366acc
Show file tree
Hide file tree
Showing 14 changed files with 839 additions and 83 deletions.
30 changes: 30 additions & 0 deletions vendor/application/route_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package application

import (
"middleware/logging"
"service/login"

"github.com/gorilla/mux"

httptransport "github.com/go-kit/kit/transport/http"

"golang.org/x/net/context"
)

//
func routeLoginService(ctx context.Context, r *mux.Router) {

logged := logging.New("login", Settings.Program.Name).Middleware()

loginService := login.New()

// POST /

r.Methods("POST").Path("/").Handler(httptransport.NewServer(
ctx,
logged(loginService.Endpoint()),
loginService.Decoder,
servered(jsoned(loginService.Encoder)),
)).Name("POST")

}
31 changes: 31 additions & 0 deletions vendor/application/route_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package application

import (
"middleware/logging"
"service/query"
"transport/http/caching"

"github.com/gorilla/mux"

httptransport "github.com/go-kit/kit/transport/http"

"golang.org/x/net/context"
)

//
func routeQueryService(ctx context.Context, r *mux.Router) {

logged := logging.New("query", Settings.Program.Name).Middleware()

queryService := query.New()

// GET /:id

r.Methods("GET").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(queryService.Endpoint()),
queryService.Decoder,
servered(jsoned(queryService.Encoder)),
))).Name("GET Id")

}
31 changes: 31 additions & 0 deletions vendor/application/route_starwars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package application

import (
"middleware/logging"
"service/starwars"
"transport/http/caching"

"github.com/gorilla/mux"

httptransport "github.com/go-kit/kit/transport/http"

"golang.org/x/net/context"
)

//
func routeStarWarsService(ctx context.Context, r *mux.Router) {

logged := logging.New("starwars", Settings.Program.Name).Middleware()

starWarsService := starwars.New()

// GET /:query

r.Methods("GET").Path("/{query}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(starWarsService.Endpoint()),
starWarsService.Decoder,
servered(jsoned(starWarsService.Encoder)),
))).Name("GET Id")

}
67 changes: 67 additions & 0 deletions vendor/application/route_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package application

import (
"middleware/logging"
"service/testing"
"transport/http/caching"

"github.com/gorilla/mux"

httptransport "github.com/go-kit/kit/transport/http"

"golang.org/x/net/context"
)

//
func routeTestService(ctx context.Context, r *mux.Router) {

logged := logging.New("test", Settings.Program.Name).Middleware()

testService := testing.New()

// POST /

r.Methods("POST").Path("/").Handler(httptransport.NewServer(
ctx,
logged(testService.Post()),
testService.DecodePost,
servered(jsoned(testService.EncodePost)),
)).Name("POST")

// GET /:id

r.Methods("POST", "GET").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Get()),
testService.DecodeGet,
servered(jsoned(testService.EncodeGet)),
))).Name("GET/POST Id")

// PUT /:id

r.Methods("PUT").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Put()),
testService.DecodePut,
servered(jsoned(testService.EncodePut)),
))).Name("PUT Id")

// PATCH /:id

r.Methods("PATCH").Path("/{id}").Handler(httptransport.NewServer(
ctx,
logged(testService.Patch()),
testService.DecodePatch,
servered(jsoned(testService.EncodePatch)),
)).Name("PATCH Id")

// DELETE /:id

r.Methods("DELETE").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Delete()),
testService.DecodeDelete,
servered(jsoned(testService.EncodeDelete)),
))).Name("DELETE Id")

}
86 changes: 8 additions & 78 deletions vendor/application/routes.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package application

import (
"middleware/logging"
"net/http"
"service/login"
"service/testing"
"transport/http/caching"
"transport/http/headered"

"github.com/gorilla/mux"

httptransport "github.com/go-kit/kit/transport/http"

"golang.org/x/net/context"
)

Expand All @@ -24,6 +18,14 @@ func routeServices() *mux.Router {

routeLoginService(ctx, r.PathPrefix("/login").Subrouter().StrictSlash(true))

// Query

routeQueryService(ctx, r.PathPrefix("/query").Subrouter().StrictSlash(true))

// StarWars

routeStarWarsService(ctx, r.PathPrefix("/starwars").Subrouter().StrictSlash(true))

// Testing

routeTestService(ctx, r.PathPrefix("/test").Subrouter().StrictSlash(true))
Expand All @@ -40,75 +42,3 @@ var (
hs.Set("Content-Type", "application/json")
})
)

//
func routeLoginService(ctx context.Context, r *mux.Router) {

logged := logging.New("login").Middleware()

loginService := login.New()

// POST /

r.Methods("POST").Path("/").Handler(httptransport.NewServer(
ctx,
logged(loginService.Endpoint()),
loginService.Decoder,
servered(jsoned(loginService.Encoder)),
)).Name("POST")

}

//
func routeTestService(ctx context.Context, r *mux.Router) {

logged := logging.New("test").Middleware()

testService := testing.New()

// POST /

r.Methods("POST").Path("/").Handler(httptransport.NewServer(
ctx,
logged(testService.Post()),
testService.DecodePost,
servered(jsoned(testService.EncodePost)),
)).Name("POST")

// GET /:id

r.Methods("POST", "GET").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Get()),
testService.DecodeGet,
servered(jsoned(testService.EncodeGet)),
))).Name("GET/POST Id")

// PUT /:id

r.Methods("PUT").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Put()),
testService.DecodePut,
servered(jsoned(testService.EncodePut)),
))).Name("PUT Id")

// PATCH /:id

r.Methods("PATCH").Path("/{id}").Handler(httptransport.NewServer(
ctx,
logged(testService.Patch()),
testService.DecodePatch,
servered(jsoned(testService.EncodePatch)),
)).Name("PATCH Id")

// DELETE /:id

r.Methods("DELETE").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer(
ctx,
logged(testService.Delete()),
testService.DecodeDelete,
servered(jsoned(testService.EncodeDelete)),
))).Name("DELETE Id")

}
4 changes: 2 additions & 2 deletions vendor/application/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Start() error {

privPort := strconv.Itoa(Settings.Port)
pubPort := "80"
if Settings.Port != 443 || Settings.Port >= 3000 {
pubPort = strconv.Itoa(Settings.Port - 443)
if Settings.Port != 443 || Settings.Port >= 3443 {
pubPort = strconv.Itoa(Settings.Port - 363)
}
srv := &http.Server{
ReadTimeout: 5 * time.Second,
Expand Down
11 changes: 8 additions & 3 deletions vendor/application/vendor/middleware/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ type logger struct {
}

//
func New(name string) *logger {
func New(options ...string) *logger {
l := log.NewLogfmtLogger(os.Stderr)
c := log.NewContext(l).With("time", log.DefaultTimestampUTC)
if name != "" {
c = c.With("service", name)
for i := len(options) - 1; i >= 0; i-- {
switch i {
case 0:
c = c.With("service", options[i])
case 1:
c = c.With("app", options[i])
}
}
return &logger{c}
}
Expand Down
Empty file.
86 changes: 86 additions & 0 deletions vendor/application/vendor/service/query/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package query

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/go-kit/kit/endpoint"

"golang.org/x/net/context"
)

// Errors

// ErrNoQuery.
var ErrNoQuery = errors.New("empty query")

// Model

// Query operations.
type Service interface {
Query(string) (string, error)
}

// Private Query model.
type queryService struct{}

// Query requests.
type queryRequest struct {
S string `json:"s"`
}

// Query response.
type queryResponse struct {
V string `json:"v"`
Err string `json:"err,omitempty"` // errors don't define JSON marshaling
}

//
func (queryService) Query(s string) (string, error) {
if s == "" {
return "", ErrNoQuery
}
return strings.ToUpper(s), nil
}

// Initialization

//
type queryEndpoint endpoint.Endpoint

//
func New() queryEndpoint {
svc := queryService{}

return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(queryRequest)
v, err := svc.Query(req.S)
if err != nil {
return queryResponse{v, err.Error()}, nil
}
return queryResponse{v, ""}, nil
}
}

// Convenience to get a go-kit type back of query's private endpoint type.
func (e queryEndpoint) Endpoint() endpoint.Endpoint {
return endpoint.Endpoint(e)
}

// Request/response encoding and decoding.

//
func (e queryEndpoint) Decoder(_ context.Context, r *http.Request) (interface{}, error) {
var request queryRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}

//
func (e queryEndpoint) Encoder(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
Loading

0 comments on commit 6366acc

Please sign in to comment.