lunedì 13 marzo 2023

Restful server in GO

Un esempio di server Restful con la libreria  go-restful

Nelle note del codice alcune indicazioni su come interagire con il server che si apre su porta 8080


// Pacchetto per utilizzo di Restfull
package main

// inserisci nuovo utente
// curl -X POST -H "Content-Type: application/json" -d '{"Id": "42", "Name": "Luca Innocenti"}' http://localhost:8080/users/
// update utente
// curl -X PUT -H "Content-Type: application/json" -d '{"Id": "42", "Name": "Luca Innocenti"}' http://localhost:8080/users/
// cancella utente con id 42
// ricerca utente con id 42
// curl -X "GET"  http://localhost:8080/users/42

//curl -vX POST http://server/api/v1/places.json -d @testplace.json

import (
    "fmt"
    "log"
    "net/http"

    restful "github.com/emicklei/go-restful/v3"
)

// User is a struct
type User struct {
    Id, Name string
}

// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
func New() *restful.WebService {
    service := new(restful.WebService)
    service.
        Path("/users").
        Consumes(restful.MIME_XML, restful.MIME_JSON).
        Produces(restful.MIME_XML, restful.MIME_JSON)

    service.Route(service.GET("/{user-id}").To(FindUser))
    service.Route(service.POST("").To(CreateUser))
    service.Route(service.PUT("/{user-id}").To(UpdateUser))
    service.Route(service.DELETE("/{user-id}").To(RemoveUser))

    return service
}

// GET
func FindUser(request *restful.Request, response *restful.Response) {
    id := request.PathParameter("user-id")
    // here you would fetch user from some persistence system
    usr := User{Id: id, Name: "Luca"}
    response.WriteEntity(usr)
}

// PUT /user-id
func UpdateUser(request *restful.Request, response *restful.Response) {
    parametri := User{Id: request.PathParameter("user-id")}
    //fmt.Println("udpdate user")
    fmt.Println("Numero " + parametri.Id + "Nome " + parametri.Name)
    usr := new(User)
    err := request.ReadEntity(&usr)
    // here you would update the user with some persistence system
    if err == nil {
        response.WriteEntity("modificato")
    } else {
        response.WriteError(http.StatusInternalServerError, err)
    }
}

// POST

func CreateUser(request *restful.Request, response *restful.Response) {
    usr := new(User)
    err := request.ReadEntity(&usr)
    // here you would create the user with some persistence system
    if err == nil {
        response.WriteEntity(usr)
    } else {
        response.WriteError(http.StatusInternalServerError, err)
    }
}

// DELETE /user-id

func RemoveUser(request *restful.Request, response *restful.Response) {
    fmt.Println(request.PathParameter("user-id"))
    log.Printf("DELETE")
    // here you would delete the user from some persistence system
}

func main() {
    restful.Add(New())
    log.Fatal(http.ListenAndServe(":8080", nil))
}






Nessun commento:

Posta un commento

ESP32-2432S028R e LVGL

La scheda ESP32-2432S028R monta un Esp Dev Module con uno schermo TFT a driver ILI9341 di 320x240 pixels 16 bit colore.Il sito di riferiment...