-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
58 lines (51 loc) · 1.35 KB
/
server.go
File metadata and controls
58 lines (51 loc) · 1.35 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type Student struct {
Name string `json:"name"`
Sex string `json:"sex"`
BirthDate string `json:"birthDate"`
Address string `json:"address"`
Email string `json:"email"`
Phone string `json:"phone"`
}
func ServeStudentsTable(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/views/studentstable.html")
}
func ReceiveStudentData(w http.ResponseWriter, r *http.Request) {
student := Student{}
defer r.Body.Close()
jsn, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal("Error occered while reading request body: ", err)
}
err = json.Unmarshal(jsn, &student)
if err != nil {
log.Fatal("Error occured while decoding request body: ", err)
}
SaveStudent(&student)
}
func SaveStudent(student *Student) {
studentJSON, _ := json.Marshal(student)
err := ioutil.WriteFile("students.json", studentJSON, 0644)
if err != nil {
log.Fatal("Error occured while writing to JSON file: ", err)
}
fmt.Printf("%+v", studentJSON)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
http.HandleFunc("/students_table/", ServeStudentsTable)
http.HandleFunc("/save_student", ReceiveStudentData)
http.Handle("/", http.FileServer(http.Dir("public")))
log.Fatal(http.ListenAndServe(":"+port, nil))
}