-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (175 loc) · 5.38 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"encoding/json"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
//_ "github.com/jinzhu/gorm/dialects/sqlite"
"fmt"
"github.com/caarlos0/env"
_ "github.com/jinzhu/gorm/dialects/postgres"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"
)
//global database access for the callbacks
var db *gorm.DB
var answers = [...]string{
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."}
func RandomAnswer() string {
return answers[rand.Intn(len(answers))]
}
type Interaction struct {
gorm.Model `json:"-"`
Question string `json:"question"`
Answer string `json:"answer"`
}
type Question struct {
Question string `json:"question"`
}
func GetReady(w http.ResponseWriter, r *http.Request) {
var count uint
if err := db.Model(&Interaction{}).Limit(1).Count(&count).Error; err != nil {
log.Printf("[GetReady] Faild to retrieve any data")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
func GetHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func PostQuestion(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
var request Question
err = json.Unmarshal(body, &request)
if err != nil {
log.Printf("[PostQuestion] Failed to parse request '%s': %s", string(body), err)
w.WriteHeader(http.StatusBadRequest)
return
}
interaction := Interaction{Question: request.Question, Answer: RandomAnswer()}
db.Create(&interaction)
db.Save(&interaction)
if db.NewRecord(interaction) {
log.Printf("[PostQuestion] Failed to save request '%s'", string(body))
w.WriteHeader(http.StatusInternalServerError)
return
}
payload, err := json.Marshal(interaction)
if err != nil {
log.Printf("[PostQuestion] Failed to create response '%s': %s", string(payload), err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(payload)
}
func GetQuestions(w http.ResponseWriter, r *http.Request) {
var questionList []Interaction
if err := db.Find(&questionList).Error; err != nil {
log.Printf("[GetQuestions] Faild to retrieve any data")
w.WriteHeader(http.StatusNotFound)
return
}
payload, err := json.Marshal(&questionList)
if err != nil {
log.Printf("[GetQuestions] Failed to create response '%s': %s", string(payload), err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(payload)
}
type config struct {
Host string `env:"EIGHTBALL_HOST" envDefault:"0.0.0.0"`
Port string `env:"EIGHTBALL_PORT" envDefault:"8080"`
DatabaseHost string `env:"EIGHTBALL_DATABASE_HOST" envDefault:"pgsql"`
DatabasePort string `env:"EIGHTBALL_DATABASE_PORT" envDefault:"5432"`
DatabaseUser string `env:"EIGHTBALL_DATABASE_USER" envDefault:"postgres"`
DatabasePass string `env:"EIGHTBALL_DATABASE_PASS" envDefault:"root"`
DatabaseDbName string `env:"EIGHTBALL_DATABASE_DBNAME" envDefault:"postgres"`
}
func main() {
var err error
cfg := config{}
err = env.Parse(&cfg)
if err != nil {
log.Fatal("[main] Failed to load env vars")
}
conection := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable",
cfg.DatabaseHost,
cfg.DatabasePort,
cfg.DatabaseUser,
cfg.DatabaseDbName,
cfg.DatabasePass)
db, err = gorm.Open("postgres", conection)
if err != nil {
log.Fatalf("[main] Failed to open database: '%s'!", err)
}
defer db.Close()
db.AutoMigrate(&Interaction{})
rand.Seed(time.Now().UnixNano())
router := mux.NewRouter()
router.HandleFunc("/questions", PostQuestion).Methods("POST")
router.HandleFunc("/questions", GetQuestions).Methods("GET")
router.HandleFunc("/health", GetHealth).Methods("GET")
router.HandleFunc("/readiness", GetReady).Methods("GET")
srv := &http.Server{
Addr: cfg.Host + ":" + cfg.Port,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: router, // Pass our instance of gorilla/mux in.
}
// Run our server in a goroutine so that it doesn't block.
log.Printf("[main] Serving on '%s:%s'", cfg.Host, cfg.Port)
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("[main] Shutting down!")
os.Exit(0)
}