FAQ
I am creating a REST API and am noticing that I have a lot of duplicated
code gluing my models into HTTP Handlers. To solve this I am trying to
create a generic HTTP Handler which I can then pass a model into and it
performs the request as expected. Here is a playground example:

https://play.golang.org/p/myQtUVg1iq

When you run it you will see the problem. My return types on the Get
methods do not match up. Is there an idiomatic solution to a problem like
this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at Jan 6, 2015 at 10:27 pm
    Check out github.com/mohamedattahri/rst

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Landon Jones at Jan 7, 2015 at 1:33 am
    I'm not sure how idiomatic the following solution is, but it is a close
    approximation of how I would deal with it.

    https://play.golang.org/p/oE7CIiZCY6
    On Tue, Jan 6, 2015 at 12:59 PM, wrote:

    I am creating a REST API and am noticing that I have a lot of duplicated
    code gluing my models into HTTP Handlers. To solve this I am trying to
    create a generic HTTP Handler which I can then pass a model into and it
    performs the request as expected. Here is a playground example:

    https://play.golang.org/p/myQtUVg1iq

    When you run it you will see the problem. My return types on the Get
    methods do not match up. Is there an idiomatic solution to a problem like
    this?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mitranim Forgo at Jan 7, 2015 at 8:37 am
    I ran into the same problem and came up with this library:

    https://github.com/Mitranim/gotools/tree/master/dsadapter

    It provides generic methods for model types, defines a model lifecycle with
    computed properties, permissions, and validation, and provides additional
    utilities for a RESTful server, like glueing together model types and URL
    resource strings.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mitranim Forgo at Jan 7, 2015 at 8:39 am
    Lack of option to edit posts is quite annoying. Addendum to my post above:
    the library uses the App Engine Datastore, but is generic enough so that
    you can rewrite a few methods and use it with any other database.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mitranim Forgo at Jan 7, 2015 at 9:09 am
    Another addendum to better address the original question (shouldn't have
    been posting 5 minutes after waking up...)

    What you want basically is a way to write a generic handler that uses the
    URL string to get a value of a particular record or collection type
    matching it. The value needs to satisfy some CRUD interface to load data
    into it. After getting hold of a value, you can marshal it as JSON and send
    back.

    As I wrote above, check gotools/dsadapter
    <https://github.com/Mitranim/gotools/tree/master/dsadapter> as a reference
    to how you might implement something like this. I ended up defining a
    shared interface type (Record) for all database models that defines CRUD
    methods for an individual record, and adding generic read/find methods that
    take a record or a slice of records as a value to fill with data. As a
    result, you can pass a value of a concrete type into one of those generic
    methods and it'll have the required data after the operation, so you don't
    have to unwrap it from an interface type. I also added a few utility
    methods for generating model values out of resource strings.

    If you have a middleware framework like Martini, you could break API
    handlers into pieces of middleware and compose your handlers out of them
    like Lego bricks. For example, on my server that uses Martini, dsadapter
    and gotools/context
    <https://github.com/Mitranim/gotools/tree/master/context>, handlers look
    like this:

    // POST new record by resource
    r.Post(":resource",
       // Parse request data into record.
       ParseRecordByResource,
       // Save record to database.
       SaveRecord,
       // Send back as json.
       SendRecord)


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tgrosinger at Jan 7, 2015 at 4:37 pm
    Thank you all for your responses. I am also investigating using `go
    generate` to create the necessary HTTP handlers. Would not be reducing code
    per say, but I wouldn't have to write it at least.
    On Tuesday, January 6, 2015 10:59:42 AM UTC-8, tgros...@extrahop.com wrote:

    I am creating a REST API and am noticing that I have a lot of duplicated
    code gluing my models into HTTP Handlers. To solve this I am trying to
    create a generic HTTP Handler which I can then pass a model into and it
    performs the request as expected. Here is a playground example:

    https://play.golang.org/p/myQtUVg1iq

    When you run it you will see the problem. My return types on the Get
    methods do not match up. Is there an idiomatic solution to a problem like
    this?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Roger peppe at Jan 7, 2015 at 5:16 pm
    This is the approach we've been using. A small helper
    package http://godoc.org/github.com/juju/utils/jsonhttp provides the glue.

    https://play.golang.org/p/NXUuyXWbsz

    This approach also helps to unify error handling across the API.

    Note that the ResponseWriter is hidden so that it's not possible
    to make the mistake of writing some of the response before
    returning the JSON (which will then be ignored).

       cheers,
         rog.
    On 7 January 2015 at 16:36, wrote:

    Thank you all for your responses. I am also investigating using `go
    generate` to create the necessary HTTP handlers. Would not be reducing code
    per say, but I wouldn't have to write it at least.


    On Tuesday, January 6, 2015 10:59:42 AM UTC-8, tgros...@extrahop.com
    wrote:
    I am creating a REST API and am noticing that I have a lot of duplicated
    code gluing my models into HTTP Handlers. To solve this I am trying to
    create a generic HTTP Handler which I can then pass a model into and it
    performs the request as expected. Here is a playground example:

    https://play.golang.org/p/myQtUVg1iq

    When you run it you will see the problem. My return types on the Get
    methods do not match up. Is there an idiomatic solution to a problem like
    this?
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 6, '15 at 6:59p
activeJan 7, '15 at 5:16p
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase