-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
44 lines (35 loc) · 719 Bytes
/
main.go
File metadata and controls
44 lines (35 loc) · 719 Bytes
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
package main
import (
"fmt"
"net/http"
"encoding/json"
)
func main() {
http.HandleFunc("/student", ActionStudent)
server := new(http.Server)
server.Addr = ":9000"
fmt.Println("Server started at localhost:9000")
server.ListenAndServe()
}
func ActionStudent(w http.ResponseWriter, r *http.Request) {
if !Auth(w, r) {
return
}
if !AllowOnlyGET(w, r) {
return
}
if id := r.URL.Query().Get("id"); id != "" {
OutputJSON(w, SelectStudent(id))
return
}
OutputJSON(w, GetStudent())
}
func OutputJSON(w http.ResponseWriter, o interface{}) {
res, err := json.Marshal(o)
if err != nil {
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(res)
}