-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
restapiSetellite.go
executable file
·83 lines (79 loc) · 2.04 KB
/
restapiSetellite.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"gopkg.in/mgo.v2"
)
// handleAPISetelliteItems 함수는 project, rollmedia을 받아서 setellite 정보를 반환한다.
func handleAPISetelliteItems(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
q := r.URL.Query()
project := q.Get("project")
rollmedia := q.Get("rollmedia")
type recipe struct {
Data []Setellite `json:"data"`
}
rcp := recipe{}
rcp.Data, err = SetelliteItems(session, project, rollmedia)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"err\":\"%v\"}\n", err)
return
}
}
// handleAPISetelliteSearch 함수는 project, searchword의 검색어를 통해 setellite 정보를 반환한다.
func handleAPISetelliteSearch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
q := r.URL.Query()
project := q.Get("project")
searchword := q.Get("searchword")
type recipe struct {
Data []Setellite `json:"data"`
}
rcp := recipe{}
rcp.Data, err = SetelliteSearch(session, project, searchword)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"err\":\"%v\"}\n", err)
return
}
}