this repo has no description

add GET /settings/keys

Akshay 17f2f709 72c27d77

Changed files
+58 -8
legit
db
routes
templates
+2
legit/db/init.go
··· 22 22 did text not null, 23 23 name text not null, 24 24 key text not null, 25 + created timestamp default current_timestamp, 25 26 unique(did, name, key) 26 27 ); 27 28 create table if not exists repos ( ··· 29 30 did text not null, 30 31 name text not null, 31 32 description text not null, 33 + created timestamp default current_timestamp, 32 34 unique(did, name) 33 35 ) 34 36 `)
+28 -6
legit/db/pubkeys.go
··· 1 1 package db 2 2 3 + import "time" 4 + 3 5 func (d *DB) AddPublicKey(did, name, key string) error { 4 6 query := `insert into public_keys (did, name, key) values (?, ?, ?)` 5 7 _, err := d.db.Exec(query, did, name, key) ··· 12 14 return err 13 15 } 14 16 15 - func (d *DB) GetPublicKey(did string) (string, error) { 16 - var key string 17 - query := `select key from public_keys where did = ?` 18 - err := d.db.QueryRow(query, did).Scan(&key) 17 + type PublicKey struct { 18 + Key string 19 + Name string 20 + Created time.Time 21 + } 22 + 23 + func (d *DB) GetPublicKeys(did string) ([]PublicKey, error) { 24 + var keys []PublicKey 25 + 26 + rows, err := d.db.Query(`select key, name, created from public_keys where did = ?`, did) 19 27 if err != nil { 20 - return "", err 28 + return nil, err 21 29 } 22 - return key, nil 30 + defer rows.Close() 31 + 32 + for rows.Next() { 33 + var publicKey PublicKey 34 + if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil { 35 + return nil, err 36 + } 37 + keys = append(keys, publicKey) 38 + } 39 + 40 + if err := rows.Err(); err != nil { 41 + return nil, err 42 + } 43 + 44 + return keys, nil 23 45 }
+10 -2
legit/routes/routes.go
··· 445 445 func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) { 446 446 switch r.Method { 447 447 case http.MethodGet: 448 - // TODO: fetch keys from db 449 - if err := h.t.ExecuteTemplate(w, "keys", nil); err != nil { 448 + keys, err := h.db.GetPublicKeys("did:ashtntnashtx") 449 + if err != nil { 450 + log.Println(err) 451 + http.Error(w, "invalid `did`", http.StatusBadRequest) 452 + return 453 + } 454 + 455 + data := make(map[string]interface{}) 456 + data["keys"] = keys 457 + if err := h.t.ExecuteTemplate(w, "keys", data); err != nil { 450 458 log.Println(err) 451 459 return 452 460 }
+18
legit/templates/keys.html
··· 29 29 Submit 30 30 </button> 31 31 </form> 32 + <table> 33 + <thead> 34 + <tr> 35 + <th>Key</th> 36 + <th>Name</th> 37 + <th>Created</th> 38 + </tr> 39 + </thead> 40 + <tbody> 41 + {{ range .keys }} 42 + <tr> 43 + <td>{{ .Name }}</td> 44 + <td>{{ .Created }}</td> 45 + <td>{{ .Key }}</td> 46 + </tr> 47 + {{ end }} 48 + </tbody> 49 + </table> 32 50 </main> 33 51 </body> 34 52 </html>