Skip to content

Commit

Permalink
Add gorilla mux support, add glide support for docker, add file watch…
Browse files Browse the repository at this point in the history
…er to restart service when needed.
  • Loading branch information
Kujtim committed Oct 9, 2017
1 parent ebfca0d commit 28af64a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 69 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is what `GoKit Cli` is aiming to change.


# Prerequisites
GoKit Cli needs to ne installed using `go get` and `go install` so `Go` is a requirement to be able to test your services
GoKit Cli needs to be installed using `go get` and `go install` so `Go` is a requirement to be able to test your services
[gokit](https://github.com/go-kit/kit) is needed.

# Table of Content
Expand Down Expand Up @@ -119,4 +119,5 @@ To start your services just run
docker-compose up
```

If you change something in your code just restart the docker containers using `docker-compose restart`.
After you run `docker-compose up` your services will start up and eny change you make to your code will automatically
rebuild and restart your service (only the service that is changed)
6 changes: 5 additions & 1 deletion cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/kit/generator"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// dockerCmd represents the docker command
Expand All @@ -12,7 +13,7 @@ var dockerCmd = &cobra.Command{
Aliases: []string{"d"},
Short: "Generate docker files",
Run: func(cmd *cobra.Command, args []string) {
g := generator.NewGenerateDocker()
g := generator.NewGenerateDocker(viper.GetBool("g_d_glide"))
if err := g.Generate(); err != nil {
logrus.Error(err)
}
Expand All @@ -21,4 +22,7 @@ var dockerCmd = &cobra.Command{

func init() {
generateCmd.AddCommand(dockerCmd)
dockerCmd.Flags().Bool("glide", false, "Generate docker for project that uses glide package manager")
viper.BindPFlag("g_d_glide", dockerCmd.Flags().Lookup("glide"))

}
3 changes: 3 additions & 0 deletions cmd/g_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var initserviceCmd = &cobra.Command{
args[0],
viper.GetString("g_s_transport"),
smw,
viper.GetBool("g_s_gorilla"),
emw,
methods,
)
Expand All @@ -47,11 +48,13 @@ func init() {
generateCmd.AddCommand(initserviceCmd)
initserviceCmd.Flags().StringP("transport", "t", "http", "The transport you want your service to be initiated with")
initserviceCmd.Flags().Bool("dmw", false, "Generate default middleware for service and endpoint")
initserviceCmd.Flags().Bool("gorilla", false, "Generate http using gorilla mux")
initserviceCmd.Flags().StringArrayVarP(&methods, "methods", "m", []string{}, "Specify methods to be generated")
initserviceCmd.Flags().Bool("svc-mdw", false, "If set a default Logging and Instrumental middleware will be created and attached to the service")
initserviceCmd.Flags().Bool("endpoint-mdw", false, "If set a default Logging and Tracking middleware will be created and attached to the endpoint")
viper.BindPFlag("g_s_transport", initserviceCmd.Flags().Lookup("transport"))
viper.BindPFlag("g_s_dmw", initserviceCmd.Flags().Lookup("dmw"))
viper.BindPFlag("g_s_gorilla", initserviceCmd.Flags().Lookup("gorilla"))
viper.BindPFlag("g_s_svc_mdw", initserviceCmd.Flags().Lookup("svc-mdw"))
viper.BindPFlag("g_s_endpoint_mdw", initserviceCmd.Flags().Lookup("endpoint-mdw"))
}
94 changes: 68 additions & 26 deletions generator/add_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type GenerateTransport struct {
BaseGenerator
name string
transport string
gorillaMux bool
interfaceName string
destPath string
methods []string
Expand All @@ -38,9 +39,10 @@ type GenerateTransport struct {
}

// NewGenerateTransport returns a transport generator.
func NewGenerateTransport(name string, transport string, methods []string) Gen {
func NewGenerateTransport(name string, gorillaMux bool, transport string, methods []string) Gen {
i := &GenerateTransport{
name: name,
gorillaMux: gorillaMux,
interfaceName: utils.ToCamelCase(name + "Service"),
destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)),
methods: methods,
Expand Down Expand Up @@ -85,12 +87,12 @@ func (g *GenerateTransport) Generate() (err error) {
}
switch g.transport {
case "http":
tG := newGenerateHTTPTransport(g.name, g.serviceInterface, g.methods)
tG := newGenerateHTTPTransport(g.name, g.gorillaMux, g.serviceInterface, g.methods)
err = tG.Generate()
if err != nil {
return err
}
tbG := newGenerateHTTPTransportBase(g.name, g.serviceInterface, g.methods, mth)
tbG := newGenerateHTTPTransportBase(g.name, g.gorillaMux, g.serviceInterface, g.methods, mth)
err = tbG.Generate()
if err != nil {
return err
Expand Down Expand Up @@ -179,23 +181,24 @@ func (g *GenerateTransport) removeUnwantedMethods() {

type generateHTTPTransport struct {
BaseGenerator
name string
methods []string
interfaceName string
destPath string
generateFirstTime bool
file *parser.File
filePath string
serviceInterface parser.Interface
name string
methods []string
interfaceName string
destPath string
generateFirstTime, gorillaMux bool
file *parser.File
filePath string
serviceInterface parser.Interface
}

func newGenerateHTTPTransport(name string, serviceInterface parser.Interface, methods []string) Gen {
func newGenerateHTTPTransport(name string, gorillaMux bool, serviceInterface parser.Interface, methods []string) Gen {
t := &generateHTTPTransport{
name: name,
methods: methods,
interfaceName: utils.ToCamelCase(name + "Service"),
destPath: fmt.Sprintf(viper.GetString("gk_http_path_format"), utils.ToLowerSnakeCase(name)),
serviceInterface: serviceInterface,
gorillaMux: gorillaMux,
}
t.filePath = path.Join(t.destPath, viper.GetString("gk_http_file_name"))
t.srcFile = jen.NewFilePath(t.destPath)
Expand Down Expand Up @@ -271,11 +274,51 @@ func (g *generateHTTPTransport) Generate() (err error) {
fmt.Sprintf("make%sHandler creates the handler logic", m.Name),
})
g.code.NewLine()
var st *jen.Statement
if g.gorillaMux {
st = jen.Id("m").Dot("Methods").Call(
jen.Lit("POST"),
).Dot("Path").Call(
jen.Lit("/" + strings.Replace(utils.ToLowerSnakeCase(m.Name), "_", "-", -1)),
).Dot("Handler").Call(
jen.Qual("github.com/gorilla/handlers", "CORS").Call(
jen.Qual("github.com/gorilla/handlers", "AllowedMethods").Call(
jen.Index().String().Values(jen.Lit("POST")),
),
jen.Qual("github.com/gorilla/handlers", "AllowedOrigins").Call(
jen.Index().String().Values(jen.Lit("*")),
),
).Call(
jen.Qual("github.com/go-kit/kit/transport/http", "NewServer").Call(
jen.Id(fmt.Sprintf("endpoints.%sEndpoint", m.Name)),
jen.Id(fmt.Sprintf("decode%sRequest", m.Name)),
jen.Id(fmt.Sprintf("encode%sResponse", m.Name)),
jen.Id("options..."),
),
),
)
} else {
st = jen.Id("m").Dot("Handle").Call(
jen.Lit("/"+strings.Replace(utils.ToLowerSnakeCase(m.Name), "_", "-", -1)),
jen.Qual("github.com/go-kit/kit/transport/http", "NewServer").Call(
jen.Id(fmt.Sprintf("endpoints.%sEndpoint", m.Name)),
jen.Id(fmt.Sprintf("decode%sRequest", m.Name)),
jen.Id(fmt.Sprintf("encode%sResponse", m.Name)),
jen.Id("options..."),
),
)
}
var param *jen.Statement
if g.gorillaMux {
param = jen.Id("m").Id("*").Qual("github.com/gorilla/mux", "Router")
} else {
param = jen.Id("m").Id("*").Qual("net/http", "ServeMux")
}
g.code.appendFunction(
fmt.Sprintf("make%sHandler", m.Name),
nil,
[]jen.Code{
jen.Id("m").Id("*").Qual("net/http", "ServeMux"),
param,
jen.Id("endpoints").Qual(endpImports, "Endpoints"),
jen.Id("options").Index().Qual(
"github.com/go-kit/kit/transport/http",
Expand All @@ -284,15 +327,7 @@ func (g *generateHTTPTransport) Generate() (err error) {
},
[]jen.Code{},
"",
jen.Id("m").Dot("Handle").Call(
jen.Lit("/"+strings.Replace(utils.ToLowerSnakeCase(m.Name), "_", "-", -1)),
jen.Qual("github.com/go-kit/kit/transport/http", "NewServer").Call(
jen.Id(fmt.Sprintf("endpoints.%sEndpoint", m.Name)),
jen.Id(fmt.Sprintf("decode%sRequest", m.Name)),
jen.Id(fmt.Sprintf("encode%sResponse", m.Name)),
jen.Id("options..."),
),
),
st,
)
g.code.NewLine()

Expand Down Expand Up @@ -490,13 +525,15 @@ type generateHTTPTransportBase struct {
filePath string
file *parser.File
httpFilePath string
gorillaMux bool
serviceInterface parser.Interface
}

func newGenerateHTTPTransportBase(name string, serviceInterface parser.Interface, methods []string, allMethods []parser.Method) Gen {
func newGenerateHTTPTransportBase(name string, gorillaMux bool, serviceInterface parser.Interface, methods []string, allMethods []parser.Method) Gen {
t := &generateHTTPTransportBase{
name: name,
methods: methods,
gorillaMux: gorillaMux,
allMethods: allMethods,
interfaceName: utils.ToCamelCase(name + "Service"),
destPath: fmt.Sprintf(viper.GetString("gk_http_path_format"), utils.ToLowerSnakeCase(name)),
Expand Down Expand Up @@ -566,9 +603,14 @@ func (g *generateHTTPTransportBase) Generate() (err error) {
)
}
}

body := append([]jen.Code{
jen.Id("m").Op(":=").Qual("net/http", "NewServeMux").Call()}, handles...)
var body []jen.Code
if g.gorillaMux {
body = append([]jen.Code{
jen.Id("m").Op(":=").Qual("github.com/gorilla/mux", "NewRouter").Call()}, handles...)
} else {
body = append([]jen.Code{
jen.Id("m").Op(":=").Qual("net/http", "NewServeMux").Call()}, handles...)
}
body = append(body, jen.Return(jen.Id("m")))
g.code.appendFunction(
"NewHTTPHandler",
Expand Down
29 changes: 17 additions & 12 deletions generator/add_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
func TestNewGenerateTransport(t *testing.T) {
setDefaults()
type args struct {
name string
transport string
methods []string
name string
gorillaMux bool
transport string
methods []string
}
tests := []struct {
name string
Expand All @@ -25,9 +26,10 @@ func TestNewGenerateTransport(t *testing.T) {
{
name: "Test if generator is created properly",
args: args{
name: "test",
transport: "http",
methods: []string{},
name: "test",
gorillaMux: false,
transport: "http",
methods: []string{},
},
want: &GenerateTransport{
BaseGenerator: func() BaseGenerator {
Expand All @@ -48,9 +50,10 @@ func TestNewGenerateTransport(t *testing.T) {
{
name: "Test if bad name format still works",
args: args{
name: "t es_t",
transport: "http",
methods: []string{},
name: "t es_t",
gorillaMux: false,
transport: "http",
methods: []string{},
},
want: &GenerateTransport{
BaseGenerator: func() BaseGenerator {
Expand All @@ -71,7 +74,7 @@ func TestNewGenerateTransport(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewGenerateTransport(tt.args.name, tt.args.transport, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
if got := NewGenerateTransport(tt.args.name, tt.args.gorillaMux, tt.args.transport, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewGenerateTransport() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -707,6 +710,7 @@ func TestGenerateTransport_removeUnwantedMethods(t *testing.T) {
func Test_newGenerateHTTPTransport(t *testing.T) {
type args struct {
name string
gorillaMux bool
serviceInterface parser.Interface
methods []string
}
Expand All @@ -719,7 +723,7 @@ func Test_newGenerateHTTPTransport(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newGenerateHTTPTransport(tt.args.name, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
if got := newGenerateHTTPTransport(tt.args.name, tt.args.gorillaMux, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGenerateHTTPTransport() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -768,6 +772,7 @@ func Test_generateHTTPTransport_Generate(t *testing.T) {
func Test_newGenerateHTTPTransportBase(t *testing.T) {
type args struct {
name string
gorillaMux bool
serviceInterface parser.Interface
methods []string
allMethods []parser.Method
Expand All @@ -781,7 +786,7 @@ func Test_newGenerateHTTPTransportBase(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newGenerateHTTPTransportBase(tt.args.name, tt.args.serviceInterface, tt.args.methods, tt.args.allMethods); !reflect.DeepEqual(got, tt.want) {
if got := newGenerateHTTPTransportBase(tt.args.name, tt.args.gorillaMux, tt.args.serviceInterface, tt.args.methods, tt.args.allMethods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGenerateHTTPTransportBase() = %v, want %v", got, tt.want)
}
})
Expand Down
Loading

0 comments on commit 28af64a

Please sign in to comment.