Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pacedotdev/oto
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.4.0
Choose a base ref
...
head repository: pacedotdev/oto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.4.1
Choose a head ref
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Jul 15, 2020

  1. Copy the full SHA
    064c785 View commit details
Showing with 28 additions and 3 deletions.
  1. +4 −1 otohttp/go.mod
  2. +2 −0 otohttp/go.sum
  3. +7 −2 otohttp/server.go
  4. +15 −0 otohttp/server_test.go
5 changes: 4 additions & 1 deletion otohttp/go.mod
Original file line number Diff line number Diff line change
@@ -2,4 +2,7 @@ module github.com/pacedotdev/oto/otohttp

go 1.13

require github.com/pkg/errors v0.8.1
require (
github.com/matryer/is v1.4.0
github.com/pkg/errors v0.8.1
)
2 changes: 2 additions & 0 deletions otohttp/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9 changes: 7 additions & 2 deletions otohttp/server.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,10 @@ import (

// Server handles oto requests.
type Server struct {
// Basepath is the path prefix to match.
// Default: /oto/
Basepath string

routes map[string]http.Handler
// NotFound is the http.Handler to use when a resource is
// not found.
@@ -25,7 +29,8 @@ type Server struct {
// NewServer makes a new Server.
func NewServer() *Server {
return &Server{
routes: make(map[string]http.Handler),
Basepath: "/oto/",
routes: make(map[string]http.Handler),
OnErr: func(w http.ResponseWriter, r *http.Request, err error) {
errObj := struct {
Error string `json:"error"`
@@ -42,7 +47,7 @@ func NewServer() *Server {

// Register adds a handler for the specified service method.
func (s *Server) Register(service, method string, h http.HandlerFunc) {
s.routes[fmt.Sprintf("/oto/%s.%s", service, method)] = h
s.routes[fmt.Sprintf("%s%s.%s", s.Basepath, service, method)] = h
}

// ServeHTTP serves the request.
15 changes: 15 additions & 0 deletions otohttp/server_test.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,21 @@ func TestServer(t *testing.T) {
is.Equal(w.Body.String(), `{"greeting":"Hi Mat"}`)
}

func TestServerBasepath(t *testing.T) {
is := is.New(t)
srv := NewServer()
srv.Basepath = "/api/"
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"greeting":"Hi Mat"}`))
})
srv.Register("Service", "Method", h)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, "/api/Service.Method", strings.NewReader(`{"name":"Mat"}`))
srv.ServeHTTP(w, r)
is.Equal(w.Code, http.StatusOK)
is.Equal(w.Body.String(), `{"greeting":"Hi Mat"}`)
}

func TestEncode(t *testing.T) {
is := is.New(t)
data := struct {