Fudge

fudge / handler / handler.go

Commits | bovarysme Release v0.4.0 Committed on Dec 25, 2019

5.9 kB | Download

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package handler

import (
	"fmt"
	"html/template"
	"io"
	"net/http"

	"bovarys.me/fudge/config"
	"bovarys.me/fudge/git"
	"bovarys.me/fudge/logger"
	"bovarys.me/fudge/util"

	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
	gogit "gopkg.in/src-d/go-git.v4"
)

type Handler struct {
	Router http.Handler

	config *config.Config
	tmpl   map[string]*template.Template
}

func NewHandler(cfg *config.Config) (*Handler, error) {
	h := &Handler{
		config: cfg,
		tmpl:   make(map[string]*template.Template),
	}

	router := mux.NewRouter()
	router.StrictSlash(true)

	static := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
	router.PathPrefix("/static/").Handler(static)

	router.HandleFunc("/", h.showHome)
	router.HandleFunc("/{repository}/", h.showTree)
	router.HandleFunc("/{repository}/commits", h.showCommits)
	router.HandleFunc("/{repository}/tree/{path:.*}", h.showTree)
	router.HandleFunc("/{repository}/blob/{path:.*}", h.showBlob)
	router.HandleFunc("/{repository}/raw/{path:.*}", h.sendBlob)

	h.Router = router

	err := h.setLoggers()
	if err != nil {
		return nil, err
	}

	pages := []string{"home", "commits", "tree", "blob", "404", "500"}
	for _, page := range pages {
		path := fmt.Sprintf("template/%s.html", page)

		t, err := template.ParseFiles(
			"template/_layout.html", "template/_utils.html", path)
		if err != nil {
			return nil, err
		}

		h.tmpl[page] = t
	}

	return h, nil
}

func (h *Handler) setLoggers() error {
	loggerConfig, ok := h.config.Loggers["router"]
	if !ok {
		return nil
	}

	if !loggerConfig.Enable {
		return nil
	}

	writer, err := logger.Writer(loggerConfig)
	if err != nil {
		return err
	}

	h.Router = handlers.CombinedLoggingHandler(writer, h.Router)

	return nil
}

func (h *Handler) openRepository(w http.ResponseWriter, r *http.Request) (*gogit.Repository, error) {
	vars := mux.Vars(r)

	repository, err := git.OpenRepository(h.config.RepoRoot, vars["repository"], false)
	if err == gogit.ErrRepositoryNotExists {
		h.showError(w, r, http.StatusNotFound, nil)
		return nil, err
	}
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return nil, err
	}

	return repository, nil
}

func (h *Handler) getParams(r *http.Request) map[string]interface{} {
	vars := mux.Vars(r)

	repository := vars["repository"]
	path := vars["path"]

	params := make(map[string]interface{})

	params["Domain"] = h.config.Domain
	params["GitURL"] = h.config.GitURL
	params["RepoName"] = repository
	params["Path"] = path

	if repository != "" {
		params["Breadcrumbs"] = util.Breadcrumbs(repository, path)
	}

	return params
}

func (h *Handler) showError(w http.ResponseWriter, r *http.Request, status int, err error) {
	w.WriteHeader(status)

	switch status {
	case http.StatusNotFound:
		h.tmpl["404"].ExecuteTemplate(w, "layout", nil)
	case http.StatusInternalServerError:
		params := h.getParams(r)

		params["Debug"] = h.config.Debug
		params["Error"] = err.Error()

		h.tmpl["500"].ExecuteTemplate(w, "layout", params)
	}
}

func (h *Handler) showHome(w http.ResponseWriter, r *http.Request) {
	names, err := git.GetRepositoryNames(h.config.RepoRoot)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}

	params := h.getParams(r)

	params["Names"] = names
	params["Descriptions"] = h.config.Descriptions

	h.tmpl["home"].ExecuteTemplate(w, "layout", params)
}

func (h *Handler) showCommits(w http.ResponseWriter, r *http.Request) {
	repository, err := h.openRepository(w, r)
	if err != nil {
		return
	}

	commits, err := git.GetRepositoryCommits(repository)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}

	params := h.getParams(r)

	params["Commits"] = commits

	h.tmpl["commits"].ExecuteTemplate(w, "layout", params)
}

func (h *Handler) showTree(w http.ResponseWriter, r *http.Request) {
	repository, err := h.openRepository(w, r)
	if err != nil {
		return
	}

	vars := mux.Vars(r)

	tree, err := git.GetRepositoryTree(repository, vars["path"])
	if err != nil {
		h.showError(w, r, http.StatusNotFound, nil)
		return
	}

	objects, err := git.GetTreeObjects(tree)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}

	commit, err := git.GetRepositoryLastCommit(repository)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}

	params := h.getParams(r)

	params["LastCommit"] = commit
	params["Objects"] = objects

	h.tmpl["tree"].ExecuteTemplate(w, "layout", params)
}

func (h *Handler) showBlob(w http.ResponseWriter, r *http.Request) {
	repository, err := h.openRepository(w, r)
	if err != nil {
		return
	}

	vars := mux.Vars(r)

	blob, err := git.GetRepositoryBlob(repository, vars["path"])
	if err != nil {
		h.showError(w, r, http.StatusNotFound, nil)
		return
	}

	contents := ""
	if !blob.IsBinary {
		contents, err = util.Highlight(blob.Name, blob.Reader)
		if err != nil {
			h.showError(w, r, http.StatusInternalServerError, err)
			return
		}
	}

	commit, err := git.GetRepositoryLastCommit(repository)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}

	params := h.getParams(r)

	params["LastCommit"] = commit
	params["Blob"] = blob
	params["Contents"] = template.HTML(contents)

	h.tmpl["blob"].ExecuteTemplate(w, "layout", params)
}

func (h *Handler) sendBlob(w http.ResponseWriter, r *http.Request) {
	repository, err := h.openRepository(w, r)
	if err != nil {
		return
	}

	vars := mux.Vars(r)

	blob, err := git.GetRepositoryBlob(repository, vars["path"])
	if err != nil {
		h.showError(w, r, http.StatusNotFound, nil)
		return
	}

	value := "text/plain; charset=utf-8"
	if blob.IsBinary {
		value = "application/octet-stream"
	}

	w.Header().Set("Content-Type", value)

	_, err = io.Copy(w, blob.Reader)
	if err != nil {
		h.showError(w, r, http.StatusInternalServerError, err)
		return
	}
}