/
shop.go
102 lines (77 loc) · 2.26 KB
/
shop.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
package main
import (
"log"
"flag"
"fmt"
"os"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/fogcreek/logging"
"github.com/googollee/go-socket.io"
//"github.com/garyburd/redigo/redis"
"github.com/soveran/redisurl"
)
var dbredis = initDb()
var (
redisAddress = flag.String("redis-address", "redis://localhost:6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
)
func initDb() (c redis.Conn) {
rp, err := redisurl.ConnectToURL(*redisAddress)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//defer rp.Close()
return rp
}
func SetupSocketIO() (*socketio.Server, error) {
server, err := socketio.NewServer(nil)
if err != nil {
return nil, err
}
server.On("connection", func(so socketio.Socket) {
logging.InfoWithTags([]string{"socket.io"}, "New socket.io connection:", so.Id())
//so.Join("feedbag")
so.On("disconnection", func() {
// no op
})
})
server.On("error", func(so socketio.Socket, err error) {
logging.ErrorWithTags([]string{"socket.io"}, "Error on socket.io server", err.Error())
})
server.On("checkout", func(msg string) {
logging.InfoWithTags([]string{"socket.io"}, "New socket.io connection ups i did it again:", msg)
log.Println("emit:", msg)
})
return server, nil
}
func index (c *gin.Context) {
content := gin.H{"Hello":"World"}
c.JSON(200, content)
}
func main() {
r := gin.Default()
// if Allow DirectoryIndex
r.Use(static.Serve("/request/", static.LocalFile("./app", true)))
r.Use(static.Serve("/todo/", static.LocalFile("./todo", true)))
// set prefix
r.Use(static.Serve("/static", static.LocalFile("./app", true)))
r.Use(static.Serve("/static", static.LocalFile("./todo", true)))
r.GET("/ping", func(c *gin.Context) {
c.String(200, "test")
})
r.GET("/index", index, index)
// Setup Socket.io server and related activity fetching
socketServer, err := SetupSocketIO()
if err != nil {
logging.ErrorWithTags([]string{"socket.io"}, "Error on socket.io server", err.Error())
}
r.GET("/socket.io/", func(c *gin.Context) {
socketServer.ServeHTTP(c.Writer, c.Request)
})
// redis configuration
flag.Parse()
// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}