url-shortener-echo-templ-htmx

command module
v0.0.0-...-ca0e800 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 9 Imported by: 0

README ยถ

Shortify


A Full Stack Url Shortener App using Golang's Echo framework + </>HTMX & Templ.

GitHub License Static Badge


๐Ÿค” What Stack have we used?

In the implementation of this application we have used the following technologies:


[!NOTE] 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. Likewise, the _hyperscript library allows you to add some dynamic features to the frontend in a very easy way.

[!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.


๐Ÿ–ผ๏ธ Screenshots:

Home & Login Pages with success alert:

ย ย ย ย 

ย ย ย ย 

ย ย ย ย 

Centralized HTTP error handling:

๐Ÿ“ฆ Project structure

- assets
  |- css
  |- img
- database
- encryption
- internal
  |- api
    |- dto
  |- entity
  |- model
  |- repository
  |- service
- postgresdb_init
- settings
- timezone_conversion
- views
  |- auth_views
  |- components
  |- errors_pages
  |- layout
  |- links_views


[!NOTE] In this application, we have tried to apply a Clean Architecture pattern. 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). Related to the latter, we have used a dependency injector (Fx from uber-go), which helps to remove the global state in the application and add new components and have them instantly accessible across the application.

[!TIP] When applying this approach in a real-life application, as with most things, taking the layering approach to an extreme level can have a negative effect. Ask yourself if what you are doing actually helps make the code understandable or simply spreads the application logic across many files and makes the overall structure difficult to see.

๐Ÿ‘จโ€๐Ÿš€ Getting Started

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

Since we use the PostgreSQL database from a Docker container, it is necessary to have the latter also installed and execute this command in the project folder:

$ docker compose up -d

These other commands will also be useful to manage the database from its container:

$ docker container start shortify-db # start container
$ docker container stop shortify-db # stop container
$ docker exec -it shortify-db psql -U admin -W shortify_db # (pass: admin) access the database

Download the necessary dependencies:

$ go mod tidy

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

[!TIP] In order to have autocompletion and syntax highlighting in VS Code for the Teml 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 will allow us to monitor changes to the .templ files and compile them as we save them. Review the documentation on Templ installation and support for your IDE.


Happy coding ๐Ÿ˜€!!

Documentation ยถ

The Go Gopher

There is no documentation for this package.

Directories ยถ

Path Synopsis
internal
api
views
auth_views
templ: version: 0.2.476
templ: version: 0.2.476
components
templ: version: 0.2.476
templ: version: 0.2.476
errors_pages
templ: version: 0.2.476
templ: version: 0.2.476
layout
templ: version: 0.2.476
templ: version: 0.2.476
links_views
templ: version: 0.2.476
templ: version: 0.2.476

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL