-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
159 lines (135 loc) · 3.63 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"html/template"
"github.com/jessevdk/go-flags"
"os"
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/lib/pq"
"github.com/ChimeraCoder/anaconda"
"strconv"
"github.com/martini-contrib/sessions"
"github.com/martini-contrib/binding"
)
func main(){
// Parse options
opts := parseFlags()
// Prepare martini
m := martini.Classic()
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
Extensions: []string{".html"},
Funcs: []template.FuncMap{},
Charset: "UTF-8",
IndentJSON: true,
IndentXML: true,
}))
store := sessions.NewCookieStore([]byte("secret"))
m.Use(sessions.Sessions("session", store))
// Init Database
var db gorm.DB
var err error
switch opts.Database {
case "mysql":
db, err = gorm.Open("mysql", opts.DBOptions)
case "postgres":
db, err = gorm.Open("postgres", opts.DBOptions)
case "sqlite":
db, err = gorm.Open("sqlite", opts.DBOptions)
}
if err != nil{
panic(err)
}
// Migrate database
// Debug
//db.DropTable(&Talk{}, &Bot{}, &Tweet{})
db.AutoMigrate(&Talk{}, &Bot{}, &Tweet{})
m.Map(db)
// Init Twitter Api object
anaconda.SetConsumerKey(opts.ConsumerKey)
anaconda.SetConsumerSecret(opts.ConsumerSecret)
// Index
m.Get("/", Index)
// Register bot handlers
m.Group("/twitter", func(r martini.Router) {
r.Get("/", TwitterLogin)
r.Get("/callback", TwitterCallback)
})
// API handlers
m.Group("/api", func(r martini.Router){
m.Group("/bot", func(r martini.Router) {
m.Get("", IndexBot)
m.Get("/:id", GetBot)
m.Post("", binding.Bind(Bot{}), CreateBot)
m.Put("", binding.Bind(Bot{}), UpdateBot)
m.Delete("/:id", DeleteBot)
})
m.Group("/talk", func(r martini.Router) {
m.Get("", IndexTalk)
m.Get("/:id", GetTalk)
m.Post("", binding.Bind(Talk{}), CreateTalk)
m.Put("", binding.Bind(Talk{}), UpdateTalk)
m.Delete("/:id", DeleteTalk)
})
m.Group("/tweet", func(r martini.Router) {
m.Get("", IndexTweet)
m.Get("/:id", GetTweet)
m.Post("", binding.Bind(Tweet{}), CreateTweet)
m.Put("", binding.Bind(Tweet{}), UpdateTweet)
m.Delete("/:id", DeleteTweet)
})
// Start talk on CLI.
m.Get("/", StartTalk)
//Start talk on Web browser with WebSocket
m.Get("/ws", StartTalkSocket)
// Delete tweets of a talk from Twitter.
m.Delete("/", DelTalkTweets)
})
m.RunOnAddr(fmt.Sprintf(":%d", opts.Port))
}
func parseFlags() *Options{
var opts Options
parser := flags.NewParser(&opts, flags.Default)
parser.Name = "bot-net-framework"
parser.Usage = "[OPTIONS]"
_, err := parser.Parse()
if err != nil {
os.Exit(1)
}
if opts.Port == 0{
if opts.Port, err = strconv.Atoi(os.Getenv("BN_PORT")); opts.Port == 0 || err != nil{
fmt.Println("Port number must be set as option or environment value.")
os.Exit(1)
}
}
if opts.ConsumerKey == "" {
if opts.ConsumerKey = os.Getenv("BN_CONSUMER_KEY"); opts.ConsumerKey == ""{
fmt.Println("Consumer key must be set as option or environment value.")
os.Exit(1)
}
}
if opts.ConsumerSecret == ""{
if opts.ConsumerSecret = os.Getenv("BN_CONSUMER_SECRET"); opts.ConsumerSecret == ""{
fmt.Println("Consumer secret must be set as option or environment value.")
os.Exit(1)
}
}
if opts.Database == ""{
if opts.Database = os.Getenv("BN_DATABASE"); opts.Database == "" {
fmt.Println("Database: mysql, postgre, sqlite")
os.Exit(1)
}
}
if opts.DBOptions == "" {
if opts.DBOptions = os.Getenv("BN_DB_OPTIONS"); opts.DBOptions == ""{
fmt.Println("Datamase options must be set.")
os.Exit(1)
}
}
return &opts
}