Wednesday, August 12, 2020

Math Test Index

Here are the links to the math tests. 

Simple 

Basic


Math Test: Multiply

40 Multiply Questions

Should fit nicely into A4 paper.


See the Pen 40 Multiple Tests by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Minus

40 Minus Questions

Should fit nicely into A4 paper.

 

See the Pen 40 Minus Tests by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Addition

40 Addition Questions

Should fit nicely into A4 paper.


See the Pen 40 Plus Tests by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Simple Division

 40 Simple Division Questions


See the Pen Simple Devision by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Simple Multiply

 40 Simple Multiply Questions


See the Pen Simple Multiply by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Simple Minus

 40 Simple Minus Questions


See the Pen Simple Minus by DevilKazuya99 (@devilkazuya99) on CodePen.

Math Test: Simple Addition

40 Simple Addition Questions

See the Pen Simple Plus by DevilKazuya99 (@devilkazuya99) on CodePen.

Tuesday, August 11, 2020

Math Test Generator 1

I created this snippet to generate math questions for my kids. Click the [Print] button to print the questions and answers. It should fit nicely in 2 A4 pages. Refresh the page to regenerate a new set of Q/A.


See the Pen More 2 Steps Plus Multiply by DevilKazuya99 (@devilkazuya99) on CodePen.

Thursday, April 2, 2020

Quarkus REST Service integrate with Firestore

Recently I got a request to access data from Google Cloud Firestore. so I'm gonna give Quarkus a try because it is so badass fast.

First I bootstrap my project according to the guides here.

mvn io.quarkus:quarkus-maven-plugin:1.3.0.Final:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=rest-json-quickstart \
    -DclassName="org.acme.rest.json.FruitResource" \
    -Dpath="/fruits" \
    -Dextensions="resteasy-jackson"
cd rest-json-quickstart

Now I got my FruitResource.java, will start to make changes.


My plan is simple. I had 1 GET endpoint which will call a service function and fetch data from Firestore. Then 1 POST endpoint to call a service function to write data into Firestore.

Too easy, right? Normally to integrate with Firestore you just need to add the "firebase-admin" library into your maven pom.xml.
However, my goal, in the end, is to package the application into a native binary. Including an external library as above will make Graalvm complain. So I need to go for plan B --> Firestore REST API.
The design looks a bit weird but it is just for proof of concept, please bear with me.

Google Cloud Firestore itself already comes with REST endpoint. Read here for more info.

To access the REST endpoint, I need to generate and sign a JWT token to be used in the request header.

Quarkus guide saves the day again. See Quarkus - Using JWT RBAC for example. I uncluded quarkus-smallrye-jwt library into my project.


Then I copy the TokenUtils.java code into my project, made some small modifications in order to generate a JWT token that can be used by Cloud Firestore REST endpoint.

Looking back at Cloud Firestore REST API documentation, I am using Google Identity OAuth2 Token to authenticate my request. According to the Google OAuth2 guide, the token needs to look like this:


One more things need to do is go to GCP console > IAM > Service Accounts, create an account for firebase admin (it should exist if you are using Firestore), then create an API Key and download the private key as privateKey.pem. Then generate a publicKey.pem using openssh

  openssl rsa -in privateKey.pem -pubout -out publicKey.pem

Put both files in src/main/resources folder. When calling TokenUtils.generateTokenString(kid, timeClaims); it should give me a signed JWT token.


Now let's look at my service class.

in my firestore I had a collection "stores". So here is my REST client look like:

This code is so beautiful. I like it so much. RestTemplate in S*****g can go to the bin now.

Initially, I assigned the header at the class level with annotation buy it has a problem when compiling into native binary using GraalVM. Therefore I have to inject the token into the header every time I call the function (as function parameter). Calling the REST client will look like this:


To make it all works, I have these 3 files in src/main/resources folder.

-- application.properties

"firestore-api/mp-rest/url" is used as the base path for the REST client in FirestoreService.java.
"mp.jwt.verify.publickey.location" is needed for JWT. 
"quarkus.native.additional-build-args" is used for compiling to native binary. 

-- reflection-config.json (required for compiling to native binary)


-- resources-config.json (files from classpath to include when generating native binary)


Finally, run command
    ./mvnw package -Pnative

to generate the native binary. Here is the screen when running the binary. Please take note, the application starts up in 0.019s. I just broke my personal record of starting up a java web application.



Try it out and have fun.
You can see the code in Github.