-
Notifications
You must be signed in to change notification settings - Fork 0
/
attmpt1.go
178 lines (112 loc) · 3.57 KB
/
attmpt1.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
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
"github.com/ttacon/chalk"
_ "github.com/mattn/go-sqlite3"
"log"
"database/sql"
"strconv"
"html/template"
)
// data structure for trend
type User struct{
Roll_number int
Reg_number int
Air int
Name string
Sex string
Parent_name string
Nationality string
Category string
P_addr string
C_addr string
P_city string
C_city string
P_pincode int
C_pincode int
P_landline int
C_landline int
P_mobile int
C_mobile int
Email string
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
http.ServeFile(w,r,"index.html")
ip := r.RemoteAddr
fmt.Println(chalk.Yellow,ip," requested first-year page...",chalk.Reset)
}
func ServeHTMl(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
ip := r.RemoteAddr
filepath:= r.URL.Path
filename:=filepath[1:]
if len(filename)==0 {
http.ServeFile(w,r,"home.html")
fmt.Println(chalk.Yellow,ip," requested home page...",chalk.Reset)
} else{
http.ServeFile(w,r,filename+".html")
fmt.Println(chalk.Yellow,ip," requested",filename,"page...",chalk.Reset)
}
}
func Submit(w http.ResponseWriter, r *http.Request, p httprouter.Params){
rollnumber,_ := strconv.Atoi(r.FormValue("rollnumber"))
air,_ := strconv.Atoi(r.FormValue("air"))
regnumber,_ :=strconv.Atoi(r.FormValue("regnumber"))
P_pincode,_ := strconv.Atoi(r.FormValue("pincode1"))
C_pincode,_:= strconv.Atoi(r.FormValue("pincode2"))
P_landline,_ := strconv.Atoi(r.FormValue("phone1"))
C_landline,_ := strconv.Atoi(r.FormValue("phone2"))
P_mobile,_ := strconv.Atoi(r.FormValue("mobile1"))
C_mobile,_ := strconv.Atoi(r.FormValue("mobile2"))
student := User{
rollnumber,
regnumber,
air,
r.FormValue("name"),
r.FormValue("sex"),
r.FormValue("parentname"),
r.FormValue("nationality"),
r.FormValue("catgy"),
r.FormValue("addr"),
r.FormValue("commaddr"),
r.FormValue("city1"),
r.FormValue("city2"),
P_pincode,
C_pincode,
P_landline,
C_landline,
P_mobile,
C_mobile,
r.FormValue("email") }
output:= "You have been registered as roll number : "+r.FormValue("rollnumber")
fmt.Println(output)
t,_ := template.ParseFiles("success.html")
t.Execute(w,student)
db, err := sql.Open("sqlite3", "./test.db")
riperr(err)
defer db.Close()
query1,err := db.Prepare("insert into student_1 values(?,?,?,?,?)" )
query1.Exec(student.Roll_number,student.Reg_number,student.Air,student.Name,student.Sex)
query2,err := db.Prepare("insert into student_2 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
query2.Exec(student.Name,student.Parent_name,student.Nationality,student.Category,student.P_addr,student.C_addr,student.P_city,student.C_city,student.P_pincode,student.C_pincode,student.P_landline,student.C_landline,student.P_mobile,student.C_mobile,student.Email)
}
func GoBack(w http.ResponseWriter, r *http.Request, p httprouter.Params){
http.Redirect(w, r, "/", 301)
}
func main(){
newfeed:= chalk.Red.NewStyle().WithBackground(chalk.Black)
Server:= httprouter.New()
Server.GET("/firstyear",ServeHTMl)
Server.POST("/submit",Submit)
Server.GET("/submit",GoBack)
Server.ServeFiles("/resources/*filepath", http.Dir("resources"))
Server.GET("/",ServeHTMl)
fmt.Println(newfeed,"waiting at :3000",chalk.Reset);
http.ListenAndServe(":3000",Server)
}
func riperr(err error){
if err!= nil{
log.Fatal(err)
}
}