Giter Site home page Giter Site logo

JSON response about chi HOT 7 CLOSED

hmgle avatar hmgle commented on May 5, 2024 1
JSON response

from chi.

Comments (7)

pkieltyka avatar pkieltyka commented on May 5, 2024 10

hi @hmgle I'd prefer to keep chi as just a router. If you need a responder, I suggest to use https://github.com/unrolled/render which works just fine with chi.

your code will look like:

import renderPkg "github.com/unrolled/render"

var render *render.Render

func init() {
  render = renderPkg.New() // pass options if you want
}

func x() http.Handler {
  r := chi.New()
  r.Get("/", handler)
}

func handler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  // processing..
  render.JSON(w, 200, responseObj)
}

another suggestion, is wrap the unrolled/render package by making your own "render" package that is a part of your project, and set defaults there so its setup to use across packages.

from chi.

hugows avatar hugows commented on May 5, 2024 3

So I'd like to understand if you prefer something like:

"render.RenderList(w, r, NewArticleListResponse(articles))

instead of something like unrolled/render does, based on interface{}...

Thanks for chi btw! :)

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

The goals of chi are to stay minimal, allow full control without limitation of possibility or productivity. I guess the gap right now is that chi doesn't provide every library out of the box, you're expected to make your own choices for something like the responder. I will update the example to show how to wrap your own render responder.

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

@hmgle here you go https://github.com/pressly/chi/blob/master/_examples/rest/main.go
and
https://github.com/pressly/chi/blob/master/_examples/rest/render/render.go

from chi.

hmgle avatar hmgle commented on May 5, 2024

Got it~ It looks elegant.

from chi.

hugows avatar hugows commented on May 5, 2024

Was the example linked in https://github.com/pressly/chi/blob/master/_examples/rest/render/render.go integrated into rest/main.go?

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

@hugows yes exactly. Its now at: https://github.com/go-chi/chi/blob/master/_examples/rest/main.go

from chi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.