kit icon indicating copy to clipboard operation
kit copied to clipboard

Add full example for kitgen

Open Raffo opened this issue 8 years ago • 18 comments

Hi, I was having a look at the PR for kitgen, great stuff! I noticed though that there is no full example of how to use it to build a real service. Do you think it would make sense to add? Additionally, I wonder why the makeEndpoint functions are not exported... I would use them from the main.go in a similar fashion like that:

package main

import (
	"net/http"
	"os"

	kit_http "test/foo/http"

	"test/foo/endpoints"
	"test/foo/service"

	"github.com/go-kit/kit/log"
)

func main() {
	logger := log.NewLogfmtLogger(os.Stderr)

	var svc service.Service
	svc = service.ServiceStruct{}

	e := endpoints.Endpoints{}

	e.Concat = endpoints.MakeConcatEndpoint(svc)
	e.Sum = endpoints.MakeSumEndpoint(svc)

	handler := kit_http.NewHTTPHandler(e)

	http.Handle("/", handler)
	logger.Log("msg", "HTTP", "addr", ":8080")
	logger.Log("err", http.ListenAndServe(":8080", nil))
}

I'm probably wrong and there is another way to do that, and I think that a full example with a main.go would help to understand that.

Raffo avatar Dec 10 '17 06:12 Raffo

@peterbourgon I see that you labeled the issue, if I can get an answer to my questions in the original comment, I would be interested in contributing the examples personally ^_^

Raffo avatar Dec 11 '17 14:12 Raffo

I noticed though that there is no full example of how to use it to build a real service. Do you think it would make sense to add?

This would be a great addition to the README in that directory. PR happily accepted.

wonder why the makeEndpoint functions are not exported... I would use them from the main.go in a similar fashion like that:

Yes, they should be exported! An oversight. PR happily accepted.

peterbourgon avatar Dec 11 '17 19:12 peterbourgon

I created a PR to fix the unexported functions ( https://github.com/go-kit/kit/pull/641 ), will do the docs as well later on.

Raffo avatar Dec 12 '17 09:12 Raffo

As someone new to go-kit and having watched a few talks that keep referencing "kitgen", I'd be happy to beta-test any examples/tutorials!

retnuh avatar Dec 19 '17 14:12 retnuh

@nyarly I was trying to understand the way to work with kitgen and it looks like the service in the service folder is always overwritten. Can you confirm that this behaviour is intended? Also, I could not find the *.tmpl files mentioned in ast_templates.go, did you forget to commit them or is there something I am missing?

@retnuh I have a PR in progress, once I figure some more details above I'll be happy to provide a PR and have you as beta tester :-)

Raffo avatar Dec 19 '17 16:12 Raffo

Quick ack that I saw this. I'll dig in when I get I moment

On Tue, Dec 19, 2017 at 8:15 AM Raffaele Di Fazio [email protected] wrote:

@nyarly https://github.com/nyarly I was trying to understand the way to work with kitgen and it looks like the service in the service folder is always overwritten. Can you confirm that this behaviour is intended? Also, I could not find the *.tmpl files mentioned in ast_templates.go, did you forget to commit them or is there something I am missing?

@retnuh https://github.com/retnuh I have a PR in progress, once I figure some more details above I'll be happy to provide a PR and have you as beta tester :-)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/go-kit/kit/issues/639#issuecomment-352807089, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHyPC_DbqJgKttT8ehLwqWZ-N7jzka8ks5tB-EwgaJpZM4Q8ZGW .

nyarly avatar Dec 19 '17 21:12 nyarly

@nyarly gentle "up" :-)

Raffo avatar Jan 10 '18 14:01 Raffo

@Raffo Sorry for the late response - the holiday season laid me low. I'm still not able to speak!

The service folder getting clobbered is by design. The idea is that the service sub-package "belongs" to kitgen - it should be simple enough to use the types and functions defined there without using the code itself. In my experience, generated code has to be treated specially, since you'll from time to time need to update its source and re-generate.

The *.tmpl reference is a documentation bug. The generator, here: https://github.com/go-kit/kit/blob/53f10af5d5c7375d4655a3d6852457ed17ab5cc7/cmd/kitgen/main.go#L18 actually pulls in all the files under templates - "all the files" at the moment means full.go.

A tutorial README would be really useful, agreed. I'll add it to my TODO list :) In the meantime, the tests for kitgen are very much of the "smoke test" variety. The good thing about that is that, essentially, there's a set of inputs and outputs under testdata that can serve as examples of how kitgen works.

nyarly avatar Jan 16 '18 19:01 nyarly

@nyarly Thanks for the answer, it explains most of my questions.

There is still something I don't understand though: what do you mean by "it should be simple enough to use the types and functions defined there without using the code itself" ? Where am I supposed to write the business logic of the program if not in the package service as those will contain the structs and the methods that are supposed to contain the business logic?

Currently this file will be overwritten every time so it is not really an option to write the logic there which makes the generated file pretty useless to my eyes, but I'm sure there's something trivial I am missing. I am just trying to understand what could be a workflow around kitgen that is actually usable.

Raffo avatar Jan 18 '18 14:01 Raffo

@Raffo I assume he means to use those files as a basis for your own code. I for one only use kitgen for initial boilerplate code and then don't ever generate it again (instead making manual changes).

abemedia avatar Feb 08 '18 18:02 abemedia

@Raffo Were you able to write-up the documentation for kitgen?

sudo-suhas avatar Jun 15 '18 07:06 sudo-suhas

Well yes, I have something lying around, but I was not able to have a workflow that I actually enjoyed. AFAIK kitgen did not evolve from where it was which didn't really make it super useful for me if not for bootstrapping initial simple files.

Raffo avatar Jun 15 '18 21:06 Raffo

Bootstrapping is good. Bootstrapping increases adoption.

ermik avatar Aug 03 '18 13:08 ermik

@ermik Thanks for providing a README for kitgen! Can I contribute to the README some additional information on how to get a full service up and running?

Raffo avatar Sep 02 '18 19:09 Raffo

Yeah, the PR is merged so you can pull the current repo to continue improving it. Feel free to @ mention me when you create a pull request with your changes if you'd like feedback.

ermik avatar Sep 03 '18 22:09 ermik

Is it intentional that kitgen will create a struct with the same name as the interface that is defined? In the go-kit examples, the services struct is not exported but the service interface is. Whereas I currently I just get redeclared errors.

I'm happy to open a new ticket if this is an issue but if it is by design might be worth noting in the full example. 😄

xlvdk avatar Oct 18 '18 20:10 xlvdk

@NeuralSandwich not sure I'm entirely following; could you create an issue with example code?

ermik avatar Oct 19 '18 13:10 ermik

@ermik I've created #784. I hope that is clearer explanation 😄

xlvdk avatar Oct 30 '18 21:10 xlvdk