-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (113 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"log"
"strings"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/zaranggi/go-fiber-mysql-api-gateway/models"
)
func main() {
// Panggil Connection Model
models.ConnectDatabase()
// create new fiber
app := fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
})
// group routing ke /api/v1
api := app.Group("/api/v1")
// Register endpoint
registerRoutes(api)
// listen port
app.Listen(":7272")
}
// register roting
func registerRoutes(api fiber.Router) {
// Routing for dynamic SQL query /mysql/query method POST {query : write SQL query}
api.Post("/mysql/query", func(c *fiber.Ctx) error {
// Retrieve request body
var request models.QueryRequest
err := c.BodyParser(&request)
if err != nil {
log.Println(err)
}
query := request.Query
// Split the query
queries := strings.Split(query, ";")
// Remove empty queries
cleanedQueries := make([]string, 0)
for _, q := range queries {
trimmedQuery := strings.TrimSpace(q)
if trimmedQuery != "" {
cleanedQueries = append(cleanedQueries, trimmedQuery)
}
}
// Execute setiap query
results := make([]interface{}, len(cleanedQueries))
for i, q := range cleanedQueries {
rows, err := models.DB.Raw(q).Rows()
if err != nil {
log.Println(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": fiber.StatusInternalServerError,
"message": "Error, Please check your SQL query!",
"data": nil,
})
}
defer rows.Close()
// Store hasil query ke results
columns, err := rows.Columns()
if err != nil {
log.Println(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": fiber.StatusInternalServerError,
"message": "Error pembacaan column names",
"data": nil,
})
}
// Create a slice to hold the query result
queryResult := make([]map[string]interface{}, 0)
// Iterate over the query results
for rows.Next() {
values := make([]interface{}, len(columns))
valuePointers := make([]interface{}, len(columns))
for i := range columns {
valuePointers[i] = &values[i]
}
err := rows.Scan(valuePointers...)
if err != nil {
log.Println(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": fiber.StatusInternalServerError,
"message": "Error pembacaan row values",
"data": nil,
})
}
rowData := make(map[string]interface{})
for i, col := range columns {
value := values[i]
if v, ok := value.(time.Time); ok {
rowData[col] = v.Local().Format("2006-01-02 15:04:05")
} else if byteSlice, ok := value.([]byte); ok {
rowData[col] = string(byteSlice)
} else {
rowData[col] = value
}
}
// Append the row data ke query result
queryResult = append(queryResult, rowData)
}
// Store query result ke results slice
results[i] = queryResult
}
// Prepare response
response := fiber.Map{
"code": fiber.StatusOK,
"message": "Success",
"data": results,
}
// Return response
return c.JSON(response)
})
}