This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
executable file
·141 lines (94 loc) · 2.68 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
_"io"
_"io/ioutil"
"net/http"
_"github.com/gorilla/mux"
_"time"
"github.com/k-goepel/smarteyeapi/SmartEyeInterface"
"time"
"database/sql"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome! test\n")
}
/*
Test with this curl command:
curl -H "Content-Type: application/json" -d '{"name":"New Todo"}' http://localhost:8080/todos
*/
func TodoIndex(w http.ResponseWriter, r *http.Request) {
t := SmartEyeInterface.CreateTodo(1, "test", false, time.Now())
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(t); err != nil {
panic(err)
}
}
func pullTodo(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Stored in DB:\n")
db, err := sql.Open("postgres", "user=awspostgres " +
"dbname=smarteyeTest " +
"password=S!lly5tr1ng " +
"host=smarteye-testdb.coteiblw5axo.us-west-2.rds.amazonaws.com")
if err != nil {
panic(err)
}
db.Ping()
rows, err := db.Query("SELECT * FROM todo")
for rows.Next(){
id := 0
name := ""
completed := false
due := ""
//t, err := time.Parse("0000-00-00", due)
//
//if err != nil{
// panic(err)
//}
rows.Scan(&id, &name, &completed, &due)
fmt.Println(rows)
output := fmt.Sprintf("Id= $1 Name= $2 Completed= $3", id, name, completed)
fmt.Fprint(w, output)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
}
func patternPage(w http.ResponseWriter, r *http.Request){
fmt.Fprint(w, "New Pattern")
p := SmartEyeInterface.NewManualPattern("Soon", "%1Jw7$hhgeo")
err := SmartEyeInterface.UploadManualPattern(p)
output := fmt.Sprintf("$1, $2", SmartEyeInterface.GetMessage(err), SmartEyeInterface.GetTime(err))
fmt.Fprint(w, output)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
}
func pullPattern(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Stored in DB:\n")
db, err := sql.Open("postgres", "user=awspostgres " +
"dbname=smarteyeTest " +
"password=S!lly5tr1ng " +
"host=smarteye-testdb.coteiblw5axo.us-west-2.rds.amazonaws.com")
if err != nil {
panic(err)
}
db.Ping()
rows, err := db.Query("SELECT * FROM patterns")
for rows.Next(){
id := 0
name := ""
pattern := ""
//t, err := time.Parse("0000-00-00", due)
//
//if err != nil{
// panic(err)
//}
rows.Scan(&id, &name, &pattern)
fmt.Println(rows)
output := fmt.Sprintf("Id= $1 Name= $2 Pattern= $3", id, name, pattern)
fmt.Fprint(w, output)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
}