This is mere a fun project of utter laziness. Essentially the app directly reads the mysql database table and it can then be requested through http. Nothing fancy. Just so the backend ready without writing any structs for each table, then go back lazing around wholeheartedly.
Make sure docker and docker-compose are installed.
To start development, clone and navigate to the cloned folder.
$ git clone https://github.com/diditaditya/lazer
$ cd lazerCreate .env file with the following variables
DB=mysql
DB_HOST=mysql8
DB_USER=root
DB_PASSWORD=thedbpassword
DB_NAME=thedbnameNote that the DB_HOST is the mysql container name in the docker-compose.yml in this case mysql8. The DB_USER is root because nothing is set for the container. If you want to change it please check here which also contains other settings for the mysql container.
Start the docker
$ docker-compose up -dThis will create 2 containers, the go and the mysql.
In the mysql container, a database named the same as DB_NAME in .env file will be created. You can get inside the mysql container and create some tables.
$ docker exec -it mysql8 bash
$ mysql -pPassword will be asked, enter the one you set in DB_PASSWORD. And then you can start creating some tables.
The app will not automatically run. You must start it manually from inside the go container.
$ docker exec -it go bash
$ cd src/lazer
$ go run main.goYou can access the app from your browser or whatever at localhost:3500. If you want to change the port just change the docker-compose.yml which maps default gin port at 8080 to 3500. Or just experiment with traefik to access it from your localhost with subdomain, which is cool and helpful, so you don't need to care about clashing ports.
Currently the features are developed really slowly. No test whatsoever, hence expect bugs, and when you find them please do fix them and create pull request 😃
The features are:
- Automatically creates routes based on tables in the database. If you go to your browser and hit the root, you'll get the list of the tables which can be used as the routes or paths. For example if you have tables named
usersandbooks, they can be accessed throughhttp://localhost:3500/usersandhttp://localhost:3500/booksassuming default setting for host and port. - The routes can filter the data by using querystring. For example if your
bookstable has columnsid,title,author, andyear, you can filter the books by author like sohttp://localhost:3500/books?author=nobodyor maybe the year alsohttp://localhost:3500/books?author=nobody&year=2000. If you want multiple authors, use it likeauthor=nobody&author=somebody. Please note this feature is still very basic and simplistic, manage your expectation. - The routes can also be used to
POSTdata to create new entry. For example if you want to add new book,POSTtohttp://localhost:3500/bookswith JSON body like{"title": "abc", "author": 'me', "year": 1890}, which returns nothing 😆 - The
PUTverb can be used as well to update an entry by using/:tablename/:primaykeyroute and body in JSON just like the aforementionedPOSTexample. - By using
DELETEverb, you can delete entries from a table. The deletion will be bulk delete with parameters in the querystring almost similar to filter.