Share My Creation [Project Template] Web API Server

Version: 1.16
Description: Create Web API Server with CRUD functionalities


1681305482137.png
1681305567463.png
1681305621295.png


Template:
  • Web API Server (1.16).b4xtemplate
Depends on following libraries:
  • ByteConverter
  • JavaObject
  • jServer
  • Json
  • jSQL
Features:
  1. CRUD based - RESTful Web API (GET, POST, PUT, DELETE)
  2. Front-end (HTML, CSS, JS, Bootstrap)
  3. Support MySQL and SQLite database (Can be modified for MS SQL or SQL Express)
  4. Separate SQL queries file (queries-mysql.ini and queries-sqlite.ini)
  5. Sample database auto-generated for first run (Category and Product tables with dummy data)
  6. Versioning (using ROOT_PATH in config.ini, set as "/" if you don't want versioning)
  7. Auto generated documentation with API test.
How to use:
  1. Copy the "Web API Server (1.16).b4xtemplate" file into B4J Additional Libraries folder.
  2. Open B4J and create a new project with "Web API Server" template.
  3. Give your project any name you like, for e.g. WebAPI
  4. Run the project in Debug mode.
    You will see something like this in the Logs:
    B4X:
    Web API Server (version = 1.16) is running on port 19800
    Open the following URL from your web browser
    http://127.0.0.1:19800/v1/
  5. Copy the URL showed in Logs and open it using your web browser.
  6. To connect to MySQL server, go to Objects folder and open "config.ini".
  7. Comment the SQLite section and uncomment the MySQL section.
  8. Edit the root password at line #45 (second last line). Save the file.
    1681306109357.png
  9. In B4J project, comment the line
    '#AdditionalJar: sqlite-jdbc-3.39.3.0
    and uncomment the line
    #AdditionalJar: mysql-connector-java-8.0.30.
    Make sure you are using the correct version of connector.
    B4X:
    #Region AdditionalJar
    ' MySQL connector
    #AdditionalJar: mysql-connector-java-8.0.30
    ' SQLite connector
    '#AdditionalJar: sqlite-jdbc-3.39.3.0
    #End Region
  10. Follow step #3 above.
Client Template:
  • Web API Client (1.05).b4xtemplate
B4A
. .
B4i
. .
B4J


How to use:
  1. Copy the "Web API Client (1.05).b4xtemplate" file into B4X Additional Libraries folder.
  2. Open B4A (B4J or B4i) and create a new project with "Web API Client (1.05)" template.
  3. Edit the URL with your Web API Server host or IP
    B4X:
    Private URL As String = "http://192.168.50.42:17178/v1/" ' Change to your Web API Server URL
  4. Run in debug.
Video:

Comments: I try to make this template as compact as possible (remove user table and email function) but it is still big. If you don't like some features, just remove them. 😄

12/04/2023
- Web API Server - v1.16
- Similar to Web API Template 2.0, it is now less confusing by using #Desc and #Path for documentation
- Web API Client - v1.0.5 (compatible with Web API Server v1.16)

03/10/2022
- Web API Server - v1.15
- JS: Fixed bug point to wrong id for Category dropdown in Edit dialog
- JS: Remove comma in thousand for price textbox in Edit dialog
- Add a Home button in Category page

01/10/2022
- Web API Server - v1.14
- Manage Category in new page
- Dynamic populate Category dropdown in add/edit product dialog
- Improve JS scripts
- SQLite version point to sqlite-jdbc-3.39.3.0

27/08/2022
- Web API Client - v1.0.4 (compatible with Web API Server v1.13)

25/08/2022
- Web API Server - v1.13
- Home page with Add, Edit and Delete Product (using Bootstrap modal and jQuery Validate)
- Help or API documentation page improvements
- Many variables and SQL commands are renamed!
- SQLite version point to sqlite-jdbc-3.39.2.0
- Separate Category and Product handlers
- FindHandler added for more search endpoints
- Endpoints structure changed!
- Warning: This version is not compatible with Web API Client v1.0.3 and below

28/05/2022
- Web API - v1.12 (fixed a bug in HelpHandler.bas when reading commented line)
- Updated to use jServer v4.0
- Web API Client - v1.0.3 (Converted to B4XTemplate)

26/01/2022
- Create Publish.zip using #CustomBuildAction
- Create Publish.zip using Comment Link

19/10/2021 - v1.11
- Better use of Connection Pool for MySQL (do not close pool)
- Change DataUtils module to DataConnector class
- Clean up some redundant codes

08/10/2021
- WebAPI Client - v1.02
- - Some bugs fix and UI improvements

24/9/2021 - v1.10
- Auto generated documentation/help file based on handler classes (Click on the Question mark icon on top right corner to access the help file)

06/09/2021
- WebAPI Client - v1.01
- - Some bugs fix and UI improvements

04/09/2021 - v1.09
- Fixed PutProductByCategoryAndId to update Product as different Category.
- WebAPI Client (B4X) - v1.00

02/09/2021 - v1.08
- Added ConfigureCORS for ajax request.

01/09/2021 - v1.07
- Changes in Utility module (ReturnSuccess and ReturnSuccess2) require Status Code.
- Changes in ProductHandler (PostCategory and PostProductByCategory) return new record as result with Status Code 201.

27/08/2021 - v1.06
- Support for SQLite database

About auto generated help file (version 1.10+)
https://www.b4x.com/android/forum/threads/web-api-template.133764/post-851114
 

Attachments

  • ajax-test.zip
    30.2 KB · Views: 1,123
  • Web API Server (1.16).b4xtemplate
    481.3 KB · Views: 549
  • Web API Client (1.05).b4xtemplate
    443.9 KB · Views: 553
Last edited:

Pendrush

Well-Known Member
Licensed User
Longtime User
As general rule you should open connection for client, when server return result connection should be closed.
Better way is to use some MySQL (MariaDB) connection pool. As open/close connection is expensive. When client request connection "hot" (pool) connection is given to the client. On connection close, connection is returned to the pool. HikariCP is my preffered.

Pool performance chart:

HikariCP-bench-2.6.0.png
 

ilan

Expert
Licensed User
Longtime User
As general rule you should open connection for client, when server return result connection should be closed.
Better way is to use some MySQL (MariaDB) connection pool. As open/close connection is expensive. When client request connection "hot" (pool) connection is given to the client. On connection close, connection is returned to the pool. HikariCP is my preffered.

Pool performance chart:

HikariCP-bench-2.6.0.png

sorry, but I have not understood your answer. only 1 client is making the connection. all other clients are just making get/post requests to the webapp.

anyway, I have opened a new thread since I don't want to make a big mess in @aeric's thread.

 

aeric

Expert
Licensed User
Longtime User
You can do an experiment if you don’t believe you need to close the connection. 😊

I share my experience here. I have jAdmin hosted in my VPS and I didn’t close the connection correctly last time. I also didn’t set the MaxPoolConnection. Then after a few minutes, the app cannot be access. The server has reached the default maximum number of connection.

REST API is designed to be stateless. It is different from web socket. However you still can keep user session in server.

Let say I am making a Get request from Chrome, which involve a database query, a connection is made. Then I make second call to the same request, a new connection is made. So after a while, many connections are made. I may close the browser and open again multiple times and each time, a new connection is created. The server doesn’t do garbage collection and not smart enough to close the connections for me automatically. It doesn’t know if you don’t implicitly close it. This is my understanding.
 

juangtju

Member
hi excuse me , i use your example for create a web app with connect to a mysql database , but i dont know how is the procedure to consult a new database for save record in other database with other field on it created in a form, you can help me with that??
 

aeric

Expert
Licensed User
Longtime User
hi excuse me , i use your example for create a web app with connect to a mysql database , but i dont know how is the procedure to consult a new database for save record in other database with other field on it created in a form, you can help me with that??
I construct the database table using script.
You can check queries_mysql.ini inside the Objects folder. The MySQL connection username and password are set in config.ini
You can also design your own database using other tools such as Heidi, PHPMyAdmin or Adminer.
 

tchart

Well-Known Member
Licensed User
Longtime User
As general rule you should open connection for client, when server return result connection should be closed.
Better way is to use some MySQL (MariaDB) connection pool. As open/close connection is expensive. When client request connection "hot" (pool) connection is given to the client. On connection close, connection is returned to the pool. HikariCP is my preffered.

Pool performance chart:

HikariCP-bench-2.6.0.png

Slightly off topic but I recently did some tests with HikariCP and I got worse performance compared to having a single DB connection (defined in Main). I did some further reading on this and having a connection pool offers no advantage for a local database.

This was doing the same stress test I've documented on here before. Using the H2 database I get about 4000-5000 requests per second. When run with HikariCP it goes down to 1500-3500 requests per second (I noticed it did fluctuate a lot).
 

aeric

Expert
Licensed User
Longtime User
Slightly off topic but I recently did some tests with HikariCP and I got worse performance compared to having a single DB connection (defined in Main). I did some further reading on this and having a connection pool offers no advantage for a local database.

This was doing the same stress test I've documented on here before. Using the H2 database I get about 4000-5000 requests per second. When run with HikariCP it goes down to 1500-3500 requests per second (I noticed it did fluctuate a lot).
Can I summarize that it is sufficient to use the normal connection pool as Erel’s example?
 

tchart

Well-Known Member
Licensed User
Longtime User
Can I summarize that it is sufficient to use the normal connection pool as Erel’s example?

I would say yes, if you are using a TCP based database connection (eg MySQL, Postgres etc) then it makes sense to use a pool.

If you are using a file based database then don't use a pool.
 

juangtju

Member
I construct the database table using script.
You can check queries_mysql.ini inside the Objects folder. The MySQL connection username and password are set in config.ini
You can also design your own database using other tools such as Heidi, PHPMyAdmin or Adminer.
yes i know that , the problem is not the creation of a new table , i have the existing table with i workaround , but who i dont know is how to call these tables in a b4xtables example you have posted here , that is my principal problem
 

aeric

Expert
Licensed User
Longtime User
yes i know that , the problem is not the creation of a new table , i have the existing table with i workaround , but who i dont know is how to call these tables in a b4xtables example you have posted here , that is my principal problem
You can post a new question. I think you can’t send me a personal message since you are new here.
 

aeric

Expert
Licensed User
Longtime User

aeric

Expert
Licensed User
Longtime User
Top