mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-08-03 13:12:16 +02:00
initial import
This commit is contained in:
1
microservices/jsonservice/GET-request.http
Normal file
1
microservices/jsonservice/GET-request.http
Normal file
@ -0,0 +1 @@
|
||||
GET http://localhost:8080/customer HTTP/1.1
|
8
microservices/jsonservice/POST-request.http
Normal file
8
microservices/jsonservice/POST-request.http
Normal file
@ -0,0 +1,8 @@
|
||||
POST http://localhost:8080/customer HTTP/1.1
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"ID": 2,
|
||||
"Lastname": "Wurst"
|
||||
}
|
||||
|
5
microservices/jsonservice/go.mod
Normal file
5
microservices/jsonservice/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module golang.source-fellows.com/samples/jsonservice
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/gorilla/mux v1.7.4
|
2
microservices/jsonservice/go.sum
Normal file
2
microservices/jsonservice/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
37
microservices/jsonservice/main.go
Normal file
37
microservices/jsonservice/main.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type Customer struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
Firstname string `json:"first,omitempty"`
|
||||
Lastname string `json:"last,omitempty"`
|
||||
}
|
||||
|
||||
var customer *Customer
|
||||
|
||||
func myGetFunc(rw http.ResponseWriter, rq *http.Request) {
|
||||
bites, _ := json.Marshal(customer)
|
||||
rw.Header().Add("Content-Type", "application/json")
|
||||
rw.Write(bites)
|
||||
}
|
||||
|
||||
func myPostFunc(rw http.ResponseWriter, rq *http.Request) {
|
||||
body, _ := ioutil.ReadAll(rq.Body)
|
||||
json.Unmarshal(body, customer)
|
||||
rw.WriteHeader(204)
|
||||
}
|
||||
|
||||
func main() {
|
||||
customer = &Customer{ID: 1, Firstname: "Peter"}
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/customer", myGetFunc).Methods("GET")
|
||||
r.HandleFunc("/customer", myPostFunc).Methods("POST")
|
||||
http.ListenAndServe(":8080", r)
|
||||
}
|
Reference in New Issue
Block a user