Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
feat: support custom marshal and content type
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 28, 2019
1 parent dbaa1d5 commit 3c8a305
Show file tree
Hide file tree
Showing 6 changed files with 1,510 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://img.shields.io/travis/vicanso/elton-responder.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-responder)

Responder middleware for elton, it can convert `Context.Body` to json data. By this middleware, it's more simple for successful response.
Responder middleware for elton, it can convert `Context.Body` to json data. By this middleware, it's more simple for successful response. More response type can be supported through custom marshal function and content type.


```go
Expand Down
82 changes: 82 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"encoding/json"
"io/ioutil"
"time"

"github.com/vicanso/elton"
responder "github.com/vicanso/elton-responder"
)

type AutoGenerated struct {
Users []struct {
ID int `json:"id"`
Username string `json:"username"`
AvatarTemplate string `json:"avatar_template"`
} `json:"users"`
Topics struct {
CanCreateTopic bool `json:"can_create_topic"`
MoreTopicsURL string `json:"more_topics_url"`
Draft interface{} `json:"draft"`
DraftKey string `json:"draft_key"`
DraftSequence interface{} `json:"draft_sequence"`
PerPage int `json:"per_page"`
Topics []struct {
ID int `json:"id"`
Title string `json:"title"`
FancyTitle string `json:"fancy_title"`
Slug string `json:"slug"`
PostsCount int `json:"posts_count"`
ReplyCount int `json:"reply_count"`
HighestPostNumber int `json:"highest_post_number"`
ImageURL string `json:"image_url"`
CreatedAt time.Time `json:"created_at"`
LastPostedAt time.Time `json:"last_posted_at"`
Bumped bool `json:"bumped"`
BumpedAt time.Time `json:"bumped_at"`
Unseen bool `json:"unseen"`
Pinned bool `json:"pinned"`
Unpinned interface{} `json:"unpinned"`
Excerpt string `json:"excerpt,omitempty"`
Visible bool `json:"visible"`
Closed bool `json:"closed"`
Archived bool `json:"archived"`
Bookmarked interface{} `json:"bookmarked"`
Liked interface{} `json:"liked"`
Views int `json:"views"`
LikeCount int `json:"like_count"`
HasSummary bool `json:"has_summary"`
Archetype string `json:"archetype"`
LastPosterUsername string `json:"last_poster_username"`
CategoryID int `json:"category_id"`
PinnedGlobally bool `json:"pinned_globally"`
Posters []struct {
Extras string `json:"extras"`
Description string `json:"description"`
UserID int `json:"user_id"`
} `json:"posters"`
} `json:"topics"`
} `json:"topics"`
}

func main() {
d := elton.New()

buf, _ := ioutil.ReadFile("./test.json")
genInfo := &AutoGenerated{}
marshal := func(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
json.Unmarshal(buf, genInfo)

d.Use(responder.New(responder.Config{
Marshal: marshal,
}))
d.GET("/", func(c *elton.Context) (err error) {
c.Body = genInfo
return
})

d.ListenAndServe(":3000")
}
Loading

0 comments on commit 3c8a305

Please sign in to comment.