github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/documize/api/endpoint/init.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package endpoint 13 14 import ( 15 "fmt" 16 "net/http" 17 18 "github.com/documize/community/documize/api/request" 19 "github.com/documize/community/documize/api/store" 20 "github.com/documize/community/wordsmith/log" 21 ) 22 23 var storageProvider store.StorageProvider 24 25 func init() { 26 storageProvider = new(store.LocalStorageProvider) 27 } 28 29 //getAppURL returns full HTTP url for the app 30 func getAppURL(c request.Context, endpoint string) string { 31 32 scheme := "http://" 33 34 if c.SSL { 35 scheme = "https://" 36 } 37 38 return fmt.Sprintf("%s%s/%s", scheme, c.AppURL, endpoint) 39 } 40 41 func writePayloadError(w http.ResponseWriter, method string, err error) { 42 w.Header().Set("Content-Type", "application/json; charset=utf-8") 43 w.WriteHeader(http.StatusBadRequest) 44 _, err2 := w.Write([]byte("{Error: 'Bad payload'}")) 45 log.IfErr(err2) 46 log.Error(fmt.Sprintf("Unable to decode HTML request body for method %s", method), err) 47 } 48 49 func writeTransactionError(w http.ResponseWriter, method string, err error) { 50 w.Header().Set("Content-Type", "application/json; charset=utf-8") 51 w.WriteHeader(http.StatusInternalServerError) 52 _, err2 := w.Write([]byte("{Error: 'No transaction'}")) 53 log.IfErr(err2) 54 log.Error(fmt.Sprintf("Unable to get database transaction for method %s", method), err) 55 } 56 57 /* 58 func WriteAddRecordError(w http.ResponseWriter, method string, err error) { 59 w.Header().Set("Content-Type", "application/json; charset=utf-8") 60 w.WriteHeader(http.StatusInternalServerError) 61 _, err2 := w.Write([]byte("{Error: 'Add error'}")) 62 log.IfErr(err2) 63 log.Error(fmt.Sprintf("Unable to insert new database record for method %s", method), err) 64 } 65 66 func WriteGetRecordError(w http.ResponseWriter, method, entity string, err error) { 67 w.Header().Set("Content-Type", "application/json; charset=utf-8") 68 w.WriteHeader(http.StatusInternalServerError) 69 _, err2 := w.Write([]byte("{Error: 'Get error'}")) 70 log.IfErr(err2) 71 log.Error(fmt.Sprintf("Unable to get %s record for method %s", entity, method), err) 72 } 73 74 func WriteUpdateRecordError(w http.ResponseWriter, method string, id string, err error) { 75 w.Header().Set("Content-Type", "application/json; charset=utf-8") 76 w.WriteHeader(http.StatusInternalServerError) 77 _, err2 := w.Write([]byte("{Error: 'Add error'}")) 78 log.IfErr(err2) 79 log.Error(fmt.Sprintf("Unable to update database record ID %s for method %s", id, method), err) 80 } 81 82 func WriteParameterParsingError(w http.ResponseWriter, method, parameter string, err error) { 83 w.Header().Set("Content-Type", "application/json; charset=utf-8") 84 w.WriteHeader(http.StatusBadRequest) 85 _, err2 := w.Write([]byte("{Error: 'Bad parameter'}")) 86 log.IfErr(err2) 87 log.Error(fmt.Sprintf("Unable to parse API parameter %s for method %s", parameter, method), err) 88 } 89 */ 90 91 func writeMissingDataError(w http.ResponseWriter, method, parameter string) { 92 w.Header().Set("Content-Type", "application/json; charset=utf-8") 93 w.WriteHeader(http.StatusBadRequest) 94 _, err := w.Write([]byte("{Error: 'Missing data'}")) 95 log.IfErr(err) 96 log.Info(fmt.Sprintf("Missing data %s for method %s", parameter, method)) 97 } 98 99 func writeNotFoundError(w http.ResponseWriter, method string, id string) { 100 w.Header().Set("Content-Type", "application/json; charset=utf-8") 101 w.WriteHeader(http.StatusNotFound) 102 _, err := w.Write([]byte("{Error: 'Not found'}")) 103 log.IfErr(err) 104 log.Info(fmt.Sprintf("Not found ID %s for method %s", id, method)) 105 } 106 107 func writeGeneralSQLError(w http.ResponseWriter, method string, err error) { 108 w.Header().Set("Content-Type", "application/json; charset=utf-8") 109 w.WriteHeader(http.StatusBadRequest) 110 _, err2 := w.Write([]byte("{Error: 'SQL error'}")) 111 log.IfErr(err2) 112 log.Error(fmt.Sprintf("General SQL error for method %s", method), err) 113 } 114 115 func writeJSONMarshalError(w http.ResponseWriter, method, entity string, err error) { 116 w.Header().Set("Content-Type", "application/json; charset=utf-8") 117 w.WriteHeader(http.StatusBadRequest) 118 _, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}")) 119 log.IfErr(err2) 120 log.Error(fmt.Sprintf("Failed to JSON marshal %s for method %s", entity, method), err) 121 } 122 123 func writeServerError(w http.ResponseWriter, method string, err error) { 124 w.Header().Set("Content-Type", "application/json; charset=utf-8") 125 w.WriteHeader(http.StatusBadRequest) 126 _, err2 := w.Write([]byte("{Error: 'Internal server error'}")) 127 log.IfErr(err2) 128 log.Error(fmt.Sprintf("Internal server error for method %s", method), err) 129 } 130 131 func writeDuplicateError(w http.ResponseWriter, method, entity string) { 132 w.Header().Set("Content-Type", "application/json; charset=utf-8") 133 w.WriteHeader(http.StatusConflict) 134 _, err := w.Write([]byte("{Error: 'Duplicate record'}")) 135 log.IfErr(err) 136 log.Info(fmt.Sprintf("Duplicate %s record detected for method %s", entity, method)) 137 } 138 139 func writeUnauthorizedError(w http.ResponseWriter) { 140 w.Header().Set("Content-Type", "application/json; charset=utf-8") 141 w.WriteHeader(http.StatusUnauthorized) 142 _, err := w.Write([]byte("{Error: 'Unauthorized'}")) 143 log.IfErr(err) 144 } 145 146 func writeForbiddenError(w http.ResponseWriter) { 147 w.Header().Set("Content-Type", "application/json; charset=utf-8") 148 w.WriteHeader(http.StatusForbidden) 149 _, err := w.Write([]byte("{Error: 'Forbidden'}")) 150 log.IfErr(err) 151 } 152 153 func writeBadRequestError(w http.ResponseWriter, method, message string) { 154 w.Header().Set("Content-Type", "application/json; charset=utf-8") 155 w.WriteHeader(http.StatusBadRequest) 156 _, err := w.Write([]byte("{Error: 'Bad Request'}")) 157 log.IfErr(err) 158 log.Info(fmt.Sprintf("Bad Request %s for method %s", message, method)) 159 } 160 161 func writeSuccessBytes(w http.ResponseWriter, data []byte) { 162 w.Header().Set("Content-Type", "application/json; charset=utf-8") 163 w.WriteHeader(http.StatusOK) 164 _, err := w.Write(data) 165 log.IfErr(err) 166 } 167 168 func writeSuccessString(w http.ResponseWriter, data string) { 169 w.Header().Set("Content-Type", "application/json; charset=utf-8") 170 w.WriteHeader(http.StatusOK) 171 _, err := w.Write([]byte(data)) 172 log.IfErr(err) 173 } 174 175 func writeSuccessEmptyJSON(w http.ResponseWriter) { 176 w.Header().Set("Content-Type", "application/json; charset=utf-8") 177 w.WriteHeader(http.StatusOK) 178 _, err := w.Write([]byte("{}")) 179 log.IfErr(err) 180 }