From 4a51e54d7e426f07551271cb1a96f69a991992c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Fri, 23 Sep 2016 16:03:21 +0200 Subject: [PATCH] simplified --- http_utility.go | 31 ++++++++++++++++++++----------- json_utility.go | 48 +++++++++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/http_utility.go b/http_utility.go index be45aa2..d810348 100644 --- a/http_utility.go +++ b/http_utility.go @@ -5,13 +5,18 @@ import ( "encoding/json" ) -var _apiVersion = "/api/v1" +var _apiVersion = "" +var _authEndPoint = "" func SetApiVersion(ver string) string { _apiVersion = ver return _apiVersion } +func SetAuthEndpoint(ep string) { + _authEndPoint = ep +} + //// //// ERROR UTILITY //// @@ -64,7 +69,7 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { //TODO: Add parameters to enable/disable token and roles authorization checks // Sets common headers and checks for token validity. -func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc { +func ProcessHeaders(fn http.HandlerFunc, shouldAuth bool) 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") @@ -93,15 +98,17 @@ func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc { return } - if req.URL.Path != _apiVersion + "/token/new" { - token := req.Header.Get("Authorization") - if _, err := ParseAPIToken(token); err != nil { - RespondWithHttpError(w, req, http.StatusUnauthorized, - []HttpErrorDesc{ - {Lang: "en", Desc: "Unauthorized request."}, - {Lang: "rs", Desc: "Neautorizovani zahtev."}, - }) - return + if shouldAuth { + if req.URL.Path != _apiVersion + _authEndPoint { + token := req.Header.Get("Authorization") + if _, err := ParseAPIToken(token); err != nil { + RespondWithHttpError(w, req, http.StatusUnauthorized, + []HttpErrorDesc{ + {Lang: "en", Desc: "Unauthorized request."}, + {Lang: "rs", Desc: "Neautorizovani zahtev."}, + }) + return + } } } @@ -114,6 +121,8 @@ func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc { }) return } + + // execute HandlerFunc fn(w, req) } } diff --git a/json_utility.go b/json_utility.go index 3e5ef5f..6372ce6 100644 --- a/json_utility.go +++ b/json_utility.go @@ -2,16 +2,14 @@ package restutility import ( "net/http" - "strings" "encoding/json" "errors" "gopkg.in/rana/ora.v3" "io" "io/ioutil" - "fmt" ) -var allPayloads []Payload +var allPayloads []payloadBuff type LangMap map[string]map[string]string @@ -33,7 +31,7 @@ type Translation struct { FieldsLabels map[string]string `json:"fieldsLabels"` } -type Payload struct { +type payloadBuff struct { Type string `json:"tableType"` Method string `json:"method"` Params map[string]string `json:"params"` @@ -45,15 +43,26 @@ type Payload struct { Data interface{} `json:"data"` } +type Payload struct { + Method string `json:"method"` + Params map[string]string `json:"params"` + Lang []Translation `json:"lang"` + Fields []Field `json:"fields"` + Correlations []CorrelationField `json:"correlationFields"` + IdField string `json:"idField"` + // Data can only hold slices of any type. It can't be used for itteration + Data interface{} `json:"data"` +} + func NewPayload(r *http.Request, table string) Payload { var pload Payload - pload.Method = strings.ToLower(r.Method + " " + r.URL.Path) + pload.Method = r.Method + " " + r.URL.Path pload.Params = make(map[string]string, 0) - pload.Lang = loadTranslations(allPayloads, table) - pload.Fields = loadFields(allPayloads, table) - pload.IdField = loadIdField(allPayloads, table) - pload.Correlations = loadCorrelations(allPayloads, table) + pload.Lang = loadTranslations(table) + pload.Fields = loadFields(table) + pload.IdField = loadIdField(table) + pload.Correlations = loadCorrelations(table) return pload } @@ -63,10 +72,10 @@ func DeliverPayload(w http.ResponseWriter, payload Payload) { payload.Data = nil } -func loadTranslations(payloads []Payload, id string) []Translation { +func loadTranslations(id string) []Translation { translations := make([]Translation, 0) - for _, pload := range payloads { + for _, pload := range allPayloads { if pload.Type == id { for _, t := range pload.Lang { //translations[t.Language] = t.FieldsLabels @@ -81,10 +90,10 @@ func loadTranslations(payloads []Payload, id string) []Translation { return translations } -func loadFields(payloads []Payload, id string) []Field { +func loadFields(id string) []Field { fields := make([]Field, 0) - for _, pload := range payloads { + for _, pload := range allPayloads { if pload.Type == id{ for _, f := range pload.Fields { fields = append(fields, f) @@ -95,8 +104,8 @@ func loadFields(payloads []Payload, id string) []Field { return fields } -func loadIdField(payloads []Payload, id string) string { - for _, pload := range payloads { +func loadIdField(id string) string { + for _, pload := range allPayloads { if pload.Type == id { return pload.IdField } @@ -104,10 +113,10 @@ func loadIdField(payloads []Payload, id string) string { return "" } -func loadCorrelations(payloads []Payload, id string) []CorrelationField { +func loadCorrelations(id string) []CorrelationField { resp := make([]CorrelationField, 0) - for _, pload := range payloads { + for _, pload := range allPayloads { if pload.Type == id { for _, f := range pload.Correlations { resp = append(resp, f) @@ -124,7 +133,6 @@ func InitTables(db *ora.Ses, project string) error { if len(allPayloads) == 0 { return errors.New("tables config is corrupt") } - fmt.Printf("broj ucitanih tabela: %d\n", len(allPayloads)) return nil } @@ -149,7 +157,9 @@ func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) { lob := rset.Row[0].(io.Reader) bytes, err = ioutil.ReadAll(lob) if err != nil { - fmt.Printf("mega error: %v\n", err) + // TODO: Find a fix for this. + // Ignore, it's some weird streaming read/write LOB error. + //return nil, err } } -- 1.8.1.2