From 25e0015504e59050a2b2e299e6bb89ad2522282f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 21 Sep 2016 15:05:03 +0200 Subject: [PATCH] exported everything --- format_utility.go | 30 +++--------------- http_utility.go | 92 +++++++++++++++++++------------------------------------ tables_utility.go | 12 +++----- 3 files changed, 41 insertions(+), 93 deletions(-) diff --git a/format_utility.go b/format_utility.go index bcfefa2..0ae8d9d 100644 --- a/format_utility.go +++ b/format_utility.go @@ -6,15 +6,11 @@ import ( "strconv" ) -//// -//// TIME FORMAT UTILITY -//// - -func unixToDate(input int64) time.Time { +func UnixToDate(input int64) time.Time { return time.Unix(input, 0) } -func dateToUnix(input interface{}) int64 { +func DateToUnix(input interface{}) int64 { if input != nil { t := input.(time.Time) return t.Unix() @@ -23,32 +19,14 @@ func dateToUnix(input interface{}) int64 { return 0 } -func aersDate(unixString string) (string, error) { - unixTime, err := strconv.ParseInt(unixString, 10, 64) - if err != nil { - return "", err - } - - date := unixToDate(unixTime).String() - tokens := strings.Split(date, "-") - dateString := tokens[0] + tokens[1] + strings.Split(tokens[2], " ")[0] - - return dateString, nil -} - -//// -//// STRING UTILITY -//// - -// surrondWithSingleQuotes is used when url param is of type string -func putQuotes(input string) string { +func EqualQuotes(input string) string { if input != "" { return " = '" + input + "'" } return "" } -func putLikeQuotes(input string) string { +func LikeQuotes(input string) string { if input != "" { return " LIKE UPPER('%" + input + "%')" } diff --git a/http_utility.go b/http_utility.go index ecc5baf..04b319d 100644 --- a/http_utility.go +++ b/http_utility.go @@ -5,7 +5,12 @@ import ( "encoding/json" ) -const APIVersion = "/api/v1" +var _apiVersion = "/api/v1" + +func SetApiVersion(ver string) string { + _apiVersion = ver + return _apiVeresion +} //// //// ERROR UTILITY @@ -26,7 +31,7 @@ type HttpErrorDesc struct { Desc string `json:"description"` } -func respondWithHttpError(w http.ResponseWriter, +func RespondWithHttpError(w http.ResponseWriter, req *http.Request, code int, httpErr []HttpErrorDesc) { @@ -39,35 +44,21 @@ func respondWithHttpError(w http.ResponseWriter, json.NewEncoder(w).Encode(err) } -func respondWithHttpError400(w http.ResponseWriter, req *http.Request) { - respondWithHttpError(w, req, http.StatusBadRequest, - []HttpErrorDesc{ - { - Lang: "en", - Desc: templateHttpErr400_EN, - }, - { - Lang: "rs", - Desc: templateHttpErr400_RS, - }, - }) +func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) { + RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ + {Lang: "en", Desc: templateHttpErr400_EN}, + {Lang: "rs", Desc: templateHttpErr400_RS}, + }) } -func respondWithHttpError500(w http.ResponseWriter, req *http.Request) { - respondWithHttpError(w, req, http.StatusInternalServerError, - []HttpErrorDesc{ - { - Lang: "en", - Desc: templateHttpErr500_EN, - }, - { - Lang: "rs", - Desc: templateHttpErr500_RS, - }, - }) +func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { + RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{ + {Lang: "en", Desc: templateHttpErr500_EN}, + {Lang: "rs", Desc: templateHttpErr500_RS}, + }) } -func deliverPayload(w http.ResponseWriter, payload JSONPayload) { +func DeliverPayload(w http.ResponseWriter, payload JSONPayload) { json.NewEncoder(w).Encode(payload) payload.Data = nil } @@ -78,7 +69,7 @@ func deliverPayload(w http.ResponseWriter, payload JSONPayload) { // wrapHandlerFunc is as wrapper function for route handlers. // Sets common headers and checks for token validity. -func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc { +func HandleFuncWrap(fn http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { // @TODO: check Content-type header (must be application/json) // ctype := w.Header.Get("Content-Type") @@ -107,19 +98,13 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc { return } - if req.URL.Path != APIVersion + "/token/new" { + if req.URL.Path != _apiVersion + "/token/new" { token := req.Header.Get("Authorization") - if _, err := parseAPIToken(token); err != nil { - respondWithHttpError(w, req, http.StatusUnauthorized, + if _, err := ParseAPIToken(token); err != nil { + RespondWithHttpError(w, req, http.StatusUnauthorized, []HttpErrorDesc{ - { - Lang: "en", - Desc: "Unauthorized request.", - }, - { - Lang: "rs", - Desc: "Neautorizovani zahtev.", - }, + {Lang: "en", Desc: "Unauthorized request."}, + {Lang: "rs", Desc: "Neautorizovani zahtev."}, }) return } @@ -127,16 +112,10 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc { err := req.ParseForm() if err != nil { - respondWithHttpError(w, req, http.StatusBadRequest, + RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ - { - Lang: "en", - Desc: templateHttpErr400_EN, - }, - { - Lang: "rs", - Desc: templateHttpErr400_RS, - }, + {Lang: "en", Desc: templateHttpErr400_EN}, + {Lang: "rs", Desc: templateHttpErr400_RS}, }) return } @@ -148,16 +127,9 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc { //// NOT FOUND HANDLER //// -func notFoundHandler(w http.ResponseWriter, req *http.Request) { - respondWithHttpError(w, req, http.StatusNotFound, - []HttpErrorDesc{ - { - Lang: "en", - Desc: "Not found.", - }, - { - Lang: "rs", - Desc: "Traženi resurs ne postoji.", - }, - }) +func NotFoundHandler(w http.ResponseWriter, req *http.Request) { + RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{ + {Lang: "en", Desc: "Not found."}, + {Lang: "rs", Desc: "Traženi resurs ne postoji."}, + }) } diff --git a/tables_utility.go b/tables_utility.go index 5074d70..748b6fb 100644 --- a/tables_utility.go +++ b/tables_utility.go @@ -82,20 +82,18 @@ func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { var _tables TableConfig var _prevProject string -func loadTablesConfig(jsonbuf []byte) error { +func InitTables(jsonbuf []byte) error { json.Unmarshal(jsonbuf, &_tables.Tables) - if len(_tables.Tables) == 0 { return errors.New("tables config is corrupt") } - return nil } -func loadTable(table string) JSONParams { +func LoadTable(table string) JSONParams { return NewJSONParams(_tables.LoadTranslations(table), - _tables.LoadFields(table), - _tables.LoadIdField(table), - _tables.LoadCorrelations(table)) + _tables.LoadFields(table), + _tables.LoadIdField(table), + _tables.LoadCorrelations(table)) } -- 1.8.1.2