#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Public Server As ABMServer 'requires 1.23+
Public MyTheme As ABMTheme
Public ABM As ABMaterial 'ignore
' JKS variables
Public JKS As ABKeystoreSSL
Public JKSReloadTimer As Timer
Public JKSName As String = "keystore"
Public JKSStorePassword As String = "01234"
Public JKSManagerPassword As String = "56789"
End Sub
Sub AppStart (Args() As String)
' must be the first line in AppStart. DO NOT DELETE OR CHANGE!
' ------------------------------------------------------------
ABM.SessionCacheControlV3 = "ABMCacheV3"
' ------------------------------------------------------------
Dim DonatorKey As String = ""
Server.Initialize("", DonatorKey, "template") ' Application = ' the handler that will be used in the url e.g. http://localhost:51042/template
' some parameters
Server.Port = 51042
Server.PortSSL = 51043
...
' create the pages
Dim myPage As ABMPageTemplate
myPage.Initialize
' add the pages to the app
Server.AddPage(myPage.page)
' do the same for your own pages: dim, initialize and Server.AddPage
' ...
' start the server
If Server.PortSSL <> 0 Then
' make the jks file (if needed)
MakeJKS
If File.Exists(File.DirApp, JKSName & ".jks") Then
Log("Starting server in HTTPS mode...")
Server.StartServerHTTP2(JKSName & ".jks", JKSStorePassword, JKSManagerPassword)
' set a timer for a day
JKSReloadTimer.Initialize("JKSReloadTimer", 24*60*60*1000)
JKSReloadTimer.Enabled = True
Else
' start it without https
LogError("Failed to open or create " & JKSName & ".jks" & ". Starting server in HTTP mode...")
Server.StartServer
End If
Else
Server.StartServer
End If
' redirect the output of the logs to a file if in release mode
Server.RedirectOutput(File.DirApp, "logs.txt")
StartMessageLoop
End Sub
' creates a certificate with Let's Encrypt
Sub MakeJKS() As Boolean
Dim Result As Boolean
' needs to be the 'entry point' in your webserver (NOT your B4X app). In my case it was var/www/html and not var/www/)
' MUST be accessible on port 80
JKS.Initialize("/var/www/html/")
Dim domains As List
domains.Initialize
domains.Add("yourdomain.com")
' when developing, I check here if it is my local PC. If so I do not use the production server of Let's Encrypt
If ABM.GetMyIP = "192.168.86.150" Then
Result = JKS.GenerateJKS(domains, File.DirApp, JKSName, JKSStorePassword, JKSManagerPassword, False, "K:\KeyVault", False)
If File.Exists(File.DirApp, JKSName & ".jks") = False Then
' let's try to make a self signed certificate for development
LogError("Failed to open or create " & JKSName & ".jks with Let's Encrypt. Let's try to make a self signed one...")
Result = JKS.GenerateSelfSignedJKS(File.DirApp, JKSName, JKSStorePassword, JKSManagerPassword, "K:\KeyVault")
End If
Else
Result = JKS.GenerateJKS(domains, File.DirApp, JKSName, JKSStorePassword, JKSManagerPassword, True, "../KeyVault", False)
End If
Return Result
End Sub
Sub JKSReloadTimer_Tick
If MakeJKS Then
' we created a new one, so reload it in the server
JKS.ReloadJKS(Server.GetSSLConfiguration)
End If
End Sub