Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/goit/index.go (102 lines, 2.4 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
3
package goit
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
4
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
5
import (
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
6
	"errors"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
7
	"log"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
8
	"net/http"
2954edd Jakob Wakeling 2024-06-16 22:57:19
9
	"slices"
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
10
	"sort"
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
11
	"strings"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
12
	"time"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
13
2954edd Jakob Wakeling 2024-06-16 22:57:19
14
	"github.com/Jamozed/Goit/src/util"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
15
	"github.com/go-git/go-git/v5"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
16
	"github.com/go-git/go-git/v5/plumbing"
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
17
)
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
18
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
19
func HandleIndex(w http.ResponseWriter, r *http.Request) {
7974d70 Jakob Wakeling 2023-11-03 22:19:52
20
	auth, user, err := Auth(w, r, true)
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
21
	if err != nil {
7974d70 Jakob Wakeling 2023-11-03 22:19:52
22
		log.Println("[index]", err.Error())
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
23
		HttpError(w, http.StatusInternalServerError)
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
24
		return
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
25
	}
7974d70 Jakob Wakeling 2023-11-03 22:19:52
26
2954edd Jakob Wakeling 2024-06-16 22:57:19
27
	userQuery := util.FormValues(r, "u")
2954edd Jakob Wakeling 2024-06-16 22:57:19
28
	visibilityQuery := util.FormValues(r, "v")
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
29
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
30
	type row struct{ Name, Description, Owner, Visibility, LastCommit string }
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
31
	data := struct {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
32
		Title, Username string
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
33
		Admin, Auth     bool
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
34
		Repos           []row
7974d70 Jakob Wakeling 2023-11-03 22:19:52
35
	}{Title: "Repositories", Auth: auth}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
36
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
37
	if user != nil {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
38
		data.Username = user.Name
7974d70 Jakob Wakeling 2023-11-03 22:19:52
39
		data.Admin = user.IsAdmin
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
40
	}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
41
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
42
	repos, err := GetRepos()
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
43
	if err != nil {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
44
		log.Println("[/]", err.Error())
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
45
		HttpError(w, http.StatusInternalServerError)
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
46
		return
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
47
	}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
48
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
49
	rtemp := repos[:0]
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
50
	for _, repo := range repos {
778d467 Jakob Wakeling 2024-01-17 18:29:55
51
		if IsVisible(&repo, auth, user) {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
52
			rtemp = append(rtemp, repo)
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
53
		}
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
54
	}
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
55
	repos = rtemp
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
56
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
57
	sort.Slice(repos, func(i, j int) bool {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
58
		/* TODO sort capitals like AaBbCc etc. */
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
59
		return strings.ToLower(repos[i].Name) < strings.ToLower(repos[j].Name)
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
60
	})
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
61
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
62
	for _, repo := range repos {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
63
		owner, err := GetUser(repo.OwnerId)
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
64
		if err != nil {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
65
			log.Println("[/]", err.Error())
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
66
		}
7974d70 Jakob Wakeling 2023-11-03 22:19:52
67
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
68
		/* Only display repositories matching user query if present */
2954edd Jakob Wakeling 2024-06-16 22:57:19
69
		if len(userQuery) > 0 && !slices.Contains(userQuery, owner.Name) {
2954edd Jakob Wakeling 2024-06-16 22:57:19
70
			continue
2954edd Jakob Wakeling 2024-06-16 22:57:19
71
		}
2954edd Jakob Wakeling 2024-06-16 22:57:19
72
2954edd Jakob Wakeling 2024-06-16 22:57:19
73
		/* Only display repositories matching a visibility query if present */
2954edd Jakob Wakeling 2024-06-16 22:57:19
74
		if len(visibilityQuery) > 0 && !slices.Contains(visibilityQuery, repo.Visibility.String()) {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
75
			continue
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
76
		}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
77
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
78
		var lastCommit string
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
79
		if gr, err := git.PlainOpen(RepoPath(repo.Name, true)); err != nil {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
80
			log.Println("[/]", err.Error())
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
81
		} else if ref, err := gr.Head(); err != nil {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
82
			if !errors.Is(err, plumbing.ErrReferenceNotFound) {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
83
				log.Println("[/]", err.Error())
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
84
			}
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
85
		} else if commit, err := gr.CommitObject(ref.Hash()); err != nil {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
86
			log.Println("[/]", err.Error())
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
87
		} else {
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
88
			lastCommit = commit.Author.When.UTC().Format(time.DateTime)
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
89
		}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
90
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
91
		data.Repos = append(data.Repos, row{
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
92
			Name: repo.Name, Description: repo.Description, Owner: owner.Name,
778d467 Jakob Wakeling 2024-01-17 18:29:55
93
			Visibility: repo.Visibility.String(), LastCommit: lastCommit,
a9ee9eb Jakob Wakeling 2023-12-18 23:19:43
94
		})
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
95
	}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
96
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
97
	if err := Tmpl.ExecuteTemplate(w, "index", data); err != nil {
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
98
		log.Println("[/]", err.Error())
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
99
	}
1f376a9 Jakob Wakeling 2023-08-03 20:35:23
100
}
101