http/rest implementation of redis in golang
Here is an implementation of redis with a little twist.
redis is an in-memory database so It's so fast and its usage is for caching (most of the time) but it can be used for other purposes.
I also used some middlewares:
- logger (for logging user data, end points, time and time taken to serve the request)
- CORS
- bodyLimit (to stop user from adding big data for values in database)
- rateLimit (for stopping high loads on short time)
there are 9 end points that you can use to work with database :
func NewRouter(e *echo.Echo) *echo.Echo {
e.POST("/set", cmd.Set)
e.GET("/get", cmd.Get) // get value from key by query parameters {"name" for database name} {"key" for data key}
e.DELETE("/del", cmd.Del)
e.POST("/use", cmd.UseDB)
e.POST("/keyregex", cmd.KeyRegex)
e.GET("/listdata", cmd.ListData) // get list of values from database name by query parameters {"name" for database name}
e.GET("/listdb", cmd.ListDBs) // get list of databases from storage name by query parameters {"name" for storage name}
e.POST("/save", cmd.Save)
e.POST("/load", cmd.Load)
return e
}/setyou can set a new (key, value) pair by database name, key and value./getyou can get data by passing database name and key./delyou can delete data by passing database name and key./useyou can set a pointer to passed database and if that doesn't exist will create a new one./keyregexyou can find a key by passing database name and expected piece of data./listdatait will list all data from a database by passing database name./listdbit will show all databases from a container by passing the name of the container./savewill save all data from a database to passed file path in body of request./loadwill load all data to a database from passed file path in body of request.
if you pass a new database name the server will create a new database and set that as reference.
also, if you pass an empty string as a database name it will use last used database as the reference and do the operation on that database.
its so simple just run these commands, (I also used echo in project you have to first install that by go get github.com/labstack/echo/v4)
go build
./go-rediscurl -X POST \
http://localhost:8080/set \
-H 'cache-control: no-cache' \
-H 'postman-token: 7c9578fb-d646-a7a0-ebe5-831956b9a224' \
-d '{
"DbName": "default",
"Key": "giga",
"Value": "chad"
}'