Server-side component development in Go program language

An easy way to create a server-side component in any programming language

Benefits of server-side components

By definition, component-based development is an approach to software development that focuses on the design and development of reusable components. The essence of isolated component development is exactly the same as the logic of industrial production based on standardized raw materials and components. Component developers create components that perform general or specific sub-tasks, from which any application can then be assembled. The components can operate in an isolated, sandboxed environment and are often independent of the business logic of the final use. The given component must be able to receive specific parameters, have an expected mode of operation, and its output events are also defined.

All components made in this way are fully tested, documented in a standard way, and can be easily reused in any application. Developer resource management is also an important aspect, because these components can be made in parallel as desired, independently of the specific applications.

The approach first became dominant in client-side application development for technical reasons. However, the advantages of the solution can also be used in server-side programming, and the possibilities of server-side technology offer even additional advantages.

Reducing the complexity and dependencies of program development means a shorter development time and the creation of products of better quality and lower cost.

HTTP request management

Traditional server-side applications send complete HTML pages to browsers for display. Each response from the server replaces the entire screen. However, the components are organized based on a different logic. These are hierarchically organized, logically independent html code parts. Components are able to independently react and process user interactions and notify other processing components of the processing results. The entire page is not replaced or reloaded in the browser, only the required parts of the application.

This is not a problem for client-side components, because the component state data is stored locally and the components are regenerated within the browser. Unfortunately, this principle of operation cannot be implemented with HTTP standard requests at the moment.

Fortunately, there is a great program library that implements exactly the functionality required for component communication. Htmx is small (~14k), dependency-free, browser-oriented javascript library that allows you to access modern browser features directly from HTML, rather than using javascript. Yes, in a funny way, we can currently get to the Javascript-free world with the help of a Javascript library. :-)

The server-side components use only a small part of the possibilities of htmx. With the help of the function library, we are able to send POST requests with any html element, and replace the HTML code fragment returned as a response anywhere on the page without reloading the page.

Server-side component events

The components of the source code for the description are implemented in Go, but the same logic can be implemented in any other server-side programming language. These components are not frameworks, they only use Go’s built-in packages and have no external dependencies. A library of components whose elements can be freely combined with each other and easily developed further. It’s actually just a code implementation proposal that anyone can easily use to create a server-side component in any programming language.

Creating a server-side component

The basis of the components is a data-driven template, which contains the rules of their appearance and behavior. The components use the declarative template from the text/template package in the go standard library. In traditional server-side programming, the same package is the basis for generating the html code displayed on the client side.

The main difference is that templates do not contain the rules of entire pages or certain parts of them, but the generated independent small html code fragments are organized based on a different logic. These hierarchically organized, logically independent html code parts are able to independently react and process user interactions and notify other components of the processing results. React’s JSX or Lit’s declarative template works on the same principle, but the same logic can be implemented in any other server-side programming language’s template package with similar functionality.

All components have their own properties that determine their current state and based on which the component’s Render function generates the HTML code. Each component has a Render function and a few other similar mandatory functions that can be used to safely query and set all properties of the component.

The ClientComponent interface contains the mandatory functions that are available in all components, and each component is also of the ClientComponent type in addition to its own type. The BaseComponent type is likewise a ClientComponent, which, in addition to the mandatory functions of the interface, also contains properties and functions that all components based on it inherit and can use if necessary.

The easiest way to create a new component is to add new properties and functions to the BaseComponent and override the functions of the ClientComponent interface if necessary. The Application component is a top-level element to which all other components belong. This element is completely never replaced, only some of its parts can change. Its task is to load and manage all static elements required for the operation and display of the components, such as style sheets and the htmx package.

Examples and demo application

The logic of the components can be understood most easily from the code of the existing components. The source code repository contains sample code for ~15 server-side components, from the basic input component to the complex component.

The css files of the components are included in the static package. The index.css contains the reference to the style sheets of all components, so it is sufficient to specify this in the Application HeadLink property. The styles of the new components can be specified in additional css files and the styles of the existing components can also be overwritten.

The demo application displays all components with their test data. Applications can store component state in memory, but they can save it anywhere in json format and load it back. The demo application can store session data in memory and as session files. The source code of the example application also contains an example of using a session database (sqlite3, postgres, mysql, mssql). If you want to use a database session, uncomment before importing the database driver you want to use.

The source code of the components and the demo application can be found here: Nervatura server-side Go components