TTime/backend
2024-03-20 16:46:48 +01:00
..
docs tyding up and tryinng to get tokens in docs to work 2024-03-19 00:33:38 +01:00
internal ChangeUserName function in db.go fixed, corresponding test added 2024-03-20 16:46:48 +01:00
go.mod Experimental port to CGO-free modernc.org/sqlite, no more WSL 2024-03-15 11:02:33 +01:00
go.sum Experimental port to CGO-free modernc.org/sqlite, no more WSL 2024-03-15 11:02:33 +01:00
main.go Reworking endpoint getWeeklyReportsUser 2024-03-20 16:05:50 +01:00
Makefile Golds automatic documentation generator 2024-03-19 20:21:38 +01:00
README.md Implementation details in backend readme 2024-02-13 09:54:47 +01:00
tygo.yaml Tygo for go->typescript type generation 2024-03-16 17:43:38 +01:00

The Backend

This is the root of the backend. It contains the main server and the database logic. The structure might look confusing at first, given that go conventions (which I followed to the best of my ability) uses a rather goofy structure with the cmd and internal directories.

Golang does not support classes, but it does support structs. These are practically synonymous, with the important distinction being that structs does not support traditional inheritance. Instead, go uses interfaces to achieve polymorphism. This is a rather interesting way of doing things, and it's a bit of a learning curve if you're coming from a language like C++ or Java. Having a good understanding of Composition over Inheritance will help you understand the go way of doing things.

An interface is functionally similar to an interface in Java, or an abstract class in C++. It simply describes how an object can behave.

An important note is that go, unlike C, handles its own dependencies. A simple go get command will fetch all the dependencies for you. The go.mod file is the equivalent of a package.json file in Node.js, if you're familiar with that.

To run the backend, you can use the go run command. For example, to run the server, you can use go run cmd/main.go. This will start the server on its default port (8080, at the time of writing).

Structure

There are several moving parts to the backend. The core web server is the heart of the backend. It glues together the database and serves the routes. Some of the routes are whats called API endpoints. These are the routes that the frontend will use to communicate with the backend. The 'root' route of the web server is the one that serves the frontend. Its the route that is called when visiting 'yourwebsite.com' or 'localhost:8080' in your browser.

Implementation

Currently the server uses the standard library 'net/http' to serve the routes. Other considerations include Gin and Fiber. For the database interactions, we use sqlx, which is a superset of the standard library's 'database/sql' package. It provides a more ergonomic way of interacting with the database, while still being pure sql. There are other popular libraries like GORM and XORM, (ORM as in Object Relational Mapping), but those abstract away the sql, which does not fit the project's goals.