
[ad_1]
An accelerated REST API with Microservices Framework

For some time now I have been wanting to check out some of the latest and most famous web frameworks. In this article I want to take a look devX for the following techniques.
Web-frameworks I’d like to see:
Requirements
To make this glimpse possible we need a small application that can be built easily but contains the functionality of a modern web app.
So I chose – who would have thought – a weather app.
The user can get the weather information for the city he searched for.
The data for this will be retrieved from a free third party API using REST.
Same goes for city search – the API will provide for that.
I’m not going to do anything to the received data, but just tunnel the JSON response.
basic architecture

Let’s stick to a simple architecture. We have a backend service. It will communicate with the API of openweathermap, The service will then provide the data to the client using a simple REST API.
Our hypothetical frontend can then simply say GET /weather/:name .
i will use the version 1.16.6.
In the documentation you can find a valuable l . Can getInk With a great tutorial for the Easy API with Jin. That’s exactly what we need!


After installation – don’t forget to set environment variable with GOPATH.

$ mkdir wetterapp-gin
$ go mod init davidminkovski.com/wetterapp-gin
Let’s create and initialize the folder for our Weather app.
I followed the tutorial and made a test API.
For IDE I used visual studio code,
package mainimport (
"github.com/gin-gonic/gin"
"net/http"
)type city struct {
ID string `json:"id"`
Name string `json:"name"`
}var cities = []city{
{ID: "1", Name: "Frankfurt am Main"},
{ID: "2", Name: "Köln"},
}func main() {
router := gin.Default()
router.GET("/cities", getCities)
router.Run("localhost:8080")
}func getCities(c *gin.Context) {
c.IndentedJSON(http.StatusOK, cities)
}
Now there are two commands left to execute.
The first loads the dependencies and the second starts the server.
$ go get .
$ go start .


Ok that was super fast. 30 minutes later And I have my REST API.
Now I want to query the weather API. There Go.
package mainimport (
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"net/http"
)func main() {
router := gin.Default()
router.GET("/weather/:name", getWeatherForCity)
router.Run("localhost:8080")
}func getWeatherForCity(c *gin.Context) {
name := c.Param("name")
key := "MY_API_KEY"res, err := http.Get("http://api.openweathermap.org/data/2.5/weather?q=" + name + "&appid=" + key)
if err != nil {
log.Fatal(err)
}
responseData, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
c.Data(http.StatusOK, "application/json", responseData)}

that was it! I have a simple web api server that I can use to query weather data for city names. I must say that coming from a JS or Java background, it took some time to understand and get used to the new syntax and language features, but wow pace!
rating
- ease of use: 1/3
- Speed: 3/3
- documentation: 2/3
i will use quercus Edition 2.1.0.


First of all Quercus runs on GraalVM and you need JDK for Java.
Then we will need package manager maven or gradle. This takes about 15 minutes—again, don’t forget to set the correct environment variables.
Quercus really shines with this configurator, Similar to other Java frameworks like SpringBoot, you can choose the right package and simply generate the required application. Unfortunately it is not very intuitive to know which is which and for whom. I went with my gut and chose the following:


After a quick download I have my project.
It comes with a basic setup, readme and docker configuration.

The documentation provides a greatArticle For a REST API. In addition you will find Here Enough information to work with other APIs.
First we build a model for our weather data.
// WeatherResponse.java@JsonIgnoreProperties(ignoreUnknown = true)
public class WeatherResponse {public String name;
public Weather alpha2Code;
public Main main;
public Wind wind;
public Coord coord;public static class Weather {
public String main;
public String description;
public String icon;
}
public static class Wind {
public float speed;
}
public static class Main{
public float temp;
public float feels_like;
public float temp_min;
public float temp_max;
public int pressure;
public int humidity;
}
public static class Coord{
public float lon;
public float lat;
}
}
Now we need a service that communicates with third party APIs.
In application,Property We set the url and remove the ssl.
// WeatherService.java@Path("/data/2.5/")
@RegisterRestClient(configKey="weather-api")
public interface WeatherService {@GET
// application.propertiesweather-api/mp-rest/url=http://api.openweathermap.org/ #
@Path("/weather")
Set<WeatherResponse> getByCity(@QueryParam("q") String name, @QueryParam("appid") String appId);
}
weather-api/mp-rest/scope=javax.inject.Singleton #
quarkus.tls.trust-all=true
Let’s go ahead and create a resource that will listen for any incoming requests.
// WeatherResource.java@Path("/weather")
public class WeatherResource {@Inject
@RestClient
WeatherService weatherService;String appId = "MY_API_KEY";
@GET
@Path("/{name}")
@Produces("application/json")
public Set<WeatherResponse> name(@PathParam("name") String name) {
return weatherService.getByCity(name, appId);
}
}
We did it! Coming from a Java background the ease of use is clearly a factor here, but the amount of work one has to do with it compared to other frameworks and languages.
rating
- ease of use: 2/3
- Speed: 1/3
- documentation: 2/3
I hope this article has given you a small glimpse at Gin Gonik and Quercus.
Both frameworks are great and have a great future ahead.
Golang and Java in particular being frameworks I am sure they will continue to evolve as programming languages.
Given the ongoing microservices trend, these frameworks can be a great foundation to start your next microservices project!
[ad_2]
Source link
#Glimpse #Quercus #Gin #Gonik #David #Minkowski #August