|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "strconv" |
| 11 | + |
| 12 | + "github.com/gorilla/mux" |
| 13 | + _ "github.com/lib/pq" |
| 14 | +) |
| 15 | + |
| 16 | +type App struct { |
| 17 | + Router *mux.Router |
| 18 | + DB *sql.DB |
| 19 | +} |
| 20 | + |
| 21 | +func (a *App) Initialize(db_type, db_user, db_password, db_host, db_database string) { |
| 22 | + |
| 23 | + connStr := fmt.Sprintf("%s://%s:%s@%s/%s?sslmode=disable", db_type, db_user, db_password, db_host, db_database) |
| 24 | + |
| 25 | + var err error |
| 26 | + a.DB, err = sql.Open("postgres", connStr) |
| 27 | + if err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + a.Router = mux.NewRouter() |
| 32 | + |
| 33 | + a.initializeRoutes() |
| 34 | +} |
| 35 | + |
| 36 | +func (a *App) Run(addr string) { |
| 37 | + fmt.Printf("Server running on %s \n", addr) |
| 38 | + log.Fatal(http.ListenAndServe(addr, a.Router)) |
| 39 | +} |
| 40 | + |
| 41 | +func (a *App) initializeRoutes() { |
| 42 | + api := a.Router.PathPrefix("/api/v1").Subrouter() |
| 43 | + api.HandleFunc("", a.getRequest).Methods(http.MethodGet) |
| 44 | + api.HandleFunc("", a.apiMakeDbRequest).Methods(http.MethodPost) |
| 45 | + api.HandleFunc("", a.notFound) |
| 46 | + api.HandleFunc("/user/{userID}/comment/{commentID}", a.withParams).Methods(http.MethodGet) |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +type QueryRequest struct { |
| 51 | + QueryString string `json:"query_string"` |
| 52 | +} |
| 53 | + |
| 54 | +func (a *App) apiMakeDbRequest(w http.ResponseWriter, r *http.Request) { |
| 55 | + w.Header().Set("Content-Type", "application/json") |
| 56 | + reqBody, _ := ioutil.ReadAll(r.Body) |
| 57 | + |
| 58 | + var queryrequest QueryRequest |
| 59 | + json.Unmarshal(reqBody, &queryrequest) |
| 60 | + fmt.Println(queryrequest) |
| 61 | + |
| 62 | + response, err := do_db_query(queryrequest.QueryString) |
| 63 | + if err != nil { |
| 64 | + w.WriteHeader(http.StatusInternalServerError) |
| 65 | + w.Write([]byte(`{"message": "error"}`)) |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Println(response) |
| 69 | + type RespObject struct { |
| 70 | + Data []interface{} |
| 71 | + } |
| 72 | + respObject := RespObject{response} |
| 73 | + data, err := json.Marshal(respObject) |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err) |
| 76 | + } |
| 77 | + fmt.Println("", data) |
| 78 | + |
| 79 | + w.Header().Set("Content-Type", "application/json") |
| 80 | + w.WriteHeader(http.StatusOK) |
| 81 | + w.Write(data) |
| 82 | + // var res map[string]interface{} |
| 83 | + // json.Marshal(&response, &res) |
| 84 | + |
| 85 | + // w.Write([]byte(`{"message": "post called"}`)) |
| 86 | +} |
| 87 | + |
| 88 | +func (a *App) getRequest(w http.ResponseWriter, r *http.Request) { |
| 89 | + w.Header().Set("Content-Type", "application/json") |
| 90 | + w.WriteHeader(http.StatusOK) |
| 91 | + w.Write([]byte(`{"message": "get called"}`)) |
| 92 | +} |
| 93 | + |
| 94 | +func (a *App) withParams(w http.ResponseWriter, r *http.Request) { |
| 95 | + pathParams := mux.Vars(r) |
| 96 | + w.Header().Set("Content-Type", "application/json") |
| 97 | + |
| 98 | + userID := -1 |
| 99 | + var err error |
| 100 | + if val, ok := pathParams["userID"]; ok { |
| 101 | + userID, err = strconv.Atoi(val) |
| 102 | + if err != nil { |
| 103 | + w.WriteHeader(http.StatusInternalServerError) |
| 104 | + w.Write([]byte(`{"message": "need a number"}`)) |
| 105 | + return |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + commentID := -1 |
| 110 | + if val, ok := pathParams["commentID"]; ok { |
| 111 | + commentID, err = strconv.Atoi(val) |
| 112 | + if err != nil { |
| 113 | + w.WriteHeader(http.StatusInternalServerError) |
| 114 | + w.Write([]byte(`{"message": "need a number"}`)) |
| 115 | + return |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + query := r.URL.Query() |
| 120 | + location := query.Get("location") |
| 121 | + // example query |
| 122 | + // http://127.0.0.1:8080/api/v1/user/23/comment/55?location=elsewhere |
| 123 | + w.Write([]byte(fmt.Sprintf(`{"userID": %d, "commentID": %d, "location": "%s" }`, userID, commentID, location))) |
| 124 | +} |
| 125 | + |
| 126 | +func (a *App) notFound(w http.ResponseWriter, r *http.Request) { |
| 127 | + w.Header().Set("Content-Type", "application/json") |
| 128 | + w.WriteHeader(http.StatusNotFound) |
| 129 | + w.Write([]byte(`{"message": "not found"}`)) |
| 130 | +} |
0 commit comments