tangled
alpha
login
or
join now
tjh.dev
/
core
forked from
tangled.org/core
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
add page for repo index
oppi.li
1 year ago
05709520
40c080cd
+112
-47
7 changed files
expand all
collapse all
unified
split
appview
pages
pages.go
templates
repo
index.html
repo.html
user
profile.html
state
repo.go
knotserver
routes.go
types
repo.go
+13
appview/pages/pages.go
···
11
11
12
12
"github.com/sotangled/tangled/appview/auth"
13
13
"github.com/sotangled/tangled/appview/db"
14
14
+
"github.com/sotangled/tangled/types"
14
15
)
15
16
16
17
//go:embed templates/*
···
125
126
func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
126
127
return p.execute("user/profile", w, params)
127
128
}
129
129
+
130
130
+
type RepoIndexParams struct {
131
131
+
LoggedInUser *auth.User
132
132
+
Name string
133
133
+
UserDid string
134
134
+
UserHandle string
135
135
+
types.RepoIndexResponse
136
136
+
}
137
137
+
138
138
+
func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
139
139
+
return p.execute("repo/index", w, params)
140
140
+
}
+41
appview/pages/templates/repo/index.html
···
1
1
+
{{define "title"}} {{ or .UserHandle .UserDid }} / {{ .Name }} {{end}}
2
2
+
3
3
+
{{define "content"}}
4
4
+
{{- $id := "" -}}
5
5
+
{{- if .UserHandle -}}
6
6
+
{{- $id = printf "@%s" .UserHandle -}}
7
7
+
{{- else -}}
8
8
+
{{- $id = .UserDid -}}
9
9
+
{{- end -}}
10
10
+
11
11
+
<h1>
12
12
+
{{ $id }} / {{ .Name }}
13
13
+
</h1>
14
14
+
<main>
15
15
+
<div class="log">
16
16
+
{{ range .Commits }}
17
17
+
<div>
18
18
+
<div><a href="/{{ $id }}/{{ $.Name }}/commit/{{ .Hash.String }}" class="commit-hash">{{ slice .Hash.String 0 8 }}</a></div>
19
19
+
<pre>{{ .Message }}</pre>
20
20
+
</div>
21
21
+
<div class="commit-info">
22
22
+
{{ .Author.Name }} <a href="mailto:{{ .Author.Email }}" class="commit-email">{{ .Author.Email }}</a>
23
23
+
<div>{{ .Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
24
24
+
</div>
25
25
+
{{ end }}
26
26
+
</div>
27
27
+
{{- if .Readme }}
28
28
+
<article class="readme">
29
29
+
{{- .Readme -}}
30
30
+
</article>
31
31
+
{{- end -}}
32
32
+
33
33
+
<div class="clone-url">
34
34
+
<strong>clone</strong>
35
35
+
<pre>
36
36
+
git clone https://tangled.sh/{{ $id }}/{{ .Name }}
37
37
+
</pre>
38
38
+
</div>
39
39
+
</main>
40
40
+
{{end}}
41
41
+
-36
appview/pages/templates/repo/repo.html
···
1
1
-
<html>
2
2
-
{{ template "layouts/head" . }}
3
3
-
4
4
-
{{ template "layouts/repo-header" . }}
5
5
-
6
6
-
<body>
7
7
-
{{ template "layouts/nav" . }}
8
8
-
<main>
9
9
-
{{ $repo := .name }}
10
10
-
<div class="log">
11
11
-
{{ range .commits }}
12
12
-
<div>
13
13
-
<div><a href="/{{ $repo }}/commit/{{ .Hash.String }}" class="commit-hash">{{ slice .Hash.String 0 8 }}</a></div>
14
14
-
<pre>{{ .Message }}</pre>
15
15
-
</div>
16
16
-
<div class="commit-info">
17
17
-
{{ .Author.Name }} <a href="mailto:{{ .Author.Email }}" class="commit-email">{{ .Author.Email }}</a>
18
18
-
<div>{{ .Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
19
19
-
</div>
20
20
-
{{ end }}
21
21
-
</div>
22
22
-
{{- if .readme }}
23
23
-
<article class="readme">
24
24
-
{{- .readme -}}
25
25
-
</article>
26
26
-
{{- end -}}
27
27
-
28
28
-
<div class="clone-url">
29
29
-
<strong>clone</strong>
30
30
-
<pre>
31
31
-
git clone https://{{ .servername }}/{{ .name }}
32
32
-
</pre>
33
33
-
</div>
34
34
-
</main>
35
35
-
</body>
36
36
-
</html>
+1
-1
appview/pages/templates/user/profile.html
···
8
8
<ul id="my-knots">
9
9
{{range .Repos}}
10
10
<li>
11
11
-
<code>name: {{.Name}}</code><br>
11
11
+
<code>name: <a href="/@{{or $.UserHandle $.UserDid }}/{{.Name}}">{{.Name}}</a></code><br>
12
12
<code>knot: {{.Knot}}</code><br>
13
13
</li>
14
14
{{else}}
+28
-2
appview/state/repo.go
···
1
1
package state
2
2
3
3
import (
4
4
+
"encoding/json"
4
5
"fmt"
5
6
"io"
6
7
"log"
···
8
9
9
10
"github.com/bluesky-social/indigo/atproto/identity"
10
11
"github.com/go-chi/chi/v5"
12
12
+
"github.com/sotangled/tangled/appview/pages"
13
13
+
"github.com/sotangled/tangled/types"
11
14
)
12
15
13
16
func (s *State) RepoIndex(w http.ResponseWriter, r *http.Request) {
···
33
36
log.Println("failed to reach knotserver", err)
34
37
return
35
38
}
39
39
+
defer resp.Body.Close()
36
40
37
37
-
txt, err := io.ReadAll(resp.Body)
38
38
-
log.Println(resp.Status, string(txt))
41
41
+
// Read the response body
42
42
+
body, err := io.ReadAll(resp.Body)
43
43
+
if err != nil {
44
44
+
log.Fatalf("Error reading response body: %v", err)
45
45
+
return
46
46
+
}
47
47
+
48
48
+
var result types.RepoIndexResponse
49
49
+
err = json.Unmarshal(body, &result)
50
50
+
if err != nil {
51
51
+
log.Fatalf("Error unmarshalling response body: %v", err)
52
52
+
return
53
53
+
}
54
54
+
55
55
+
log.Println(resp.Status, result)
56
56
+
57
57
+
user := s.auth.GetUser(r)
58
58
+
s.pages.RepoIndexPage(w, pages.RepoIndexParams{
59
59
+
LoggedInUser: user,
60
60
+
UserDid: id.DID.String(),
61
61
+
UserHandle: id.Handle.String(),
62
62
+
Name: repoName,
63
63
+
RepoIndexResponse: result,
64
64
+
})
39
65
40
66
return
41
67
}
+14
-8
knotserver/routes.go
···
21
21
"github.com/russross/blackfriday/v2"
22
22
"github.com/sotangled/tangled/knotserver/db"
23
23
"github.com/sotangled/tangled/knotserver/git"
24
24
+
"github.com/sotangled/tangled/types"
24
25
)
25
26
26
27
func (h *Handle) Index(w http.ResponseWriter, r *http.Request) {
27
27
-
w.Write([]byte("This is a knot, part of the wider Tangle network: https://knots.sh"))
28
28
+
w.Write([]byte("This is a knot, part of the wider Tangle network: https://tangled.sh"))
28
29
}
29
30
30
31
func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) {
···
34
35
gr, err := git.Open(path, "")
35
36
if err != nil {
36
37
if errors.Is(err, plumbing.ErrReferenceNotFound) {
37
37
-
writeMsg(w, "repo empty")
38
38
+
resp := types.RepoIndexResponse{
39
39
+
IsEmpty: true,
40
40
+
}
41
41
+
writeJSON(w, resp)
38
42
return
39
43
} else {
40
44
l.Error("opening repo", "error", err.Error())
···
86
90
if len(commits) >= 3 {
87
91
commits = commits[:3]
88
92
}
89
89
-
data := make(map[string]any)
90
90
-
data["ref"] = mainBranch
91
91
-
data["readme"] = readmeContent
92
92
-
data["commits"] = commits
93
93
-
data["desc"] = getDescription(path)
93
93
+
resp := types.RepoIndexResponse{
94
94
+
IsEmpty: false,
95
95
+
Ref: mainBranch,
96
96
+
Commits: commits,
97
97
+
Description: getDescription(path),
98
98
+
Readme: readmeContent,
99
99
+
}
94
100
95
95
-
writeJSON(w, data)
101
101
+
writeJSON(w, resp)
96
102
return
97
103
}
98
104
+15
types/repo.go
···
1
1
+
package types
2
2
+
3
3
+
import (
4
4
+
"html/template"
5
5
+
6
6
+
"github.com/go-git/go-git/v5/plumbing/object"
7
7
+
)
8
8
+
9
9
+
type RepoIndexResponse struct {
10
10
+
IsEmpty bool `json:"is_empty"`
11
11
+
Ref string `json:"ref,omitempty"`
12
12
+
Readme template.HTML `json:"readme,omitempty"`
13
13
+
Commits []*object.Commit `json:"commits,omitempty"`
14
14
+
Description string `json:"description,omitempty"`
15
15
+
}