Last active
January 24, 2016 08:45
-
-
Save petomalina/b635b47d697cfddeea92 to your computer and use it in GitHub Desktop.
[Golang] MongoDB REST controller snippet for atom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/json" | |
"net/http" | |
) | |
// SendJSON will write the response with the OK | |
// http code | |
func SendJSON(obj interface{}, w http.ResponseWriter) error { | |
return SendJSONCode(http.StatusOK, obj, w) | |
} | |
// SendJSONCode will send the json to the client, | |
// allowing to specify http status code | |
func SendJSONCode(code int, obj interface{}, w http.ResponseWriter) error { | |
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | |
w.WriteHeader(http.StatusOK) | |
return json.NewEncoder(w).Encode(obj) | |
} | |
// SendError will write error to the response | |
func SendError(code int, err error, w http.ResponseWriter) error { | |
w.WriteHeader(code) | |
w.Write([]byte(err.Error())) | |
return nil | |
} | |
// SendInternalError will send the internal server | |
// error code with the error string information | |
func SendInternalError(err error, w http.ResponseWriter) error { | |
return SendError(http.StatusInternalServerError, err, w) | |
} | |
// SendBadRequest will write the bad request error | |
// to the provided response writer | |
func SendBadRequest(err error, w http.ResponseWriter) error { | |
return SendError(http.StatusBadRequest, err, w) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'.source.go': | |
'Mongo controller': | |
'prefix': 'mgoco' | |
'body': """ | |
// $1Model is | |
type $1Model struct { | |
ID bson.ObjectId `json:"id" bson:"_id"` | |
} | |
// $1Controller is | |
type $1Controller struct { | |
Collection *mgo.Collection | |
} | |
// Get handles | |
func (c *$1Controller) Get(w http.ResponseWriter, r *http.Request) { | |
query := mux.Vars(r) | |
res := []$1Model{} | |
err := c.Collection.Find(query).All(&res) | |
if err != nil { | |
SendInternalError(err, w) | |
} else { | |
SendJSON(&res, w) | |
} | |
} | |
// Post handles | |
func (c *$1Controller) Post(w http.ResponseWriter, r *http.Request) { | |
model := $1Model{} | |
err := json.NewDecoder(r.Body).Decode(&model) | |
if err != nil { | |
SendBadRequest(err, w) | |
} else { | |
err = c.Collection.Insert(&model) | |
if err != nil { | |
SendInternalError(err, w) | |
} else { | |
SendJSON(&model, w) | |
} | |
} | |
} | |
// Put handles | |
func (c *$1Controller) Put(w http.ResponseWriter, r *http.Request) { | |
query := mux.Vars(r) | |
model := $1Model{} | |
err := json.NewDecoder(r.Body).Decode(&model) | |
if err != nil { | |
SendBadRequest(err, w) | |
} else { | |
_, err = c.Collection.UpdateAll(query, model) | |
if err != nil { | |
SendInternalError(err, w) | |
return | |
} | |
res := []$1Model{} | |
err = c.Collection.Find(query).All(&res) | |
if err != nil { | |
SendInternalError(err, w) | |
return | |
} | |
SendJSON(&res, w) | |
} | |
} | |
// Delete handles | |
func (c *$1Controller) Delete(w http.ResponseWriter, r *http.Request) { | |
query := mux.Vars(r) | |
info, err := c.Collection.RemoveAll(query) | |
if err != nil { | |
SendInternalError(err, w) | |
} else { | |
SendJSON(info, w) | |
} | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment