Skip to content

Commit

Permalink
Refactor: extract main and AppEngine routers (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Sep 26, 2019
1 parent 1e62a5a commit cfdbaef
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 39 deletions.
18 changes: 2 additions & 16 deletions appengine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,11 @@ package main
import (
"net/http"

gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/int128/jira-to-slack/pkg/handlers"
aeRouter "github.com/int128/jira-to-slack/pkg/router/appengine"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
)

func router() http.Handler {
m := mux.NewRouter()
m.Handle("/", &handlers.Index{}).Methods("GET")
m.Handle("/", gh.ContentTypeHandler(&handlers.Webhook{
HTTPClientFactory: func(r *http.Request) *http.Client {
return urlfetch.Client(r.Context())
},
}, "application/json")).Methods("POST")
return m
}

func main() {
http.Handle("/", router())
http.Handle("/", aeRouter.New())
appengine.Main()
}
17 changes: 2 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ import (
"net/http"
"os"

gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/int128/jira-to-slack/pkg/handlers"
"github.com/int128/jira-to-slack/pkg/router"
)

var version string

const defaultPort = "3000"

func router() http.Handler {
r := mux.NewRouter()
r.Handle("/", &handlers.Index{}).Methods("GET")
r.Handle("/", gh.ContentTypeHandler(&handlers.Webhook{}, "application/json")).Methods("POST")

m := http.NewServeMux()
m.Handle("/", gh.LoggingHandler(os.Stdout, r))
m.Handle("/healthz", &handlers.Healthz{})
return m
}

func main() {
log.Printf("jira-to-slack %s", version)

Expand All @@ -35,7 +22,7 @@ func main() {
addr := ":" + port

log.Printf("Listening on %s", addr)
if err := http.ListenAndServe(addr, router()); err != nil {
if err := http.ListenAndServe(addr, router.New()); err != nil {
log.Fatalf("Error while listening on %s: %s", addr, err)
}
}
5 changes: 3 additions & 2 deletions pkg/handlers/webhook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,7 +15,7 @@ import (

// Webhook handles requests from JIRA webhook.
type Webhook struct {
HTTPClientFactory func(*http.Request) *http.Client // Default to http.DefaultClient
HTTPClientFactory func(context.Context) *http.Client // Default to http.DefaultClient
}

type WebhookParams struct {
Expand Down Expand Up @@ -85,7 +86,7 @@ func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
var hc *http.Client
if h.HTTPClientFactory != nil {
hc = h.HTTPClientFactory(r)
hc = h.HTTPClientFactory(ctx)
}

in := usecases.WebhookIn{
Expand Down
27 changes: 27 additions & 0 deletions pkg/router/appengine/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package appengine

import (
"context"
"net/http"

gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/int128/jira-to-slack/pkg/handlers"
"google.golang.org/appengine/urlfetch"
)

func httpClientFactory(ctx context.Context) *http.Client {
return urlfetch.Client(ctx)
}

func New() http.Handler {
m := mux.NewRouter()
m.Handle("/", &handlers.Index{}).Methods("GET")
m.Handle("/",
gh.ContentTypeHandler(
&handlers.Webhook{HTTPClientFactory: httpClientFactory},
"application/json",
),
).Methods("POST")
return m
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main
package appengine

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

func TestRouter(t *testing.T) {
s := httptest.NewServer(router())
func TestNew(t *testing.T) {
s := httptest.NewServer(New())
defer s.Close()

t.Run("index", func(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package router

import (
"net/http"
"os"

gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/int128/jira-to-slack/pkg/handlers"
)

func New() http.Handler {
r := mux.NewRouter()
r.Handle("/", &handlers.Index{}).Methods("GET")
r.Handle("/", gh.ContentTypeHandler(&handlers.Webhook{}, "application/json")).Methods("POST")

m := http.NewServeMux()
m.Handle("/", gh.LoggingHandler(os.Stdout, r))
m.Handle("/healthz", &handlers.Healthz{})
return m
}
6 changes: 3 additions & 3 deletions main_test.go → pkg/router/router_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main
package router

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

func TestRouter(t *testing.T) {
s := httptest.NewServer(router())
func TestNew(t *testing.T) {
s := httptest.NewServer(New())
defer s.Close()

t.Run("index", func(t *testing.T) {
Expand Down

0 comments on commit cfdbaef

Please sign in to comment.