Share My Creation [Project Template] [Web] EndsMeet (beta)



Here is a beta version of EndsMeet, an MVC framework to develop web application in B4X using B4J IDE.
It depends on jServer and JSON libraries.
It can be easily extended with template library such as Velocity or HTMX Freemarker and database library like MiniORM and so on.
Note: This is a B4J project template. Add the attached template to B4J additional library folder and create a new project from the IDE.
 

Attachments

  • EndsMeet (0.06).b4xtemplate
    8.4 KB · Views: 186
  • EndsMeet (0.18).b4xtemplate
    16.8 KB · Views: 52
Last edited:

aeric

Expert
Licensed User
Longtime User
A short tutorial to add a new api.

Modify to the following code.

HelloController:
Public Sub RouteApi
    Select Request.Method
        Case "GET"
            Dim RequestMap As Map = EndsMeetUtils.GetRequestElements(Request.RequestURI, Main.BaseElements)
            If RequestMap.ContainsKey("First") Then
                Dim Person As String = RequestMap.Get("First")
                GetGreet(Person)
            Else
                GetJSON
            End If
        Case Else
            Log("Unsupported method: " & Request.Method)
            EndsMeetUtils.ReturnMethodNotAllow(HRM, Response)
    End Select
End Sub

Private Sub GetGreet (Name As String)
    #region Documentation
    ' #Version = v1
    ' #Desc = Return greeting message
    #End region
    Model.ReturnGreetingApi(Name)
End Sub

HelloModel:
Public Sub ReturnGreetingApi (Person As String)
    Dim m1 As Map
    m1.Initialize
    m1.Put("greeting", $"Merry Xmas, ${Person}!"$)
    HRM.ResponseObject = m1
    EndsMeetUtils.ReturnHttpResponse(HRM, Response)
End Sub

IndexView:
Public Sub Render
    Dim strHtml As String = $"<center>
        <a href="${Main.ROOT_PATH}hello">Go to Hello page</a><br/>
        <p><a href="${Main.ROOT_PATH}api/v1/hello">Get Hello API</a></p>
        <p><a href="${Main.ROOT_PATH}api/v1/hello/B4X">Get Greeting API</a></p>
        </center>"$
    EndsMeetUtils.ReturnHtml(strHtml, Response)
End Sub

Note: This example doesn't check for api version.

 

aeric

Expert
Licensed User
Longtime User
Another example using HTMX.

IndexView:
Sub Class_Globals
    Private Response As ServletResponse
End Sub

Public Sub Initialize (req As ServletRequest, resp As ServletResponse)
    Response = resp
End Sub

Public Sub Render
    Dim strHtml As String = $"<center>   
        <button hx-put="/demo/messages" style="padding: 10px">
            Put To Messages
        </button>
        <p>
        <input type="text" name="q"
        hx-get="/demo/trigger_delay"
        hx-trigger="keyup changed delay:500ms"
        hx-target="#search-results"
        placeholder="Search...">
        <div id="search-results" style="margin-top: 60px">No result</div>
        </p>
        </center>
        
        <script src="https://unpkg.com/htmx.org@2.0.0" integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" crossorigin="anonymous"></script>
        "$
    EndsMeetUtils.ReturnHtml(strHtml, Response)
End Sub

WebHandler:
Sub Class_Globals
    Private Request As ServletRequest
    Private Response As ServletResponse
End Sub

Public Sub Initialize

End Sub

Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    ProcessRequest
End Sub

Private Sub ProcessRequest
    Log($"[${Request.Method}] ${Request.RequestURI}"$)   
    If EndsMeetUtils.isWebRequest(Request.RequestURI, Main.BaseElements) = False Then
        Response.SendError(400, "Bad Request")
        Return
    End If
    
    Dim RequestMap As Map = EndsMeetUtils.GetRequestElements(Request.RequestURI, Main.BaseElements)
    Dim Path As String = RequestMap.Get("Path")
    If Path.EqualsIgnoreCase(Main.BaseElements.Path.Element) = False Then
        Response.SendError(400, "Bad Request")
        Return
    End If
    
    Select RequestMap.Size
        Case 1 ' root
            Log(RequestMap)
        Case 2 ' path (demo)
            Dim Index As IndexView
            Index.Initialize(Request, Response)
            Index.Render
            Return
        Case Else
            Dim Controller As String = RequestMap.Get("Controller")
            Select Controller
                Case "hello"
                    Dim Hello As HelloController
                    Hello.Initialize(Request, Response)
                    Hello.RouteWeb
                    Return
                Case "messages"
                    DateTime.DateFormat = "dd/MM/yyyy HH:mm:ssa"
                    Response.Write(DateTime.Date(DateTime.Now))
                    Return
                Case "trigger_delay"
                    Dim q As String = Request.GetParameter("q")
                    If q = "" Then
                        Response.Write("No result")
                    Else
                        Response.Write(q)
                    End If
                    Return
            End Select
    End Select
    Response.SendError(400, "Bad Request")
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…