@@ -2,12 +2,9 @@ package main
22
33import (
44 "database/sql"
5- "encoding/json"
65 "fmt"
7- "io/ioutil"
86 "log"
97 "net/http"
10- "strconv"
118
129 "github.com/gorilla/mux"
1310 _ "github.com/lib/pq"
@@ -29,7 +26,6 @@ func (a *App) Initialize(db_type, db_user, db_password, db_host, db_database str
2926 }
3027
3128 a .Router = mux .NewRouter ()
32-
3329 a .initializeRoutes ()
3430}
3531
@@ -44,110 +40,4 @@ func (a *App) initializeRoutes() {
4440 api .HandleFunc ("" , a .apiMakeDbRequest ).Methods (http .MethodPost )
4541 api .HandleFunc ("" , a .notFound )
4642 api .HandleFunc ("/user/{userID}/comment/{commentID}" , a .withParams ).Methods (http .MethodGet )
47-
48- }
49-
50- ////////
51-
52- func respondWithJSON (w http.ResponseWriter , code int , payload interface {}) {
53- response , _ := json .Marshal (payload )
54-
55- w .Header ().Set ("Content-Type" , "application/json" )
56- w .WriteHeader (code )
57- w .Write (response )
58- }
59-
60- func respondWithError (w http.ResponseWriter , code int , message string ) {
61- respondWithJSON (w , code , map [string ]string {"error" : message })
62- }
63-
64- ////////
65-
66- type QueryRequest struct {
67- QueryString string `json:"query_string"`
68- }
69-
70- func (a * App ) apiMakeDbRequest (w http.ResponseWriter , r * http.Request ) {
71-
72- executeOnly := r .URL .Query ().Get ("executeOnly" )
73- executeOnly_state := 0
74- var err error
75-
76- if len (executeOnly ) > 0 {
77- executeOnly_state , err = strconv .Atoi (executeOnly )
78- if err != nil {
79- respondWithError (w , http .StatusBadRequest , err .Error ())
80- }
81- }
82-
83- reqBody , err := ioutil .ReadAll (r .Body )
84- if err != nil {
85- respondWithError (w , http .StatusInternalServerError , err .Error ())
86- }
87-
88- var queryrequest QueryRequest
89- json .Unmarshal (reqBody , & queryrequest )
90- fmt .Println (queryrequest )
91-
92- var response interface {}
93- switch executeOnly_state {
94- case 0 :
95- response , err = do_db_select_query (a .DB , queryrequest .QueryString )
96-
97- default :
98- err = do_db_query_exec (a .DB , queryrequest .QueryString )
99- response = map [string ]interface {}{"status" : 1 }
100-
101- }
102-
103- if err != nil {
104- respondWithError (w , http .StatusInternalServerError , err .Error ())
105- return
106- }
107- respondWithJSON (w , http .StatusOK , response )
108-
109- }
110-
111- func (a * App ) getRequest (w http.ResponseWriter , r * http.Request ) {
112- w .Header ().Set ("Content-Type" , "application/json" )
113- w .WriteHeader (http .StatusOK )
114- w .Write ([]byte (`{"message": "get called"}` ))
115- }
116-
117- func (a * App ) withParams (w http.ResponseWriter , r * http.Request ) {
118- pathParams := mux .Vars (r )
119- w .Header ().Set ("Content-Type" , "application/json" )
120-
121- userID := - 1
122- var err error
123- if val , ok := pathParams ["userID" ]; ok {
124- userID , err = strconv .Atoi (val )
125- if err != nil {
126- w .WriteHeader (http .StatusInternalServerError )
127- w .Write ([]byte (`{"message": "need a number"}` ))
128- return
129- }
130- }
131-
132- commentID := - 1
133- if val , ok := pathParams ["commentID" ]; ok {
134- commentID , err = strconv .Atoi (val )
135- if err != nil {
136- w .WriteHeader (http .StatusInternalServerError )
137- w .Write ([]byte (`{"message": "need a number"}` ))
138- return
139- }
140- }
141-
142- query := r .URL .Query ()
143- location := query .Get ("location" )
144- // example query
145- // http://127.0.0.1:8080/api/v1/user/23/comment/55?location=elsewhere
146- w .Write ([]byte (fmt .Sprintf (`{"userID": %d, "commentID": %d, "location": "%s" }` , userID , commentID , location )))
147- }
148-
149- func (a * App ) notFound (w http.ResponseWriter , r * http.Request ) {
150- w .Header ().Set ("Content-Type" , "application/json" )
151- w .WriteHeader (http .StatusNotFound )
152- w .Write ([]byte (`{"message": "not found"}` ))
15343}
0 commit comments