Commit 6ec91280b2993e3b3731c71a7d2a762f41f189eb

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

working on documentation

... ... @@ -16,18 +16,20 @@ const saltSize = 32
16 16 const appName = "korisnicki-centar"
17 17 const secret = "korisnicki-centar-api"
18 18  
  19 +// TokenClaims are JWT token claims.
19 20 type TokenClaims struct {
20 21 Username string `json:"username"`
21 22 Role string `json:"role"`
22 23 jwt.StandardClaims
23 24 }
24 25  
  26 +// CredentialsStruct is an instace of username/password values.
25 27 type CredentialsStruct struct {
26 28 Username string `json:"username"`
27 29 Password string `json:"password"`
28 30 }
29 31  
30   -// generateSalt returns a random string of 'saltSize' length to be used for hashing.
  32 +// generateSalt returns a string of random characters of 'saltSize' length.
31 33 func generateSalt() (salt string, err error) {
32 34 rawsalt := make([]byte, saltSize)
33 35  
... ... @@ -42,6 +44,7 @@ func generateSalt() (salt string, err error) {
42 44  
43 45 // HashString hashes input string with SHA256 algorithm.
44 46 // If the presalt parameter is not provided HashString will generate new salt string.
  47 +// Returns hash and salt string or an error if it fails.
45 48 func HashString(str string, presalt string) (hash, salt string, err error) {
46 49 // chech if message is presalted
47 50 if presalt == "" {
... ... @@ -73,8 +76,8 @@ func HashString(str string, presalt string) (hash, salt string, err error) {
73 76 return hash, salt, nil
74 77 }
75 78  
76   -// CreateAPIToken creates JWT token encoding username, role,
77   -// expiration date and issuer claims in it.
  79 +// CreateAPIToken returns JWT token with encoded username, role, expiration date and issuer claims.
  80 +// It returns an error if it fails.
78 81 func CreateAPIToken(username, role string) (string, error) {
79 82 var apiToken string
80 83 var err error
... ... @@ -100,7 +103,8 @@ func CreateAPIToken(username, role string) (string, error) {
100 103 return apiToken, nil
101 104 }
102 105  
103   -// RefreshAPIToken prolongs JWT token's expiration date.
  106 +// RefreshAPIToken prolongs JWT token's expiration date for one week.
  107 +// It returns new JWT token or an error if it fails.
104 108 func RefreshAPIToken(tokenString string) (string, error) {
105 109 var newToken string
106 110 tokenString = strings.TrimPrefix(tokenString, "Bearer ")
... ... @@ -127,6 +131,7 @@ func RefreshAPIToken(tokenString string) (string, error) {
127 131 }
128 132  
129 133 // ParseAPIToken parses JWT token claims.
  134 +// It returns a pointer to TokenClaims struct or an error if it fails.
130 135 func ParseAPIToken(tokenString string) (*TokenClaims, error) {
131 136 if ok := strings.HasPrefix(tokenString, "Bearer "); ok {
132 137 tokenString = strings.TrimPrefix(tokenString, "Bearer ")
... ... @@ -147,7 +152,7 @@ func ParseAPIToken(tokenString string) (*TokenClaims, error) {
147 152 return claims, nil
148 153 }
149 154  
150   -// secretFunc returns byte slice of 'secret'.
  155 +// secretFunc returns byte slice of API secret keyword.
151 156 func secretFunc(token *jwt.Token) (interface{}, error) {
152 157 return []byte(secret), nil
153 158 }
... ...
... ... @@ -11,7 +11,7 @@ import (
11 11 )
12 12  
13 13 var mu = &sync.Mutex{}
14   -var allPayloads []payloadBuff
  14 +var payloads []payloadBuff
15 15  
16 16 type LangMap map[string]map[string]string
17 17  
... ... @@ -41,6 +41,7 @@ type payloadBuff struct {
41 41 Fields []Field `json:"fields"`
42 42 Correlations []CorrelationField `json:"correlationFields"`
43 43 IdField string `json:"idField"`
  44 +
44 45 // Data can only hold slices of any type. It can't be used for itteration
45 46 Data interface{} `json:"data"`
46 47 }
... ... @@ -57,35 +58,34 @@ type Payload struct {
57 58 Data interface{} `json:"data"`
58 59 }
59 60  
60   -// NewPayload returs payload for provided table.
  61 +// NewPayload returs a payload sceleton for provided table.
61 62 func NewPayload(r *http.Request, table string) Payload {
62 63 var pload Payload
63 64  
64 65 pload.Method = r.Method + " " + r.URL.Path
65 66 if table != "" {
66 67 pload.Params = make(map[string]string, 0)
67   - pload.Lang = loadTranslations(table)
68   - pload.Fields = loadFields(table)
69   - pload.IdField = loadIdField(table)
70   - pload.Correlations = loadCorrelations(table)
  68 + pload.Lang = translations(table)
  69 + pload.Fields = fields(table)
  70 + pload.IdField = id(table)
  71 + pload.Correlations = correlations(table)
71 72 }
72 73 return pload
73 74 }
74 75  
75   -// DeliverPayload writes payload to w.
  76 +// DeliverPayload encodes payload to w.
76 77 func DeliverPayload(w http.ResponseWriter, payload Payload) {
77 78 json.NewEncoder(w).Encode(payload)
78 79 payload.Data = nil
79 80 }
80 81  
81   -// loadTranslations loads translations for a payload of the given data type.
82   -func loadTranslations(id string) []Translation {
83   - translations := make([]Translation, 0)
  82 +// translations returns a slice of translations for a payload/table of ptype type.
  83 +func translations(ptype string) []Translation {
  84 + var translations []Translation
84 85  
85   - for _, pload := range allPayloads {
86   - if pload.Type == id {
  86 + for _, pload := range payloads {
  87 + if pload.Type == ptype {
87 88 for _, t := range pload.Lang {
88   - //translations[t.Language] = t.FieldsLabels
89 89 translations = append(translations, Translation{
90 90 Language: t.Language,
91 91 FieldsLabels: t.FieldsLabels,
... ... @@ -97,12 +97,12 @@ func loadTranslations(id string) []Translation {
97 97 return translations
98 98 }
99 99  
100   -// loadFields loads fields for a payload of the given data type.
101   -func loadFields(id string) []Field {
102   - fields := make([]Field, 0)
  100 +// fields returns a slice of fields for a payload/table of ptype type.
  101 +func fields(ptype string) []Field {
  102 + var fields []Field
103 103  
104   - for _, pload := range allPayloads {
105   - if pload.Type == id{
  104 + for _, pload := range payloads {
  105 + if pload.Type == ptype {
106 106 for _, f := range pload.Fields {
107 107 fields = append(fields, f)
108 108 }
... ... @@ -112,44 +112,46 @@ func loadFields(id string) []Field {
112 112 return fields
113 113 }
114 114  
115   -// loadIdField loads ID field for a payload of the given data type.
116   -func loadIdField(id string) string {
117   - for _, pload := range allPayloads {
118   - if pload.Type == id {
  115 +// id returns the name of ID field of a payload/table of ptype type.
  116 +func id(ptype string) string {
  117 + for _, pload := range payloads {
  118 + if pload.Type == ptype {
119 119 return pload.IdField
120 120 }
121 121 }
122 122 return ""
123 123 }
124 124  
125   -// loadCorrelations loads correlations field for a payload of the given data type.
126   -func loadCorrelations(id string) []CorrelationField {
127   - resp := make([]CorrelationField, 0)
  125 +// correlations returns a slice of correlation fields for a payload/table of ptype type.
  126 +func correlations(ptype string) []CorrelationField {
  127 + var corr []CorrelationField
128 128  
129   - for _, pload := range allPayloads {
130   - if pload.Type == id {
131   - for _, f := range pload.Correlations {
132   - resp = append(resp, f)
  129 + for _, pload := range payloads {
  130 + if pload.Type == ptype {
  131 + for _, c := range pload.Correlations {
  132 + corr = append(corr, c)
133 133 }
134 134 }
135 135 }
136 136  
137   - return resp
  137 + return corr
138 138 }
139 139  
140   -// InitTables loads all payloads in the memory.
  140 +// InitTables loads all payloads in the payloads variable.
  141 +// Returns an error if it fails.
141 142 func InitTables(db *ora.Ses, project string) error {
142 143 jsonbuf, _ := fetchJSON(db, EqualQuotes(project))
143 144 mu.Lock()
144 145 defer mu.Unlock()
145   - json.Unmarshal(jsonbuf, &allPayloads)
146   - if len(allPayloads) == 0 {
  146 + json.Unmarshal(jsonbuf, &payloads)
  147 + if len(payloads) == 0 {
147 148 return errors.New("tables config is corrupt")
148 149 }
149 150 return nil
150 151 }
151 152  
152   -// fetchJSON fetches JSON configuration file from TABLES_CONFIG table.
  153 +// fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
  154 +// Returns an error if it fails.
153 155 func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
154 156 stmt, err := db.Prep(`SELECT
155 157 JSON_CLOB
... ... @@ -180,8 +182,8 @@ func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
180 182 return bytes, nil
181 183 }
182 184  
183   -// DecodeJSON decodes JSON data from r to v and returns any error
184   -// that happens during decoding process.
  185 +// DecodeJSON decodes JSON data from r to v.
  186 +// Returns an error if it fails.
185 187 func DecodeJSON(r io.Reader, v interface{}) error {
186 188 return json.NewDecoder(r).Decode(v)
187 189 }
... ...
1 1 package webutility
2 2  
3   -import (
4   - "gopkg.in/rana/ora.v3"
5   -)
  3 +import "gopkg.in/rana/ora.v3"
6 4  
7 5 type ListOptions struct {
8 6 GlobalFilter bool `json:"globalFilter"`
... ... @@ -93,7 +91,8 @@ type ListConfig struct {
93 91 Details ListDetails `json:"details"`
94 92 }
95 93  
96   -// GetListConfig returns list configuration for the given object type for the front-end application.
  94 +// GetListConfig returns list configuration for the provided object type for the front-end application
  95 +// or an error if it fails.
97 96 func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
98 97 resp := newDefaultList(objType)
99 98 var err error
... ... @@ -115,7 +114,8 @@ func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
115 114 return resp, nil
116 115 }
117 116  
118   -// GetListConfigObjectIDField returns ID field for the given object type.
  117 +// GetListConfigObjectIDField takes in database connection and an object type and it returns the
  118 +// ID field name for the provided object type.
119 119 func GetListConfigObjectIDField(db *ora.Ses, otype string) string {
120 120 var resp string
121 121 var err error
... ... @@ -145,7 +145,7 @@ func GetListConfigObjectIDField(db *ora.Ses, otype string) string {
145 145 return resp
146 146 }
147 147  
148   -// newDefaultList returns default configuration for the given object type.
  148 +// newDefaultList returns default configuration for the provided object type.
149 149 func newDefaultList(objType string) ListConfig {
150 150 list := ListConfig{
151 151 ObjectType: objType,
... ... @@ -174,7 +174,7 @@ func newDefaultList(objType string) ListConfig {
174 174 return list
175 175 }
176 176  
177   -// setListParams sets the default parameters of the provided configuration list for the given object type.
  177 +// setListParams sets the default parameters of the provided configuration list for the provided object type.
178 178 func setListParams(db *ora.Ses, list *ListConfig, objType string) error {
179 179 var err error
180 180 var stmt *ora.Stmt
... ... @@ -212,7 +212,7 @@ func setListParams(db *ora.Ses, list *ListConfig, objType string) error {
212 212 return nil
213 213 }
214 214  
215   -// getListNavigation returns list navigation node slice for the given objectType.
  215 +// getListNavigation returns list navigation node slice for the provided objectType.
216 216 func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) {
217 217 resp := make([]ListNavNode, 0)
218 218 var err error
... ... @@ -250,7 +250,7 @@ func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) {
250 250 return resp, nil
251 251 }
252 252  
253   -// getListActions returns list actions for the given objectType.
  253 +// getListActions returns list actions for the provided object type.
254 254 func getListActions(db *ora.Ses, objType string) (ListActions, error) {
255 255 var resp ListActions
256 256 var err error
... ... @@ -286,7 +286,7 @@ func getListActions(db *ora.Ses, objType string) (ListActions, error) {
286 286 return resp, nil
287 287 }
288 288  
289   -// getListFiters returns list filter slice for the given object type.
  289 +// getListFiters returns list filter slice for the provided object type.
290 290 func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) {
291 291 resp := make([]ListFilter, 0)
292 292 filtersFields, err := getFilterFieldsAndPosition(db, objType)
... ... @@ -353,7 +353,7 @@ type _filter struct {
353 353 Type string
354 354 }
355 355  
356   -// getFiltersByFilterField returns filter slice for the given filter field.
  356 +// getFiltersByFilterField returns filter slice for the provided filter field.
357 357 func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error) {
358 358 resp := make([]_filter, 0)
359 359 var err error
... ... @@ -386,7 +386,7 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error
386 386 return resp, nil
387 387 }
388 388  
389   -// getFilterDropdownConfig returns dropdown menu for the given filter field.
  389 +// getFilterDropdownConfig returns dropdown menu for the provided filter field.
390 390 func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) {
391 391 var resp Dropdown
392 392 var err error
... ... @@ -435,7 +435,7 @@ func sortFilters(filters []ListFilter) {
435 435 }
436 436 }
437 437  
438   -// getListGraph return list graph slice for the given object type.
  438 +// getListGraph return list graph slice for the provided object type.
439 439 func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
440 440 resp := make([]ListGraph, 0)
441 441 var err error
... ... @@ -470,7 +470,7 @@ func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
470 470 return resp, nil
471 471 }
472 472  
473   -// getListOptions returns list options for the given object type.
  473 +// getListOptions returns list options for the provided object type.
474 474 func getListOptions(db *ora.Ses, objType string) (ListOptions, error) {
475 475 var resp ListOptions
476 476 var err error
... ... @@ -508,7 +508,7 @@ func getListOptions(db *ora.Ses, objType string) (ListOptions, error) {
508 508 return resp, nil
509 509 }
510 510  
511   -// getListParent returns list parent node slice for the given object type.
  511 +// getListParent returns list parent node slice for the provided object type.
512 512 func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) {
513 513 resp := make([]ListParentNode, 0)
514 514 var err error
... ... @@ -542,7 +542,7 @@ func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) {
542 542 return resp, nil
543 543 }
544 544  
545   -// getListPivot list pivot slice for the given object type.
  545 +// getListPivot list pivot slice for the provided object type.
546 546 func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) {
547 547 resp := make([]ListPivot, 0)
548 548 var err error
... ... @@ -577,7 +577,7 @@ func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) {
577 577 return resp, nil
578 578 }
579 579  
580   -// getListDetails returns list details for the given object type.
  580 +// getListDetails returns list details for the provided object type.
581 581 func getListDetails(db *ora.Ses, objType string) (ListDetails, error) {
582 582 var resp ListDetails
583 583 var err error
... ...