-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (106 loc) · 2.84 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
package main
import (
stdContext "context"
"fmt"
"log"
"time"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"github.com/jinzhu/gorm"
"github.com/kataras/iris"
"github.com/ysaakpr/kwon/jobs"
"github.com/ysaakpr/kwon/orders"
"go.uber.org/zap"
"google.golang.org/grpc"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func runtime(db *gorm.DB, logger *zap.Logger) func(ctx iris.Context) {
return func(ctx iris.Context) {
ctx.Values().Set("DB", db)
ctx.Values().Set("LOGGER", logger)
ctx.Next()
}
}
func newClient() *dgo.Dgraph {
// Dial a gRPC connection. The address to dial to can be configured when
// setting up the dgraph cluster.
d, err := grpc.Dial("ae527e5fc7c3a11e895260272f5adedd-1486258187.ap-southeast-1.elb.amazonaws.com:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
return dgo.NewDgraphClient(
api.NewDgraphClient(d),
)
}
func setup(c *dgo.Dgraph) {
// Install a schema into dgraph. Accounts have a `name` and a `balance`.
err := c.Alter(stdContext.Background(), &api.Operation{
Schema: `
name: string @index(term) .
total_price: float .
total_discount: float .
code: string @index(term) .
reference: string @index(term) .
discription: string .
image_url: string .
unit_discount: float .
unit_price: float .
unit: string .
quantity: int .
pincode: string .
phone_number: string .
`,
})
if err != nil {
fmt.Printf("%#v", err)
panic("unable to create schema")
}
}
func main() {
app := iris.Default()
logger, _ := zap.NewProduction()
defer logger.Sync()
iris.RegisterOnInterrupt(func() {
timeout := 5 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
// close all hosts
app.Shutdown(ctx)
})
dsURL := "root:livspace@/ls-ims?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open("mysql", dsURL)
if err != nil {
logger.Info("Failed to open a database connection",
zap.String("url", dsURL),
)
panic("sdfsdf")
}
defer db.Close()
jobService := jobs.NewJobService(db)
app.UseGlobal(runtime(db, logger))
// Method: GET
// Resource: http://localhost:8080/
app.Handle("GET", "/", func(ctx iris.Context) {
val := jobService.Search(&ctx)
ctx.JSON(val)
})
dgo := newClient()
dgo.Alter(stdContext.Background(), &api.Operation{DropAll: true})
//setup(dgo)
app.PartyFunc("/api/v1/order", orders.RestAPIOrdersV1(dgo))
// same as app.Handle("GET", "/ping", [...])
// Method: GET
// Resource: http://localhost:8080/ping
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString("pong")
})
// Method: GET
// Resource: http://localhost:8080/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello iris web framework."})
})
// http://localhost:8080
// http://localhost:8080/ping
// http://localhost:8080/hello
app.Run(iris.Addr(":8080"))
}