Skip to content

Commit 732d82f

Browse files
committed
uupdated project refatoring
1 parent 02bf865 commit 732d82f

File tree

4 files changed

+119
-111
lines changed

4 files changed

+119
-111
lines changed

app.go

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package main
22

33
import (
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
}

do_db_query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func do_db_select_query(db *sql.DB, query_string string) ([]query_response, erro
5252
}
5353

5454
if len(query_results) < 1 {
55-
err = errors.New("ot found")
55+
err = errors.New("empty response")
5656
return nil, err
5757
}
5858

responseHandlers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
9+
response, _ := json.Marshal(payload)
10+
11+
w.Header().Set("Content-Type", "application/json")
12+
w.WriteHeader(code)
13+
w.Write(response)
14+
}
15+
16+
func respondWithError(w http.ResponseWriter, code int, message string) {
17+
respondWithJSON(w, code, map[string]string{"error": message})
18+
}

routeFunctions.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"strconv"
9+
10+
"github.com/gorilla/mux"
11+
)
12+
13+
type QueryRequest struct {
14+
QueryString string `json:"query_string"`
15+
}
16+
17+
func (a *App) apiMakeDbRequest(w http.ResponseWriter, r *http.Request) {
18+
19+
executeOnly := r.URL.Query().Get("executeOnly")
20+
executeOnly_state := 0
21+
var err error
22+
23+
if len(executeOnly) > 0 {
24+
executeOnly_state, err = strconv.Atoi(executeOnly)
25+
if err != nil {
26+
respondWithError(w, http.StatusBadRequest, err.Error())
27+
}
28+
}
29+
30+
reqBody, err := ioutil.ReadAll(r.Body)
31+
if err != nil {
32+
respondWithError(w, http.StatusInternalServerError, err.Error())
33+
}
34+
35+
var queryrequest QueryRequest
36+
json.Unmarshal(reqBody, &queryrequest)
37+
fmt.Println(queryrequest)
38+
39+
var response interface{}
40+
switch executeOnly_state {
41+
case 0:
42+
response, err = do_db_select_query(a.DB, queryrequest.QueryString)
43+
44+
default:
45+
err = do_db_query_exec(a.DB, queryrequest.QueryString)
46+
response = map[string]interface{}{"status": 1}
47+
48+
}
49+
50+
if err != nil {
51+
respondWithError(w, http.StatusInternalServerError, err.Error())
52+
return
53+
}
54+
respondWithJSON(w, http.StatusOK, response)
55+
56+
}
57+
58+
func (a *App) getRequest(w http.ResponseWriter, r *http.Request) {
59+
w.Header().Set("Content-Type", "application/json")
60+
w.WriteHeader(http.StatusOK)
61+
w.Write([]byte(`{"message": "get called"}`))
62+
}
63+
64+
func (a *App) withParams(w http.ResponseWriter, r *http.Request) {
65+
pathParams := mux.Vars(r)
66+
w.Header().Set("Content-Type", "application/json")
67+
68+
userID := -1
69+
var err error
70+
if val, ok := pathParams["userID"]; ok {
71+
userID, err = strconv.Atoi(val)
72+
if err != nil {
73+
w.WriteHeader(http.StatusInternalServerError)
74+
w.Write([]byte(`{"message": "need a number"}`))
75+
return
76+
}
77+
}
78+
79+
commentID := -1
80+
if val, ok := pathParams["commentID"]; ok {
81+
commentID, err = strconv.Atoi(val)
82+
if err != nil {
83+
w.WriteHeader(http.StatusInternalServerError)
84+
w.Write([]byte(`{"message": "need a number"}`))
85+
return
86+
}
87+
}
88+
89+
query := r.URL.Query()
90+
location := query.Get("location")
91+
// example query
92+
// http://127.0.0.1:8080/api/v1/user/23/comment/55?location=elsewhere
93+
w.Write([]byte(fmt.Sprintf(`{"userID": %d, "commentID": %d, "location": "%s" }`, userID, commentID, location)))
94+
}
95+
96+
func (a *App) notFound(w http.ResponseWriter, r *http.Request) {
97+
w.Header().Set("Content-Type", "application/json")
98+
w.WriteHeader(http.StatusNotFound)
99+
w.Write([]byte(`{"message": "not found"}`))
100+
}

0 commit comments

Comments
 (0)