Skip to content

Commit

Permalink
Version 2.08
Browse files Browse the repository at this point in the history
1. (new) WebApiUtils v2.08
2. (new) MiniORMUtils v1.13
3. (new) WebAPIController v1.07
4. (change) Use DbFile instead of DbName for SQLite in config.ini
5. (change) Move code from ConfigureDatabase sub to Initialize sub in DatabaseConfiguration class
6. (remove) Code for Firebird, PostgreSQL and MS SQL
  • Loading branch information
pyhoon committed Jul 6, 2024
1 parent 82d4ab6 commit 36306ae
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 113 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# webapi-2-b4j

Version: 2.07
Version: 2.08

Create Web API Server using B4X project template

Expand All @@ -14,7 +14,7 @@ Create Web API Server using B4X project template
---

## Template:
- Web API Server (2.07).b4xtemplate
- Web API Server (2.08).b4xtemplate

## Depends on:
- [WebApiUtils.b4xlib](https://github.com/pyhoon/WebApiUtils-B4J)
Expand Down
44 changes: 19 additions & 25 deletions WebAPI2/$APPNAME$.b4j
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
AppType=StandardJava
Build1=Default,b4j.webapi,SQLite,hu2_acceptall
Build2=Firebird,b4j.webapi,Firebird,server,hu2_acceptall
Build3=MSSQL,b4j.webapi,MSSQL,server,hu2_acceptall
Build4=MySQL,b4j.webapi,MySQL,server,hu2_acceptall
Build5=PostgreSQL,b4j.webapi,PostgreSQL,server,hu2_acceptall
Build1=Default,b4j.webapi,SQLite,server,hu2_acceptall
Build2=MySQL,b4j.webapi,MySQL,server,hu2_acceptall
File1=category.html
File2=config.example
File3=index.html
Expand Down Expand Up @@ -33,7 +30,7 @@ NumberOfModules=11
Version=10
@EndOfDesignText@
' Product: Web API Server
' Version: 2.07
' Version: 2.08
' Description: Server application project template
' Developer: Aeric Poon (https://www.b4x.com/android/forum/members/aeric.74499/)
' License: Open Source
Expand All @@ -49,16 +46,10 @@ Version=10
#IgnoreWarnings: 32
#End Region

#If SQLite
#AdditionalJar: sqlite-jdbc-3.39.3.0
#Else If MySQL
#If MySQL
#AdditionalJar: mysql-connector-java-8.0.30
#Else If MSSQL
#AdditionalJar: jtds-1.3.1
#Else If PostgreSQL
#AdditionalJar: postgresql-42.6.0
#Else If Firebird
#AdditionalJar: jaybird-5.0.0.java11
#Else
#AdditionalJar: sqlite-jdbc-3.39.3.0
#End If

Sub Process_Globals
Expand All @@ -73,8 +64,8 @@ Sub Process_Globals
Public SESSIONS_ENABLED As Boolean
Public COOKIES_ENABLED As Boolean
Public COOKIES_EXPIRATION As Long
Public Const VERSION As String = "2.07"
'Public Const PREFIX As String = "WebAPI_v207_" ' use for cookies and sessions
Public Const VERSION As String = "2.08"
'Public Const PREFIX As String = "WebAPI_v208_" ' use for cookies and sessions
End Sub

' <link>Open in browser|http://localhost:19800/web</link>
Expand All @@ -85,9 +76,13 @@ Sub AppStart (Args() As String)

Dim sc As ServerConfiguration
sc.Initialize
'sc.SimpleResponse.Enable = True
sc.EnableCORS = True
#If DEBUG
sc.EnableHelp = True
#End If
#If Release
sc.EnableSSL = True
#End If
sc.Finalize
sc.ShowWelcomeText

Expand All @@ -98,13 +93,12 @@ Sub AppStart (Args() As String)
Server.Start

' Show Controllers in Documentation
' Tips: You can unlist an API using #Hide tag
Controllers.Initialize
Controllers.Add("CategoriesController")
Controllers.Add("ProductsController")
Controllers.Add("FindController")

PRINT_FULL_REQUEST_URL = True
If sc.EnableHelp Then
Controllers.Initialize
Controllers.Add("CategoriesController")
Controllers.Add("ProductsController")
Controllers.Add("FindController")
End If
StartMessageLoop
End Sub

Expand Down
4 changes: 2 additions & 2 deletions WebAPI2/$APPNAME$.b4j.meta
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ ModuleClosedNodes6=
ModuleClosedNodes7=
ModuleClosedNodes8=
ModuleClosedNodes9=
NavigationStack=Main
NavigationStack=Main,AppStart,40,1
SelectedBuild=0
VisibleModules=
VisibleModules=
11 changes: 11 additions & 0 deletions WebAPI2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ignore files
*.b4j.meta

# ignore ALL files in these directories
AutoBackups/
Objects/b4xlibs/
Objects/bin/
Objects/logs/
Objects/shell/
Objects/src/
Objects/*.db
2 changes: 1 addition & 1 deletion WebAPI2/ApiHandler.bas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Type=Class
Version=9.8
@EndOfDesignText@
' Api Handler class
' Version 2.07
' Version 2.08
Sub Class_Globals
Private Request As ServletRequest
Private Response As ServletResponse
Expand Down
109 changes: 59 additions & 50 deletions WebAPI2/DatabaseConfiguration.bas
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,79 @@ Type=Class
Version=10
@EndOfDesignText@
' Database Configuration class
' Version 2.07
' Version 2.08
Sub Class_Globals

Private Conn As Conn
Private const COLOR_RED As Int = -65536 'ignore
Private const COLOR_GREEN As Int = -16711936 'ignore
Private const COLOR_BLUE As Int = -16776961 'ignore
Private const COLOR_MAGENTA As Int = -65281 'ignore
End Sub

Public Sub Initialize
If Not(File.Exists(File.DirApp, "config.ini")) Then
File.Copy(File.DirAssets, "config.example", File.DirApp, "config.ini")
End If
Dim Config As Map = WebApiUtils.ReadMapFile(File.DirApp, "config.ini")

Conn.Initialize
Conn.DBDir = Config.GetDefault("DbDir", "")
Conn.DBFile = Config.GetDefault("DbFile", "")
Conn.DBType = Config.GetDefault("DbType", "")
Conn.DBHost = Config.GetDefault("DbHost", "")
Conn.DBPort = Config.GetDefault("DbPort", "")
Conn.DBName = Config.GetDefault("DbName", "")
Conn.DriverClass = Config.GetDefault("DriverClass", "")
Conn.JdbcUrl = Config.GetDefault("JdbcUrl", "")
Conn.User = Config.GetDefault("User", "")
Conn.Password = Config.GetDefault("Password", "")
Conn.MaxPoolSize = Config.GetDefault("MaxPoolSize", 0)
End Sub

' Configure Database (create if not exist)
Public Sub ConfigureDatabase
Dim Conn As Conn
Conn.Initialize
Conn.DBType = Main.Config.GetDefault("DbType", "")
Conn.DBName = Main.Config.GetDefault("DbName", "")
Conn.DBHost = Main.Config.GetDefault("DbHost", "")
Conn.DBPort = Main.Config.GetDefault("DbPort", "")
Conn.DBDir = Main.Config.GetDefault("DbDir", "")
Conn.DriverClass = Main.Config.GetDefault("DriverClass", "")
Conn.JdbcUrl = Main.Config.GetDefault("JdbcUrl", "")
Conn.User = Main.Config.GetDefault("User", "")
Conn.Password = Main.Config.GetDefault("Password", "")
Conn.MaxPoolSize = Main.Config.GetDefault("MaxPoolSize", 0)
Try
'Log("Checking database...")
Select Conn.DBType.ToUpperCase
Case "SQLITE"
#If SQLite
Dim DBFound As Boolean
If File.Exists(Conn.DBDir, Conn.DBName) Then
DBFound = True
End If
Main.DBConnector.Initialize(Conn)
#Else
LogColor($"Build configuration does not match ${Conn.DBType} database settings!"$, -65536)
LogColor($"Application is terminated."$, -65536)
ExitApplication
Return
#End If
Case "MYSQL"
#If MYSQL
Main.DBConnector.Initialize(Conn)
Wait For (Main.DBConnector.DBExist) Complete (DBFound As Boolean)
#Else
LogColor($"Build configuration does not match ${Conn.DBType}!"$, -65536)
LogColor($"Application is terminated."$, -65536)
ExitApplication
Return
#End If
Case Else
Main.DBConnector.Initialize(Conn)
Wait For (Main.DBConnector.DBExist) Complete (DBFound As Boolean)
End Select
Log("Checking database...")
#If MySQL
Dim DBType As String = "MySQL"
#Else
Dim DBType As String = "SQLite"
#End If

If Conn.DBType.EqualsIgnoreCase(DBType) Then
Main.DBConnector.Initialize(Conn)
#If MySQL
Wait For (Main.DBConnector.DBExist2) Complete (DBFound As Boolean)
#Else
Dim DBFound As Boolean = Main.DBConnector.DBExist
#End If
Else
ShowBuildConfigurationNotMatch
Return
End If

If DBFound Then
Log($"${Conn.DBType} database found!"$)
LogColor($"${Conn.DBType} database found!"$, COLOR_BLUE)
Else
LogColor($"${Conn.DBType} database not found!"$, -65536)
LogColor($"${Conn.DBType} database not found!"$, COLOR_RED)
CreateDatabase
End If
Catch
LogError(LastException.Message)
LogColor("Error checking database!", -65536)
LogColor("Error checking database!", COLOR_RED)
Log("Application is terminated.")
ExitApplication
End Try
End Sub

Private Sub CreateDatabase
Log("Creating database...")
Wait For (Main.DBConnector.DBCreate) Complete (Success As Boolean)
Select Conn.DBType.ToUpperCase
Case "MYSQL"
Wait For (Main.DBConnector.DBCreateMySQL) Complete (Success As Boolean)
Case "SQLITE"
Wait For (Main.DBConnector.DBCreateSQLite) Complete (Success As Boolean)
End Select
If Not(Success) Then
Log("Database creation failed!")
Return
Expand All @@ -96,7 +99,7 @@ Private Sub CreateDatabase
MDB.Insert
MDB.Parameters = Array("Toys")
MDB.Insert

MDB.Table = "tbl_products"
MDB.Columns.Add(MDB.CreateORMColumn2(CreateMap("Name": "category_id", "Type": MDB.INTEGER)))
MDB.Columns.Add(MDB.CreateORMColumn2(CreateMap("Name": "product_code", "Length": "12")))
Expand All @@ -115,9 +118,15 @@ Private Sub CreateDatabase

Wait For (MDB.ExecuteBatch) Complete (Success As Boolean)
If Success Then
Log("Database is created successfully!")
LogColor("Database is created successfully!", COLOR_BLUE)
Else
Log("Database creation failed!")
LogColor("Database creation failed!", COLOR_RED)
End If
MDB.Close
End Sub

Private Sub ShowBuildConfigurationNotMatch
LogColor($"Build configuration does not match ${Conn.DBType}!"$, COLOR_RED)
LogColor($"Application is terminated."$, COLOR_RED)
ExitApplication
End Sub
40 changes: 18 additions & 22 deletions WebAPI2/Files/config.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Product: Web API Server
# Version: 2.07
# Version: 2.08
# Developer: Aeric Poon
# License: Donationware
# Paypal: https://paypal.me/aeric80/
Expand Down Expand Up @@ -34,34 +34,32 @@ SSLPort=0

# Windows development server
# Leave SSL_KEYSTORE_DIR as empty (Objects folder or File.DirApp)
; SSL_KEYSTORE_DIR=
; SSL_KEYSTORE_FILE=keystore
; SSL_KEYSTORE_PASSWORD=xxxxxxxxx
#SSL_KEYSTORE_DIR=
#SSL_KEYSTORE_FILE=keystore
#SSL_KEYSTORE_PASSWORD=xxxxxxxxx

# Ubuntu Linux VPS
; SSL_KEYSTORE_DIR=/etc/letsencrypt/live/mydomain.com
; SSL_KEYSTORE_FILE=keystore.jks
; SSL_KEYSTORE_PASSWORD=xxxxxxxxx
#SSL_KEYSTORE_DIR=/etc/letsencrypt/live/mydomain.com
#SSL_KEYSTORE_FILE=keystore.jks
#SSL_KEYSTORE_PASSWORD=xxxxxxxxx

# Define Email Settings
; SMTP_SERVER=xxxxxxxxx
; SMTP_USERNAME=xxxxxxxxx
; SMTP_PASSWORD=xxxxxxxxx
; SMTP_PORT=465
; SMTP_USESSL=True
; HTML_BODY=True
; ADMIN_EMAIL=xxxxxxxxx
#SMTP_SERVER=xxxxxxxxx
#SMTP_USERNAME=xxxxxxxxx
#SMTP_PASSWORD=xxxxxxxxx
#SMTP_PORT=465
#SMTP_USESSL=True
#HTML_BODY=True
#ADMIN_EMAIL=xxxxxxxxx

# DATABASE CONFIGURATION

## SQLite configuration:
DbType=SQLite
DbName=webapi2.db
DbDir=
DbFile=webapi2.db
#DbDir=C:/B4X/Development/Web API Server 2.08/Objects
DriverClass=org.sqlite.JDBC
JdbcUrl=jdbc:sqlite:{DbDir}/{DbName}
## For custom directory
## Use DbDir=C:/B4X/Development/Web API Server 2.06/Objects
JdbcUrl=jdbc:sqlite:{DbDir}/{DbFile}

## MySQL configuration:
#DbType=MySQL
Expand All @@ -72,6 +70,4 @@ JdbcUrl=jdbc:sqlite:{DbDir}/{DbName}
#JdbcUrl=jdbc:mysql://{DbHost}:{DbPort}/{DbName}?characterEncoding=utf8&useSSL=false
#User=root
#Password=password
#MaxPoolSize=100
## For mysql-connector-java-5.1.49 (deprecated)
## Use DriverClass=com.mysql.jdbc.Driver
#MaxPoolSize=100
Loading

0 comments on commit 36306ae

Please sign in to comment.