-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from cirnum/feature/server-impl
server support added to add multiple cluster
- Loading branch information
Showing
8 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
_ "github.com/cirnum/strain-hub/server/app/models" | ||
"github.com/cirnum/strain-hub/server/app/utils" | ||
"github.com/google/uuid" | ||
|
||
"github.com/cirnum/strain-hub/server/db" | ||
"github.com/cirnum/strain-hub/server/db/models" | ||
"github.com/cirnum/strain-hub/server/pkg/constants" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// To Get all the Server | ||
func GetAllServer(c *fiber.Ctx) error { | ||
ctx := context.Background() | ||
pagination := utils.GetPagination(c) | ||
listServer, err := db.Provider.ListServer(ctx, &pagination) | ||
|
||
if err != nil { | ||
return utils.ResponseError(c, err, "Failed to add the server.", 0) | ||
} | ||
return utils.ResponseSuccess(c, listServer, "Server added successfully.", 0) | ||
|
||
} | ||
|
||
// To Update server | ||
func UpdateServer(c *fiber.Ctx) error { | ||
ctx := context.Background() | ||
serverPayload := &models.Server{} | ||
|
||
if err := c.BodyParser(serverPayload); err != nil { | ||
return utils.ResponseError(c, err, constants.InvalidBody, fiber.StatusInternalServerError) | ||
} | ||
updatedServer, err := db.Provider.UpdateServer(ctx, *serverPayload) | ||
|
||
if err != nil { | ||
return utils.ResponseError(c, err, "Failed to update the server.", 0) | ||
} | ||
return utils.ResponseSuccess(c, updatedServer, "Server Updated successfully.", 0) | ||
|
||
} | ||
|
||
func AddServer(c *fiber.Ctx) error { | ||
|
||
ctx := context.Background() | ||
serverPayload := &models.Server{} | ||
|
||
if err := c.BodyParser(serverPayload); err != nil { | ||
return utils.ResponseError(c, err, constants.InvalidBody, fiber.StatusInternalServerError) | ||
} | ||
serverPayload.ID = uuid.New().String() | ||
jwtToken, err := utils.GenerateTokenKey(*serverPayload) | ||
|
||
if err != nil { | ||
return utils.ResponseError(c, err, err.Error(), fiber.StatusInternalServerError) | ||
} | ||
|
||
serverPayload.Token = jwtToken | ||
server, err := db.Provider.AddServer(ctx, *serverPayload) | ||
|
||
if err != nil { | ||
return utils.ResponseError(c, err, err.Error(), fiber.StatusInternalServerError) | ||
} | ||
return utils.ResponseSuccess(c, server, "Server added successfully.", fiber.StatusOK) | ||
} | ||
|
||
func GetServerById(c *fiber.Ctx) error { | ||
ctx := context.Background() | ||
id := c.Params("id") | ||
|
||
server, err := db.Provider.GetRequestById(ctx, id) | ||
if err != nil { | ||
return utils.ResponseError(c, err, err.Error(), fiber.StatusInternalServerError) | ||
} | ||
return utils.ResponseSuccess(c, server, "Server retrieved successfully.", fiber.StatusOK) | ||
} | ||
|
||
func DeleteServerById(c *fiber.Ctx) error { | ||
ctx := context.Background() | ||
id := c.Params("id") | ||
|
||
err := db.Provider.DeleteServerById(ctx, id) | ||
if err != nil { | ||
return utils.ResponseError(c, err, err.Error(), fiber.StatusInternalServerError) | ||
} | ||
return utils.ResponseSuccess(c, nil, "Fetched Server lists.", fiber.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package models | ||
|
||
// type Request struct { | ||
// ID string `gorm:"primaryKey;type:char(36)" json:"id,omitempty"` | ||
// UserID string `json:"userID,omitempty"` | ||
// URL string `json:"url"` | ||
// Requests int64 `json:"requests"` | ||
// Time int `json:"time"` | ||
// Clients int `json:"clients"` | ||
// Headers string `json:"headers"` | ||
// Params string `json:"params"` | ||
// KeepAlive bool `json:"keepAlive"` | ||
// Method string `json:"method"` | ||
// Ips string `json:"ips" bson:"ips"` | ||
// PostData string `json:"postData,omitempty"` | ||
// UpdatedAt int64 `json:"updated_at" bson:"updated_at"` | ||
// CreatedAt int64 `json:"created_at" bson:"created_at"` | ||
// } | ||
type Server struct { | ||
ID string `gorm:"primaryKey;type:char(36)" json:"id,omitempty"` | ||
UserID string `json:"userID,omitempty"` | ||
IP string `json:"ip"` | ||
Port int64 `json:"port"` | ||
Token string `json:"token"` | ||
Active bool `json:"active"` | ||
Interval int `json:"interval"` | ||
UpdatedAt int64 `json:"updated_at" bson:"updated_at"` | ||
CreatedAt int64 `json:"created_at" bson:"created_at"` | ||
} | ||
|
||
type ServerList struct { | ||
Pagination *Pagination `json:"pagination"` | ||
Data []Server `json:"data"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package sql | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/cirnum/strain-hub/server/db/models" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// AddServer to update user information in database | ||
func (p *provider) AddServer(ctx context.Context, server models.Server) (models.Server, error) { | ||
if server.ID == "" { | ||
server.ID = uuid.New().String() | ||
} | ||
server.Token = uuid.New().String() | ||
server.CreatedAt = time.Now().Unix() | ||
server.UpdatedAt = time.Now().Unix() | ||
|
||
result := p.db.Create(&server) | ||
|
||
if result.Error != nil { | ||
return server, result.Error | ||
} | ||
|
||
return server, nil | ||
} | ||
|
||
// AddServer to update user information in database | ||
func (p *provider) UpdateServer(ctx context.Context, server models.Server) (models.Server, error) { | ||
server.UpdatedAt = time.Now().Unix() | ||
result := p.db.Model(&server).Where("id = ?", server.ID).Update("UpdatedAt", server.UpdatedAt) | ||
if result.Error != nil { | ||
return server, result.Error | ||
} | ||
return server, nil | ||
} | ||
|
||
// ListServer to get list of users from database | ||
func (p *provider) ListServer(ctx context.Context, pagination *models.Pagination) (*models.ServerList, error) { | ||
var servers []models.Server | ||
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&servers) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
|
||
serverList := []models.Server{} | ||
for _, request := range servers { | ||
serverList = append(serverList, request) | ||
} | ||
|
||
var total int64 | ||
totalRes := p.db.Model(&models.Request{}).Count(&total) | ||
if totalRes.Error != nil { | ||
return nil, totalRes.Error | ||
} | ||
|
||
paginationClone := pagination | ||
paginationClone.Total = total | ||
|
||
return &models.ServerList{ | ||
Pagination: paginationClone, | ||
Data: serverList, | ||
}, nil | ||
} | ||
|
||
// GetServerById to update user information in database | ||
func (p *provider) GetServerById(ctx context.Context, id string) (models.Server, error) { | ||
var server models.Server | ||
|
||
if id == "" { | ||
return server, errors.New("Request id missing.") | ||
} | ||
|
||
result := p.db.Where("id = ?", id).First(&server) | ||
if result.Error != nil { | ||
return server, result.Error | ||
} | ||
|
||
return server, nil | ||
} | ||
|
||
// DeleteServerById Request by id to update user information in database | ||
func (p *provider) DeleteServerById(ctx context.Context, id string) error { | ||
|
||
if id == "" { | ||
return errors.New("Request id missing.") | ||
} | ||
|
||
result := p.db.Delete(&models.Server{ | ||
ID: id, | ||
}) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/cirnum/strain-hub/server/app/controllers" | ||
"github.com/cirnum/strain-hub/server/pkg/middleware" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// PrivateRoutes func for describe group of private routes. | ||
func ServerRoutes(a *fiber.App) { | ||
// Create routes group. | ||
server := "/server" | ||
route := a.Group("/api/v1") | ||
// user routes | ||
route.Post(server, middleware.JWTProtected(), controllers.AddServer) | ||
route.Get(server, middleware.JWTProtected(), controllers.GetAllServer) | ||
route.Get(server+"/:id", middleware.JWTProtected(), controllers.GetServerById) | ||
route.Delete(server+"/:id", middleware.JWTProtected(), controllers.DeleteServerById) | ||
route.Put(server, middleware.JWTProtectedClient(), controllers.UpdateServer) | ||
} |
37682a2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
strain-hub – ./
strain-hub-perfcheck.vercel.app
strain-hub-git-main-perfcheck.vercel.app
strain-hub.vercel.app
www.perfcheck.com
perfcheck.com