Commit 4a51e54d7e426f07551271cb1a96f69a991992c8

Authored by Marko Tikvić
1 parent ecec68b180
Exists in master and in 1 other branch v2

simplified

Showing 2 changed files with 49 additions and 30 deletions   Show diff stats
... ... @@ -5,13 +5,18 @@ import (
5 5 "encoding/json"
6 6 )
7 7  
8   -var _apiVersion = "/api/v1"
  8 +var _apiVersion = ""
  9 +var _authEndPoint = ""
9 10  
10 11 func SetApiVersion(ver string) string {
11 12 _apiVersion = ver
12 13 return _apiVersion
13 14 }
14 15  
  16 +func SetAuthEndpoint(ep string) {
  17 + _authEndPoint = ep
  18 +}
  19 +
15 20 ////
16 21 //// ERROR UTILITY
17 22 ////
... ... @@ -64,7 +69,7 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
64 69  
65 70 //TODO: Add parameters to enable/disable token and roles authorization checks
66 71 // Sets common headers and checks for token validity.
67   -func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc {
  72 +func ProcessHeaders(fn http.HandlerFunc, shouldAuth bool) http.HandlerFunc {
68 73 return func(w http.ResponseWriter, req *http.Request) {
69 74 // @TODO: check Content-type header (must be application/json)
70 75 // ctype := w.Header.Get("Content-Type")
... ... @@ -93,15 +98,17 @@ func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc {
93 98 return
94 99 }
95 100  
96   - if req.URL.Path != _apiVersion + "/token/new" {
97   - token := req.Header.Get("Authorization")
98   - if _, err := ParseAPIToken(token); err != nil {
99   - RespondWithHttpError(w, req, http.StatusUnauthorized,
100   - []HttpErrorDesc{
101   - {Lang: "en", Desc: "Unauthorized request."},
102   - {Lang: "rs", Desc: "Neautorizovani zahtev."},
103   - })
104   - return
  101 + if shouldAuth {
  102 + if req.URL.Path != _apiVersion + _authEndPoint {
  103 + token := req.Header.Get("Authorization")
  104 + if _, err := ParseAPIToken(token); err != nil {
  105 + RespondWithHttpError(w, req, http.StatusUnauthorized,
  106 + []HttpErrorDesc{
  107 + {Lang: "en", Desc: "Unauthorized request."},
  108 + {Lang: "rs", Desc: "Neautorizovani zahtev."},
  109 + })
  110 + return
  111 + }
105 112 }
106 113 }
107 114  
... ... @@ -114,6 +121,8 @@ func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc {
114 121 })
115 122 return
116 123 }
  124 +
  125 + // execute HandlerFunc
117 126 fn(w, req)
118 127 }
119 128 }
... ...
... ... @@ -2,16 +2,14 @@ package restutility
2 2  
3 3 import (
4 4 "net/http"
5   - "strings"
6 5 "encoding/json"
7 6 "errors"
8 7 "gopkg.in/rana/ora.v3"
9 8 "io"
10 9 "io/ioutil"
11   - "fmt"
12 10 )
13 11  
14   -var allPayloads []Payload
  12 +var allPayloads []payloadBuff
15 13  
16 14 type LangMap map[string]map[string]string
17 15  
... ... @@ -33,7 +31,7 @@ type Translation struct {
33 31 FieldsLabels map[string]string `json:"fieldsLabels"`
34 32 }
35 33  
36   -type Payload struct {
  34 +type payloadBuff struct {
37 35 Type string `json:"tableType"`
38 36 Method string `json:"method"`
39 37 Params map[string]string `json:"params"`
... ... @@ -45,15 +43,26 @@ type Payload struct {
45 43 Data interface{} `json:"data"`
46 44 }
47 45  
  46 +type Payload struct {
  47 + Method string `json:"method"`
  48 + Params map[string]string `json:"params"`
  49 + Lang []Translation `json:"lang"`
  50 + Fields []Field `json:"fields"`
  51 + Correlations []CorrelationField `json:"correlationFields"`
  52 + IdField string `json:"idField"`
  53 + // Data can only hold slices of any type. It can't be used for itteration
  54 + Data interface{} `json:"data"`
  55 +}
  56 +
48 57 func NewPayload(r *http.Request, table string) Payload {
49 58 var pload Payload
50 59  
51   - pload.Method = strings.ToLower(r.Method + " " + r.URL.Path)
  60 + pload.Method = r.Method + " " + r.URL.Path
52 61 pload.Params = make(map[string]string, 0)
53   - pload.Lang = loadTranslations(allPayloads, table)
54   - pload.Fields = loadFields(allPayloads, table)
55   - pload.IdField = loadIdField(allPayloads, table)
56   - pload.Correlations = loadCorrelations(allPayloads, table)
  62 + pload.Lang = loadTranslations(table)
  63 + pload.Fields = loadFields(table)
  64 + pload.IdField = loadIdField(table)
  65 + pload.Correlations = loadCorrelations(table)
57 66  
58 67 return pload
59 68 }
... ... @@ -63,10 +72,10 @@ func DeliverPayload(w http.ResponseWriter, payload Payload) {
63 72 payload.Data = nil
64 73 }
65 74  
66   -func loadTranslations(payloads []Payload, id string) []Translation {
  75 +func loadTranslations(id string) []Translation {
67 76 translations := make([]Translation, 0)
68 77  
69   - for _, pload := range payloads {
  78 + for _, pload := range allPayloads {
70 79 if pload.Type == id {
71 80 for _, t := range pload.Lang {
72 81 //translations[t.Language] = t.FieldsLabels
... ... @@ -81,10 +90,10 @@ func loadTranslations(payloads []Payload, id string) []Translation {
81 90 return translations
82 91 }
83 92  
84   -func loadFields(payloads []Payload, id string) []Field {
  93 +func loadFields(id string) []Field {
85 94 fields := make([]Field, 0)
86 95  
87   - for _, pload := range payloads {
  96 + for _, pload := range allPayloads {
88 97 if pload.Type == id{
89 98 for _, f := range pload.Fields {
90 99 fields = append(fields, f)
... ... @@ -95,8 +104,8 @@ func loadFields(payloads []Payload, id string) []Field {
95 104 return fields
96 105 }
97 106  
98   -func loadIdField(payloads []Payload, id string) string {
99   - for _, pload := range payloads {
  107 +func loadIdField(id string) string {
  108 + for _, pload := range allPayloads {
100 109 if pload.Type == id {
101 110 return pload.IdField
102 111 }
... ... @@ -104,10 +113,10 @@ func loadIdField(payloads []Payload, id string) string {
104 113 return ""
105 114 }
106 115  
107   -func loadCorrelations(payloads []Payload, id string) []CorrelationField {
  116 +func loadCorrelations(id string) []CorrelationField {
108 117 resp := make([]CorrelationField, 0)
109 118  
110   - for _, pload := range payloads {
  119 + for _, pload := range allPayloads {
111 120 if pload.Type == id {
112 121 for _, f := range pload.Correlations {
113 122 resp = append(resp, f)
... ... @@ -124,7 +133,6 @@ func InitTables(db *ora.Ses, project string) error {
124 133 if len(allPayloads) == 0 {
125 134 return errors.New("tables config is corrupt")
126 135 }
127   - fmt.Printf("broj ucitanih tabela: %d\n", len(allPayloads))
128 136 return nil
129 137 }
130 138  
... ... @@ -149,7 +157,9 @@ func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) {
149 157 lob := rset.Row[0].(io.Reader)
150 158 bytes, err = ioutil.ReadAll(lob)
151 159 if err != nil {
152   - fmt.Printf("mega error: %v\n", err)
  160 + // TODO: Find a fix for this.
  161 + // Ignore, it's some weird streaming read/write LOB error.
  162 + //return nil, err
153 163 }
154 164 }
155 165  
... ...