-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer.go
61 lines (48 loc) · 1.34 KB
/
customer.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
package main
import (
"fmt"
"os"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/jinzhu/gorm"
)
func Database() *gorm.DB{
//open a db connection
db_user := os.Getenv("DB_USER")
db_pass := os.Getenv("DB_PASS")
db_host := os.Getenv("DB_HOST")
db_port := os.Getenv("DB_PORT")
db, err := gorm.Open("mysql", db_user+":"+db_pass+"@tcp("+db_host+":"+db_port+")/kubernetes")
if err != nil{
fmt.Println(err.Error())
}
return db
}
type customers struct {
Id string `json:"id" binding:"required"`
Nama string `json:"username" binding:"required"`
Ktp int `json:"ktp" binding:"required"`
Status int `json:"status" binding:"required"`
Reg_date string `json:"reg_date" binding:"required"`
Alamat string `json:"alamat" binding:"required"`
}
func GetCustomer(c *gin.Context) {
var customer []customers
db := Database()
db.Find(&customer)
if len(customer) <= 0 {
c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "Customer not found!"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "200", "count": len(customer), "result" : customer})
}
func main() {
//router := gin.Default()
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.GET("/customer", GetCustomer)
router.Run(":8082")
}