Skip to content

Commit a31a683

Browse files
committed
Refactor addServices function to use JSON payload and remove unused imports and code
1 parent 3c14db0 commit a31a683

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

api/v1/service/caddy.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55
"os"
66
"strconv"
7-
"strings"
87
"time"
98

109
"github.com/NetSepio/erebrus/api/v1/middleware"
@@ -31,31 +30,26 @@ var resp map[string]interface{}
3130
// addTunnel adds new tunnel config
3231
func addServices(c *gin.Context) {
3332
//post form parameters
34-
name := strings.ToLower(c.PostForm("name"))
35-
ipAddress := c.PostForm("ip_address")
33+
var payload ServicePayload
3634

37-
port := c.PostForm("port")
35+
// Bind JSON payload to the struct
36+
if err := c.ShouldBindJSON(&payload); err != nil {
37+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
38+
return
39+
}
3840

3941
// convert port string to int
40-
portInt, err := strconv.Atoi(port)
42+
portInt, err := strconv.Atoi(payload.Port)
4143
if err != nil {
4244
resp = util.Message(400, "Invalid Port")
4345
c.JSON(http.StatusInternalServerError, resp)
4446
return
4547
}
4648

47-
// port allocation
48-
// max, _ := strconv.Atoi(os.Getenv("CADDY_UPPER_RANGE"))
49-
// min, _ := strconv.Atoi(os.Getenv("CADDY_LOWER_RANGE"))
50-
5149
for {
52-
// port, err := core.GetPort(max, min)
53-
// if err != nil {
54-
// panic(err)
55-
// }
5650

5751
// check validity of Services name and port
58-
value, msg, err := middleware.IsValidService(name, portInt, ipAddress)
52+
value, msg, err := middleware.IsValidService(payload.Name, portInt, payload.IPAddress)
5953

6054
if err != nil {
6155
resp = util.Message(500, "Server error, Try after some time or Contact Admin..."+err.Error())
@@ -72,11 +66,11 @@ func addServices(c *gin.Context) {
7266
} else if value == 1 {
7367
//create a Services struct object
7468
var data model.Service
75-
data.Name = name
69+
data.Name = payload.Name
7670
data.Type = os.Getenv("NODE_TYPE")
77-
data.Port = port
71+
data.Port = payload.Port
7872
data.Domain = os.Getenv("DOMAIN")
79-
data.IpAddress = ipAddress
73+
data.IpAddress = payload.IPAddress
8074
data.CreatedAt = time.Now().UTC().Format(time.RFC3339)
8175

8276
//to add Services config

api/v1/service/type.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package caddy
2+
3+
type ServicePayload struct {
4+
Name string `json:"name" binding:"required"`
5+
IPAddress string `json:"ipAddress" binding:"required"`
6+
Port string `json:"port" binding:"required"`
7+
}

0 commit comments

Comments
 (0)