
How I write Go HTTP services after seven years - matryer
https://medium.com/@matryer/how-i-write-go-http-services-after-seven-years-37c208122831
======
maartenh
Nice article.

I've been looking into inbound content marketing lately, and this really is a
prefect example of that!

------
dalu
I've been doing it a bit differently. Lately I've written 2 generators. Gen1
works using the Go language a := Api("shop")
a.Root("/home/dalu/go/src/dalu/shop") mProduct := Model("Product")
mProduct.Field("Id",Identifier,true) // (true=optional)
mProduct.Field("Name",String,false) mProduct.Field("User",ReferenceId, false)
// etc a.Model(mProduct) a.Render()

this creates a "cobra init; cobra add api" app, \- handlers \- route
definitions \- data repositories \- the models \- angular service files

[https://github.com/dalu/reforum](https://github.com/dalu/reforum)

next step would be to extend it with a full featured adminUI in angular using
some kind of datagrid. I was never big on testing so I'm not generating tests.

version 2 of this generator you write your models and they get inspected and
from those models essentially the same thing is generated, only in v1 I wrote
it all with fmt.Sprintf and now I'm working on the text templates. I can't
really say which one is better. In v1, adding a new type means rummaging
through all the instances where a typecheck is done. In v2, it's the same but
in text templates. Both feel like they're sub-optimal solutions.

The layout of the handler in v1 is type Handler struct { userRepo productRepo
} etc, and a single repo for every model. So if I need data from 2 or more
repos it's done in the handler.

But in v2 I have 1 handler that gets passed 1 repo. And the repo is
initialized with the various options for dbname dbcollection. Yes it's all
MongoDB.

In v1 I have an unintuitive way to query the db. Let's say there's a
ProductRepo, I have a ProductParams type when I had to set true to "Name" if I
wanted to query the name attrib of that model, then set NameQuery to be a new
instance and if I wanted a equal query "name = 'hello'" I'd set the
NameQuery.Type = "eq" and NameQuery.Query = "hello".

In v2 I got rid of the "Name = true" thing. And I'm json, then base64 encoding
the query for GET queries. This way I can have a more convenient/intuitive and
slim Params structure.

I'm currently thinking about doing this, but everything should be server side
rendered, aka no angular. But I'm not sure... it's the old problem I faced as
a PHP dev. How to create re-usable components, programmatically.

You need to construct your data model and from this model you should be able
to deal with ToMany, ManyTo, ToOne, OneTo and the rendering of those Forms and
input validation and you need lazy loading for related models.

Qor does something like this in their adminui building blocks. And I've seen
something similar in PHP's Symfony.

