|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + c "github.com/core-go/cassandra" |
| 8 | + "github.com/gocql/gocql" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +type Handler struct { |
| 13 | + DB *sql.DB |
| 14 | + Session *gocql.Session |
| 15 | + Error func(context.Context, string) |
| 16 | +} |
| 17 | + |
| 18 | +func NewHandler(db *sql.DB, options ...func(context.Context, string)) *Handler { |
| 19 | + var logError func(context.Context, string) |
| 20 | + if len(options) >= 1 { |
| 21 | + logError = options[0] |
| 22 | + } |
| 23 | + return &Handler{DB: db, Error: logError} |
| 24 | +} |
| 25 | + |
| 26 | +func (h *Handler) Exec(w http.ResponseWriter, r *http.Request) { |
| 27 | + s := c.JStatement{} |
| 28 | + er0 := json.NewDecoder(r.Body).Decode(&s) |
| 29 | + if er0 != nil { |
| 30 | + http.Error(w, er0.Error(), http.StatusBadRequest) |
| 31 | + return |
| 32 | + } |
| 33 | + s.Params = c.ParseDates(s.Params, s.Dates) |
| 34 | + res, er1 := c.Exec(h.Session, s.Query, s.Params...) |
| 35 | + if er1 != nil { |
| 36 | + handleError(w, r, 500, er1.Error(), h.Error, er1) |
| 37 | + return |
| 38 | + } |
| 39 | + succeed(w, r, http.StatusOK, res) |
| 40 | +} |
| 41 | + |
| 42 | +func (h *Handler) Query(w http.ResponseWriter, r *http.Request) { |
| 43 | + s := c.JStatement{} |
| 44 | + er0 := json.NewDecoder(r.Body).Decode(&s) |
| 45 | + if er0 != nil { |
| 46 | + http.Error(w, er0.Error(), http.StatusBadRequest) |
| 47 | + return |
| 48 | + } |
| 49 | + s.Params = c.ParseDates(s.Params, s.Dates) |
| 50 | + res, err := c.QueryMap(h.Session, s.Query, s.Params...) |
| 51 | + if err != nil { |
| 52 | + handleError(w, r, 500, err.Error(), h.Error, err) |
| 53 | + return |
| 54 | + } |
| 55 | + succeed(w, r, http.StatusOK, res) |
| 56 | +} |
| 57 | + |
| 58 | +func (h *Handler) ExecBatch(w http.ResponseWriter, r *http.Request) { |
| 59 | + var s []c.JStatement |
| 60 | + b := make([]c.Statement, 0) |
| 61 | + er0 := json.NewDecoder(r.Body).Decode(&s) |
| 62 | + if er0 != nil { |
| 63 | + http.Error(w, er0.Error(), http.StatusBadRequest) |
| 64 | + return |
| 65 | + } |
| 66 | + l := len(s) |
| 67 | + for i := 0; i < l; i++ { |
| 68 | + st := c.Statement{} |
| 69 | + st.Query = s[i].Query |
| 70 | + st.Params = c.ParseDates(s[i].Params, s[i].Dates) |
| 71 | + b = append(b, st) |
| 72 | + } |
| 73 | + res, err := c.ExecuteAll(r.Context(), h.Session, b...) |
| 74 | + if err != nil { |
| 75 | + handleError(w, r, 500, err.Error(), h.Error, err) |
| 76 | + return |
| 77 | + } |
| 78 | + succeed(w, r, http.StatusOK, res) |
| 79 | +} |
| 80 | + |
| 81 | +func handleError(w http.ResponseWriter, r *http.Request, code int, result interface{}, logError func(context.Context, string), err error) { |
| 82 | + if logError != nil { |
| 83 | + logError(r.Context(), err.Error()) |
| 84 | + } |
| 85 | + returnJSON(w, code, result) |
| 86 | +} |
| 87 | +func succeed(w http.ResponseWriter, r *http.Request, code int, result interface{}) { |
| 88 | + response, _ := json.Marshal(result) |
| 89 | + w.Header().Set("Content-Type", "application/json") |
| 90 | + w.WriteHeader(code) |
| 91 | + w.Write(response) |
| 92 | +} |
| 93 | +func returnJSON(w http.ResponseWriter, code int, result interface{}) error { |
| 94 | + w.Header().Set("Content-Type", "application/json") |
| 95 | + w.WriteHeader(code) |
| 96 | + if result == nil { |
| 97 | + w.Write([]byte("null")) |
| 98 | + return nil |
| 99 | + } |
| 100 | + response, err := marshal(result) |
| 101 | + if err != nil { |
| 102 | + // log.Println("cannot marshal of result: " + err.Error()) |
| 103 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 104 | + return err |
| 105 | + } |
| 106 | + w.Write(response) |
| 107 | + return nil |
| 108 | +} |
| 109 | +func marshal(v interface{}) ([]byte, error) { |
| 110 | + b, ok1 := v.([]byte) |
| 111 | + if ok1 { |
| 112 | + return b, nil |
| 113 | + } |
| 114 | + s, ok2 := v.(string) |
| 115 | + if ok2 { |
| 116 | + return []byte(s), nil |
| 117 | + } |
| 118 | + return json.Marshal(v) |
| 119 | +} |
0 commit comments