Giter VIP home page Giter VIP logo

emarifer / go-echo-templ-htmx Goto Github PK

View Code? Open in Web Editor NEW
198.0 5.0 20.0 342 KB

Go/Echo+Templ+Htmx: Full stack application using Golang's Echo framework & Templ templating language with user session management + CRUD to a SQLite database (To Do List) and HTMX in the frontend

License: MIT License

CSS 2.54% Go 52.40% JavaScript 0.81% templ 44.24%
authentication-middleware daisyui dependency-injection echo-framework error-handling error-middleware go golang hateoas htmx

go-echo-templ-htmx's Introduction

Go/Echo+</>Templ: Full stack Demo App with session authentication (with middleware) and centralized HTTP error handling with Golang's Echo framework, CRUD to a SQLite database (To Do List), use of Templ Template language and HTMx-powered frontend

A full stack application using Golang's Echo framework. Requests to the backend are improved in their "aesthetic" aspect with the use </>htmx (hypermedia only).

GitHub License Static Badge


Note

This application is an clone of this repository of mine (rust-axum-askama-htmx-todoapp), but made in Golang.


πŸ€” Explanation

Demo application to use session authentication (with middleware) and centralized HTTP error handling with the Echo framework.

The architecture follows a typical "onion model" where each layer doesn't know about the layer above it, and each layer is responsible for a specific thing. Although the application is extremely simple, we use this pattern to illustrate its use in more complex applications.

Layering an application in this way can simplify code structure, since the responsibility of each type is clear.

To ensure that each part of the application is initialized with its dependencies, each struct defines a constructor (the New function in this example).


Important

In this application, instead of using the html/template package (native Golang templates), we use the a-h/templ library. This amazing library implements a templating language (very similar to JSX) that compiles to Go code. Templ will allow us to write code almost identical to Go (with expressions, control flow, if/else, for loops, etc.) and have autocompletion thanks to strong typing. This means that errors appear at compile time and any calls to these templates (which are compiled as Go functions) from the handlers side will always require the correct data, minimizing errors and thus increasing the security and speed of our coding.


On the other hand, we use Golang's Echo web framework, which as stated on its website is a "High performance, extensible, minimalist Go web framework".

The use of </>htmx allows behavior similar to that of a SPA, without page reloads when switching from one route to another or when making requests (via AJAX) to the backend.

The styling of the views is achieved through Tailwind CSS and DaisyUI that are obtained from their respective CDNs.

Finally, minimal use of _hyperscript is made to achieve the action of closing the alerts when they are displayed or giving interactivity to the show/hide password button in its corresponding input.

Note

This application is very similar to several of my previous repositories (flask-htmx-todolist, gofiber-templ-htmx, go-echo-templ-project-structure, echo-cookie-session-demo), which are developed with other frameworks (Fiber, Echo, Python's Flask framework…) and template languages (native Golang templates, Python's Jinja2 templates or </>Templ template language). This repository is a compendium of the aforementioned repositories.


πŸ–ΌοΈ Screenshots:

Todo List Page with success alert:


Sign Up Page with error alert:


Task update page:


Centralized HTTP error handling:


πŸ‘¨β€πŸš€ Setup:

Before compiling the view templates, you'll need to regenerate the CSS. First, you need to install the dependencies required by Tailwind CSS and daisyUI (you must have Node.js installed on your system) and then run the regeneration of the main.css file. To do this, apply the following commands:

$ cd tailwind && npm i
$npm run build-css-prod # `npm run watch-css` regenerate the css in watch mode for development

Besides the obvious prerequisite of having Go! on your machine, you must have Air installed for hot reloading when editing code.

Tip

In order to have autocompletion and syntax highlighting in VS Code for the Templ templating language, you will have to install the templ-vscode extension (for vim/nvim install this plugin). To generate the Go code corresponding to these templates you will have to download this executable binary from Github and place it in the PATH of your system. The command:

$ templ generate --watch

Tip

This command allows us to regenerate the .templ templates and, therefore, is necessary to start the application. This will also allow us to monitor changes to the .templ files and compile them as we save them if we make changes to them. Review the documentation on Templ installation and support for your IDE .

Start the app in development mode:

$ air # Ctrl + C to stop the application

Build for production:

$ go build -ldflags="-s -w" -o ./bin/main . # ./bin/main to run the application / Ctrl + C to stop the application

Happy coding πŸ˜€!!

go-echo-templ-htmx's People

Contributors

emarifer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-echo-templ-htmx's Issues

fromProtected, isError

These two variables are static. A large number of simultaneous requests can cause problems. Wouldn't it be better if these variables were stored in echo.Context ? Am I wrong ?

no matching versions for query "latest"

Hey, hello!

After running go mod tidy I got these errors. Is this something related to the new version of GO?

go version go1.22.2 linux/amd64

go: finding module for package github.com/emarifer/go-echo-templ-htmx/views/todo_views
go: finding module for package github.com/emarifer/go-echo-templ-htmx/views/auth_views
go: finding module for package github.com/emarifer/go-echo-templ-htmx/views/errors_pages
go: github.com/emarifer/go-echo-templ-htmx/handlers imports
        github.com/emarifer/go-echo-templ-htmx/views/auth_views: no matching versions for query "latest"
go: github.com/emarifer/go-echo-templ-htmx/handlers imports
        github.com/emarifer/go-echo-templ-htmx/views/errors_pages: no matching versions for query "latest"
go: github.com/emarifer/go-echo-templ-htmx/handlers imports
        github.com/emarifer/go-echo-templ-htmx/views/todo_views: no matching versions for query "latest"

CGO_ENABLED=0

Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.