routes/routes.go (view raw)
1package routes
2
3import (
4 "html/template"
5 "log"
6 "net/http"
7 "path/filepath"
8
9 "github.com/alexedwards/flow"
10 "icyphox.sh/legit/config"
11 "icyphox.sh/legit/git"
12)
13
14type deps struct {
15 c *config.Config
16}
17
18func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
19
20}
21
22func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
23 name := flow.Param(r.Context(), "name")
24 name = filepath.Clean(name)
25 // TODO: remove .git
26 path := filepath.Join(d.c.Git.ScanPath, name+".git")
27 gr, err := git.Open(path, "")
28 if err != nil {
29 d.Write404(w)
30 return
31 }
32
33 files, err := gr.FileTree("")
34 if err != nil {
35 d.Write500(w)
36 log.Println(err)
37 return
38 }
39
40 var readmeContent string
41 for _, readme := range d.c.Git.Readme {
42 readmeContent, _ = gr.FileContent(readme)
43 if readmeContent != "" {
44 break
45 }
46 }
47
48 if readmeContent == "" {
49 log.Printf("no readme found for %s", name)
50 }
51
52 data := make(map[string]any)
53 data["name"] = name
54 // TODO: make this configurable
55 data["ref"] = "master"
56 data["readme"] = readmeContent
57
58 d.listFiles(files, data, w)
59 return
60}
61
62func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
63 name := flow.Param(r.Context(), "name")
64 treePath := flow.Param(r.Context(), "...")
65 ref := flow.Param(r.Context(), "ref")
66
67 name = filepath.Clean(name)
68 // TODO: remove .git
69 path := filepath.Join(d.c.Git.ScanPath, name+".git")
70 gr, err := git.Open(path, ref)
71 if err != nil {
72 d.Write404(w)
73 return
74 }
75
76 files, err := gr.FileTree(treePath)
77 if err != nil {
78 d.Write500(w)
79 log.Println(err)
80 return
81 }
82
83 data := make(map[string]any)
84 data["name"] = name
85 data["ref"] = ref
86 data["parent"] = treePath
87
88 d.listFiles(files, data, w)
89 return
90}
91
92func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
93 name := flow.Param(r.Context(), "name")
94 treePath := flow.Param(r.Context(), "...")
95 ref := flow.Param(r.Context(), "ref")
96
97 name = filepath.Clean(name)
98 // TODO: remove .git
99 path := filepath.Join(d.c.Git.ScanPath, name+".git")
100 gr, err := git.Open(path, ref)
101 if err != nil {
102 d.Write404(w)
103 return
104 }
105
106 contents, err := gr.FileContent(treePath)
107 data := make(map[string]any)
108 data["name"] = name
109 data["ref"] = ref
110
111 d.showFile(contents, data, w)
112 return
113}
114
115func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
116 name := flow.Param(r.Context(), "name")
117 ref := flow.Param(r.Context(), "ref")
118
119 path := filepath.Join(d.c.Git.ScanPath, name+".git")
120 gr, err := git.Open(path, ref)
121 if err != nil {
122 d.Write404(w)
123 return
124 }
125
126 commits, err := gr.Commits()
127 if err != nil {
128 d.Write500(w)
129 log.Println(err)
130 return
131 }
132
133 tpath := filepath.Join(d.c.Template.Dir, "*")
134 t := template.Must(template.ParseGlob(tpath))
135
136 data := make(map[string]interface{})
137 data["commits"] = commits
138 data["meta"] = d.c.Meta
139 data["name"] = name
140 data["ref"] = ref
141
142 if err := t.ExecuteTemplate(w, "log", data); err != nil {
143 log.Println(err)
144 return
145 }
146}
147
148func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
149 name := flow.Param(r.Context(), "name")
150 ref := flow.Param(r.Context(), "ref")
151
152 path := filepath.Join(d.c.Git.ScanPath, name+".git")
153 gr, err := git.Open(path, ref)
154 if err != nil {
155 d.Write404(w)
156 return
157 }
158
159 diff, err := gr.Diff()
160 if err != nil {
161 d.Write500(w)
162 log.Println(err)
163 return
164 }
165
166 tpath := filepath.Join(d.c.Template.Dir, "*")
167 t := template.Must(template.ParseGlob(tpath))
168
169 data := make(map[string]interface{})
170
171 data["commit"] = diff.Commit
172 data["stat"] = diff.Stat
173 data["diff"] = diff.Diff
174 data["meta"] = d.c.Meta
175 data["name"] = name
176 data["ref"] = ref
177
178 if err := t.ExecuteTemplate(w, "commit", data); err != nil {
179 log.Println(err)
180 return
181 }
182}