-
Notifications
You must be signed in to change notification settings - Fork 0
/
smpp_errors.go
74 lines (61 loc) · 2.12 KB
/
smpp_errors.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
// Copyright (c) 2021 Ameed Jamous - TelecomXChange LLC.
// This program is licensed under the MIT License and is available for distribution and use.
// You are allowed to modify, distribute, and use this program as per the terms of the MIT License.
// Please contact the copyright holder (Ameed Jamous - a.jamous@telecomsxchange.com) if you have any questions or require further information.
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// Open a connection to the database
db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/tcxc")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create the HTTP handler function that selects all records.
http.HandleFunc("/errorcodes", func(w http.ResponseWriter, r *http.Request) {
// Log request
log.Println("Received request:", r.Method, r.URL, r.RemoteAddr)
// Execute the SELECT query to retrieve all rows from the smpp_error_codes table
rows, err := db.Query("SELECT * FROM smpp_error_codes")
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(500), 500)
return
}
defer rows.Close()
// Iterate over the rows and scan the values into a slice of structs
var errorCodes []struct {
ID int `json:"id"`
CommandStatusName string `json:"command_status_name"`
Value string `json:"value"`
Description string `json:"description"`
}
for rows.Next() {
var ec struct {
ID int `json:"id"`
CommandStatusName string `json:"command_status_name"`
Value string `json:"value"`
Description string `json:"description"`
}
err := rows.Scan(&ec.ID, &ec.CommandStatusName, &ec.Value, &ec.Description)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(500), 500)
return
}
errorCodes = append(errorCodes, ec)
}
// Return the error codes as a JSON response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(errorCodes)
})
// Start the server
log.Fatal(http.ListenAndServe(":8083", nil))
log.Println("Server is running")
}