mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
update simple-json-server example to use encoding/json
This commit is contained in:
parent
0c1ce96dae
commit
9d3ebbea7d
@ -32,8 +32,6 @@ This project requires these tools to be installed globally.
|
||||
|
||||
* wrangler
|
||||
* tinygo
|
||||
* [easyjson](https://github.com/mailru/easyjson)
|
||||
- `go install github.com/mailru/easyjson/...@latest`
|
||||
|
||||
### Commands
|
||||
|
||||
|
@ -1,151 +0,0 @@
|
||||
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
json "encoding/json"
|
||||
easyjson "github.com/mailru/easyjson"
|
||||
jlexer "github.com/mailru/easyjson/jlexer"
|
||||
jwriter "github.com/mailru/easyjson/jwriter"
|
||||
)
|
||||
|
||||
// suppress unused package warning
|
||||
var (
|
||||
_ *json.RawMessage
|
||||
_ *jlexer.Lexer
|
||||
_ *jwriter.Writer
|
||||
_ easyjson.Marshaler
|
||||
)
|
||||
|
||||
func easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(in *jlexer.Lexer, out *HelloResponse) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeFieldName(false)
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "message":
|
||||
out.Message = string(in.String())
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(out *jwriter.Writer, in HelloResponse) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
{
|
||||
const prefix string = ",\"message\":"
|
||||
out.RawString(prefix[1:])
|
||||
out.String(string(in.Message))
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
|
||||
// MarshalJSON supports json.Marshaler interface
|
||||
func (v HelloResponse) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(&w, v)
|
||||
return w.Buffer.BuildBytes(), w.Error
|
||||
}
|
||||
|
||||
// MarshalEasyJSON supports easyjson.Marshaler interface
|
||||
func (v HelloResponse) MarshalEasyJSON(w *jwriter.Writer) {
|
||||
easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(w, v)
|
||||
}
|
||||
|
||||
// UnmarshalJSON supports json.Unmarshaler interface
|
||||
func (v *HelloResponse) UnmarshalJSON(data []byte) error {
|
||||
r := jlexer.Lexer{Data: data}
|
||||
easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(&r, v)
|
||||
return r.Error()
|
||||
}
|
||||
|
||||
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
|
||||
func (v *HelloResponse) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||||
easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(l, v)
|
||||
}
|
||||
func easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(in *jlexer.Lexer, out *HelloRequest) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeFieldName(false)
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "name":
|
||||
out.Name = string(in.String())
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(out *jwriter.Writer, in HelloRequest) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
{
|
||||
const prefix string = ",\"name\":"
|
||||
out.RawString(prefix[1:])
|
||||
out.String(string(in.Name))
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
|
||||
// MarshalJSON supports json.Marshaler interface
|
||||
func (v HelloRequest) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(&w, v)
|
||||
return w.Buffer.BuildBytes(), w.Error
|
||||
}
|
||||
|
||||
// MarshalEasyJSON supports easyjson.Marshaler interface
|
||||
func (v HelloRequest) MarshalEasyJSON(w *jwriter.Writer) {
|
||||
easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(w, v)
|
||||
}
|
||||
|
||||
// UnmarshalJSON supports json.Unmarshaler interface
|
||||
func (v *HelloRequest) UnmarshalJSON(data []byte) error {
|
||||
r := jlexer.Lexer{Data: data}
|
||||
easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(&r, v)
|
||||
return r.Error()
|
||||
}
|
||||
|
||||
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
|
||||
func (v *HelloRequest) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||||
easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(l, v)
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
//go:generate easyjson .
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
)
|
||||
|
||||
//easyjson:json
|
||||
type HelloRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
//easyjson:json
|
||||
type HelloResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func HelloHandler(w http.ResponseWriter, req *http.Request) {
|
||||
var helloReq HelloRequest
|
||||
if err := easyjson.UnmarshalFromReader(req.Body, &helloReq); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte("request format is invalid"))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
msg := fmt.Sprintf("Hello, %s!", helloReq.Name)
|
||||
helloRes := HelloResponse{Message: msg}
|
||||
|
||||
if _, err := easyjson.MarshalToWriter(&helloRes, w); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to encode response: %w\n", err)
|
||||
}
|
||||
}
|
@ -2,11 +2,6 @@ module github.com/syumai/workers/_examples/simple-json-server
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/mailru/easyjson v0.7.7
|
||||
github.com/syumai/workers v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
require github.com/syumai/workers v0.0.0-00010101000000-000000000000
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
||||
|
||||
require github.com/josharian/intern v1.0.0 // indirect
|
||||
|
@ -1,4 +0,0 @@
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
@ -1,13 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
"github.com/syumai/workers/examples/simple-json-server/app"
|
||||
)
|
||||
|
||||
type HelloRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type HelloResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func HelloHandler(w http.ResponseWriter, req *http.Request) {
|
||||
var helloReq HelloRequest
|
||||
if err := json.NewDecoder(req.Body).Decode(&helloReq); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte("request format is invalid"))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
msg := fmt.Sprintf("Hello, %s!", helloReq.Name)
|
||||
|
||||
helloRes := HelloResponse{Message: msg}
|
||||
if err := json.NewEncoder(w).Encode(helloRes); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to encode response: %w\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/hello", app.HelloHandler)
|
||||
http.HandleFunc("/hello", HelloHandler)
|
||||
workers.Serve(nil) // use http.DefaultServeMux
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user