Output:
- Backend: Golang +Gin Framework
- Database: sqlite
- Frontend: React (upcomming...)
- Basic CRUD
- Hash Password
- JWT Authentication
- Input Validation
- MVC
- Authorization User
- Middleware Interation
Basic:
- If err == nil, it means: "everything went fine"
- If err != nil, it means: "something went wrong", and you can check err.Error() to see what happened.
- So, nil is just Goโs way of saying โthere was no error.โ You check for non-nil to handle problems.
Event-Booking-app/
โ
โโโ routes/ `[working on routes]`
โ โโโ item-routes.go
โ
โโโ controllers/ `[working on request and response]`
โ โโโ item-controller.go
โ
โโโ models/ `[working on type struct and interface]`
โ โโโ item-model.go
โ
โโโ services/ `[working on database query]`
โ โโโ item-services.go
โ
โโโ config/
โ โโโ db.go
โ
โโโ main.go
โ
โโโ go.mod
go mod init bappa.com/restgo get -u github.com/gin-gonic/gingo get github.com/mattn/go-sqlite3go get github.com/fatih/colorfor color the consolego install github.com/gravityblast/fresh@latestfor auto restartgo get golang.org/x/cryptobcrypt for hash passwordgo get -u github.com/golang-jwt/jwt/v5JWT
- go run . || go run main.go || fresh
freshfor auto restart
โ Events:
-
01-project initialized with server and gin framework
-
go mod init bappa.com/rest -
go get -u github.com/gin-gonic/gin -
02-Add and Test Create and Get event
-
[X]03-Initilazation of SQL Database
-https://github.com/mattn/go-sqlite3
- go get github.com/mattn/go-sqlite3
๐ Database Connection Part
var DB *sql.DB
func InitDB() {
var err error
DB, err = sql.Open("sqlite3", "api.db")
if err != nil {
panic("Could not connect to database.")
}
DB.SetMaxOpenConns(10)
DB.SetMaxIdleConns(5)
createTables()
}- 04-Insert & Read Data from Database
Preparing Statements vs Directly Executing Queries (Prepare() vs Exec()/Query())
We started sending SQL commands to the SQLite database.
And we did this by following different approaches:
-
DB.Exec() (when we created the tables)
-
Prepare() + stmt.Exec() (when we inserted data into the database)
-
DB.Query() (when we fetched data from the database)
Using Prepare() is 100% optional! You could send all your commands directly via Exec() or Query().
The difference between those two methods then just is
- whether you're fetching data from the database
(=> use Query())or your manipulating the database / data in the database(=> use Exec()).
But what's the advantage of using Prepare()?
Prepare() prepares a SQL statement - this can lead to better performance if the same statement is executed multiple times (potentially with different data for its placeholders).
This is only true, if the prepared statement is not closed (stmt.Close()) in between those executions. In that case, there wouldn't be any advantages.
And, indeed, in this application, we are calling stmt.Close() directly after calling stmt.Exec(). So here, it really wouldn't matter which approach you're using.
๐ก But in order to show you the different ways of using the sql package,I decided to also include this preparation approach in this project
-
05-Beautify console and json data ๐
-
go get github.com/fatih/color -
use like this
import ("github.com/fatih/color")
color.Cyan("๐ ๐ Server running at http://localhost:8080")
-
06-Add git ignore files
-
07-update readme
-
[X]08-Get event by Id done
-
[X]09-Refctoring code and route for scalability and maintalibility
-
10-Update route added
โ Users: 13-Commit
- Create routes for the users.
- First create the users table and make a connection with events table.
- Write the route, controller,service,model for signup user.
- 14. Create Hash Password signup user.
- https://pkg.go.dev/golang.org/x/crypto/bcrypt
go get golang.org/x/crypto
- 15. valided user with finished the login functionality
- 16. Generate JWT
- 17. Adding Token Varification [complex part]
โ Middleware : 20-Commit :
- 18. Now implement route protection
- 19. Retrieving & Storing User and Event IDs
- 22. Adding Authorization To Restrict Users From Editing & Deleting
โ Registration Route : 23-Commit :
- 23. Registering Users for events
