-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathredis_mysql.go
148 lines (118 loc) · 2.77 KB
/
redis_mysql.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
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"encoding/json"
"github.com/gomodule/redigo/redis"
_ "github.com/go-sql-driver/mysql"
"reflect"
)
var tpl *template.Template
var f string
var l string
func init() {
tpl = template.Must(template.ParseGlob("templates/*"))
}
func foo(w http.ResponseWriter, req *http.Request) {
f = req.FormValue("Party Name")
l = req.FormValue("STATE")
fmt.Println(f, l)
db, err := sql.Open("mysql", "root:Shivangi18!@tcp(127.0.0.1:3306)/project")
if err != nil {
panic(err.Error())
}
defer db.Close()
fmt.Println("Successfully Connected")
fmt.Println("Yes it is connected")
pool := newPool()
conn := pool.Get()
defer conn.Close()
insert, err1 := db.Prepare("INSERT INTO article (title,content) VALUES (?,?)")
if err1 != nil {
panic(err1.Error())
}
insert.Exec(f, l)
defer insert.Close()
selDB,err2:=db.Query("SELECT * FROM article WHERE id=15")
if err2 != nil {
panic(err2.Error())
}
usr := User{}
res := []User{}
for selDB.Next() {
var id int
var title, content string
err3 := selDB.Scan(&id, &title, &content)
if err3 != nil {
panic(err3.Error())
println("Hello")
println("Hello World")
}
usr.Id = id
usr.Title = title
usr.Content = content
res = append(res, usr)
}
//defer res.Close()
err = setStruct(conn,usr.Id,usr.Title,usr.Content)
if err != nil {
fmt.Println(err)
}
err2 := tpl.ExecuteTemplate(w, "index.gohtml", person{f, l, s})
if err2 != nil {
http.Error(w, err2.Error(), 500)
log.Fatalln(err2)
}
}
func login(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, "nomination_form.gohtml", nil)
}
func newPool() *redis.Pool {
return &redis.Pool{
// Maximum number of idle connections in the pool.
MaxIdle: 80,
// max number of connections
MaxActive: 12000,
// Dial is an application supplied function for creating and
// configuring a connection.
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err.Error())
}
return c, err
},
}
}
type User struct {
Id int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
func setStruct(c redis.Conn,i int,t string,co string) error {
const objectPrefix string = "user:"
usr := User{
Id: i,
Title: t ,
Content: co ,
}
// serialize User object to JSON
json, err := json.Marshal(usr)
fmt.Println(reflect.TypeOf(json))
if err != nil {
return err
}
// SET object
_, err = c.Do("SET", usr.Title, json)
if err != nil {
return err
}
return nil
}
func main() {
http.HandleFunc("/", login)
http.HandleFunc("/foo", foo)
http.ListenAndServe(":8002", nil)
}