+2
legit/db/init.go
+2
legit/db/init.go
···
22
did text not null,
23
name text not null,
24
key text not null,
25
+
created timestamp default current_timestamp,
26
unique(did, name, key)
27
);
28
create table if not exists repos (
···
30
did text not null,
31
name text not null,
32
description text not null,
33
+
created timestamp default current_timestamp,
34
unique(did, name)
35
)
36
`)
+28
-6
legit/db/pubkeys.go
+28
-6
legit/db/pubkeys.go
···
1
package db
2
3
func (d *DB) AddPublicKey(did, name, key string) error {
4
query := `insert into public_keys (did, name, key) values (?, ?, ?)`
5
_, err := d.db.Exec(query, did, name, key)
···
12
return err
13
}
14
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)
19
if err != nil {
20
-
return "", err
21
}
22
-
return key, nil
23
}
···
1
package db
2
3
+
import "time"
4
+
5
func (d *DB) AddPublicKey(did, name, key string) error {
6
query := `insert into public_keys (did, name, key) values (?, ?, ?)`
7
_, err := d.db.Exec(query, did, name, key)
···
14
return err
15
}
16
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)
27
if err != nil {
28
+
return nil, err
29
}
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
45
}
+10
-2
legit/routes/routes.go
+10
-2
legit/routes/routes.go
···
445
func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {
446
switch r.Method {
447
case http.MethodGet:
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 {
458
log.Println(err)
459
return
460
}
+18
legit/templates/keys.html
+18
legit/templates/keys.html
···
29
Submit
30
</button>
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>
50
</main>
51
</body>
52
</html>