ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
0
|
// Copyright (C) 2023, Jakob Wakeling |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
1
|
// All rights reserved. |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
2
|
|
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
3
|
package main |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
4
|
|
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
5
|
import ( |
5166d87 |
Jakob Wakeling |
2023-10-23 16:00:35 |
6
|
"flag" |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
7
|
"log" |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
8
|
"net/http" |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
9
|
"os" |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
10
|
"os/signal" |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
11
|
"path" |
8abd343 |
Jakob Wakeling |
2023-07-19 00:40:45 |
12
|
"strings" |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
13
|
"time" |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
14
|
|
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
15
|
"github.com/Jamozed/Goit/res" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
16
|
"github.com/Jamozed/Goit/src/admin" |
945fd71 |
Jakob Wakeling |
2023-11-05 15:39:31 |
17
|
"github.com/Jamozed/Goit/src/goit" |
64718e1 |
Jakob Wakeling |
2023-07-24 21:56:07 |
18
|
"github.com/Jamozed/Goit/src/repo" |
ff9ec25 |
Jakob Wakeling |
2023-08-08 22:43:43 |
19
|
"github.com/Jamozed/Goit/src/user" |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
20
|
"github.com/Jamozed/Goit/src/util" |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
21
|
"github.com/go-chi/chi/v5" |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
22
|
"github.com/go-chi/chi/v5/middleware" |
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
23
|
"github.com/gorilla/csrf" |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
24
|
) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
25
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
26
|
var protect func(http.Handler) http.Handler |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
27
|
|
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
28
|
func main() { |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
29
|
var backup bool |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
30
|
|
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
31
|
flag.BoolVar(&backup, "backup", false, "Perform a backup") |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
32
|
flag.BoolVar(&util.Debug, "debug", false, "Enable debug logging") |
5166d87 |
Jakob Wakeling |
2023-10-23 16:00:35 |
33
|
flag.Parse() |
5166d87 |
Jakob Wakeling |
2023-10-23 16:00:35 |
34
|
|
32e8b4d |
Jakob Wakeling |
2024-07-07 17:50:40 |
35
|
if backup { |
32e8b4d |
Jakob Wakeling |
2024-07-07 17:50:40 |
36
|
if err := goit.Backup(); err != nil { |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
37
|
log.Fatalln(err.Error()) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
38
|
} |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
39
|
|
32e8b4d |
Jakob Wakeling |
2024-07-07 17:50:40 |
40
|
os.Exit(0) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
41
|
} |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
42
|
|
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
43
|
/* Listen for and handle SIGINT */ |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
44
|
c := make(chan os.Signal, 1) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
45
|
signal.Notify(c, os.Interrupt) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
46
|
|
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
47
|
go func() { |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
48
|
<-c |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
49
|
goit.Cron.Stop() |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
50
|
os.Exit(0) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
51
|
}() |
410f65b |
Jakob Wakeling |
2023-10-23 15:14:42 |
52
|
|
c768e5c |
Jakob Wakeling |
2023-11-27 21:58:27 |
53
|
/* Initialise Goit */ |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
54
|
if err := goit.Goit(); err != nil { |
d631c5e |
Jakob Wakeling |
2023-07-20 23:13:39 |
55
|
log.Fatalln(err.Error()) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
56
|
} |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
57
|
|
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
58
|
h := chi.NewRouter() |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
59
|
h.NotFound(goit.HttpNotFound) |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
60
|
h.Use(middleware.RedirectSlashes) |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
61
|
h.Use(logHttp) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
62
|
|
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
63
|
h.Use(func(h http.Handler) http.Handler { |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
64
|
return http.TimeoutHandler(h, 90*time.Second, |
71b7f19 |
Jakob Wakeling |
2024-01-08 22:23:49 |
65
|
`<!DOCTYPE html><html lang="en"><head> |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
66
|
<meta charset="UTF-8"><title>503 Service Unavailable</title> |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
67
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
68
|
<link rel="stylesheet" type="text/css" href="/static/style.css"> |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
69
|
<link rel="icon" type="image/png" href="/static/favicon.png"> |
71b7f19 |
Jakob Wakeling |
2024-01-08 22:23:49 |
70
|
</head><body><b>503 Service Unavailable</b></body></html>`) |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
71
|
}) |
b4b291e |
Jakob Wakeling |
2023-12-23 17:05:01 |
72
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
73
|
protect = csrf.Protect( |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
74
|
[]byte(goit.Conf.CsrfSecret), csrf.FieldName("csrf.Token"), csrf.CookieName("csrf"), |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
75
|
csrf.Secure(util.If(goit.Conf.UsesHttps, true, false)), |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
76
|
) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
77
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
78
|
h.Group(func(r chi.Router) { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
79
|
r.Use(protect) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
80
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
81
|
r.Get("/", goit.HandleIndex) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
82
|
r.Get("/user/login", user.HandleLogin) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
83
|
r.Post("/user/login", user.HandleLogin) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
84
|
r.Get("/user/logout", goit.HandleUserLogout) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
85
|
r.Post("/user/logout", goit.HandleUserLogout) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
86
|
r.Get("/user/sessions", user.HandleSessions) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
87
|
r.Post("/user/sessions", user.HandleSessions) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
88
|
r.Get("/user/edit", user.HandleEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
89
|
r.Post("/user/edit", user.HandleEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
90
|
r.Get("/repo/create", repo.HandleCreate) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
91
|
r.Post("/repo/create", repo.HandleCreate) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
92
|
r.Get("/admin", admin.HandleStatus) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
93
|
r.Get("/admin/status", admin.HandleStatus) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
94
|
r.Get("/admin/users", admin.HandleUsers) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
95
|
r.Get("/admin/user/create", admin.HandleUserCreate) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
96
|
r.Post("/admin/user/create", admin.HandleUserCreate) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
97
|
r.Get("/admin/user/edit", admin.HandleUserEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
98
|
r.Post("/admin/user/edit", admin.HandleUserEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
99
|
r.Get("/admin/repos", admin.HandleRepos) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
100
|
r.Get("/admin/repo/edit", admin.HandleRepoEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
101
|
r.Post("/admin/repo/edit", admin.HandleRepoEdit) |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
102
|
r.Get("/admin/cron", admin.HandleCron) |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
103
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
104
|
r.Get("/static/style.css", handleStyle) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
105
|
r.Get("/static/favicon.png", handleFavicon) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
106
|
r.Get("/favicon.ico", goit.HttpNotFound) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
107
|
}) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
108
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
109
|
/* TODO figure out how to use a subrouter after manually parsing the repo path */ |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
110
|
h.HandleFunc("/*", HandleRepo) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
111
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
112
|
/* Old repository routing, doesn't support directories */ |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
113
|
// h.Get("/{repo}", repo.HandleLog) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
114
|
// h.Get("/{repo}/log", repo.HandleLog) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
115
|
// h.Get("/{repo}/log/*", repo.HandleLog) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
116
|
// h.Get("/{repo}/commit/{hash}", repo.HandleCommit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
117
|
// h.Get("/{repo}/tree", repo.HandleTree) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
118
|
// h.Get("/{repo}/tree/*", repo.HandleTree) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
119
|
// h.Get("/{repo}/file/*", repo.HandleFile) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
120
|
// h.Get("/{repo}/raw/*", repo.HandleRaw) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
121
|
// h.Get("/{repo}/download", repo.HandleDownload) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
122
|
// h.Get("/{repo}/download/*", repo.HandleDownload) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
123
|
// h.Get("/{repo}/refs", repo.HandleRefs) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
124
|
// h.Get("/{repo}/edit", repo.HandleEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
125
|
// h.Post("/{repo}/edit", repo.HandleEdit) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
126
|
// h.Get("/{repo}/info/refs", goit.HandleInfoRefs) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
127
|
// h.Get("/{repo}/git-upload-pack", goit.HandleUploadPack) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
128
|
// h.Post("/{repo}/git-upload-pack", goit.HandleUploadPack) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
129
|
// h.Get("/{repo}/git-receive-pack", goit.HandleReceivePack) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
130
|
// h.Post("/{repo}/git-receive-pack", goit.HandleReceivePack) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
131
|
|
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
132
|
/* Listen for HTTP on the specified port */ |
3f4c3f4 |
Jakob Wakeling |
2023-12-01 23:26:03 |
133
|
if err := http.ListenAndServe(goit.Conf.HttpAddr+":"+goit.Conf.HttpPort, h); err != nil { |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
134
|
log.Fatalln("[http]", err.Error()) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
135
|
} |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
136
|
} |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
137
|
|
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
138
|
func logHttp(next http.Handler) http.Handler { |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
139
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
140
|
t1 := time.Now() |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
141
|
next.ServeHTTP(w, r) |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
142
|
|
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
143
|
ip := r.RemoteAddr |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
144
|
if fip := r.Header.Get("X-Forwarded-For"); goit.Conf.IpForwarded && fip != "" { |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
145
|
ip = fip |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
146
|
} |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
147
|
|
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
148
|
log.Println("[http]", r.Method, r.URL.String(), "from", ip, "in", time.Since(t1)) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
149
|
}) |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
150
|
} |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
151
|
|
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
152
|
func handleStyle(w http.ResponseWriter, r *http.Request) { |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
153
|
w.Header().Set("Content-Type", "text/css") |
ae5fc19 |
Jakob Wakeling |
2023-07-17 21:54:54 |
154
|
if _, err := w.Write([]byte(res.Style)); err != nil { |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
155
|
log.Println("[style]", err.Error()) |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
156
|
} |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
157
|
} |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
158
|
|
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
159
|
func handleFavicon(w http.ResponseWriter, r *http.Request) { |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
160
|
if goit.Favicon == nil { |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
161
|
goit.HttpError(w, http.StatusNotFound) |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
162
|
} else { |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
163
|
w.Header().Set("Content-Type", "image/png") |
17c34f4 |
Jakob Wakeling |
2023-07-19 22:07:36 |
164
|
if _, err := w.Write(goit.Favicon); err != nil { |
e530f2c |
Jakob Wakeling |
2023-12-17 22:28:16 |
165
|
log.Println("[favicon]", err.Error()) |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
166
|
} |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
167
|
} |
08dff9a |
Jakob Wakeling |
2023-11-07 21:03:04 |
168
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
169
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
170
|
func HandleRepo(w http.ResponseWriter, r *http.Request) { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
171
|
parts := strings.Split(r.URL.Path, "/") |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
172
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
173
|
repos, err := goit.GetRepos() |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
174
|
if err != nil { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
175
|
goit.HttpError(w, http.StatusInternalServerError) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
176
|
return |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
177
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
178
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
179
|
var rpath string |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
180
|
for _, p := range parts { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
181
|
rpath = path.Join(rpath, p) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
182
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
183
|
for _, r := range repos { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
184
|
if rpath == r.Name { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
185
|
goto found |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
186
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
187
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
188
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
189
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
190
|
goit.HttpError(w, http.StatusNotFound) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
191
|
return |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
192
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
193
|
found: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
194
|
spath := strings.TrimPrefix(r.URL.Path, "/"+rpath) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
195
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
196
|
rctx := chi.RouteContext(r.Context()) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
197
|
if rctx == nil { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
198
|
log.Println("[route] NULL route context") |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
199
|
goit.HttpError(w, http.StatusInternalServerError) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
200
|
return |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
201
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
202
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
203
|
rctx.URLParams.Add("repo", rpath) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
204
|
rctx.URLParams.Add("*", "") |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
205
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
206
|
switch r.Method { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
207
|
case http.MethodGet: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
208
|
switch { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
209
|
case strings.HasPrefix(spath, "/log"), len(spath) == 0: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
210
|
rctx.URLParams.Add("*", strings.TrimLeft(strings.TrimPrefix(spath, "/log"), "/")) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
211
|
protect(http.HandlerFunc(repo.HandleLog)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
212
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
213
|
case strings.HasPrefix(spath, "/commit/"): |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
214
|
hash := strings.TrimPrefix(spath, "/commit/") |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
215
|
if strings.Contains(hash, "/") { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
216
|
goit.HttpError(w, http.StatusNotFound) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
217
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
218
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
219
|
rctx.URLParams.Add("hash", hash) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
220
|
protect(http.HandlerFunc(repo.HandleCommit)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
221
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
222
|
case strings.HasPrefix(spath, "/tree"): |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
223
|
rctx.URLParams.Add("*", strings.TrimLeft(strings.TrimPrefix(spath, "/tree"), "/")) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
224
|
protect(http.HandlerFunc(repo.HandleTree)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
225
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
226
|
case strings.HasPrefix(spath, "/file/"): |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
227
|
rctx.URLParams.Add("*", strings.TrimPrefix(spath, "/file/")) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
228
|
protect(http.HandlerFunc(repo.HandleFile)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
229
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
230
|
case strings.HasPrefix(spath, "/raw/"): |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
231
|
rctx.URLParams.Add("*", strings.TrimPrefix(spath, "/raw/")) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
232
|
protect(http.HandlerFunc(repo.HandleRaw)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
233
|
|
a0ac27c |
Jakob Wakeling |
2024-07-06 23:32:05 |
234
|
case strings.HasPrefix(spath, "/blame/"): |
a0ac27c |
Jakob Wakeling |
2024-07-06 23:32:05 |
235
|
rctx.URLParams.Add("*", strings.TrimPrefix(spath, "/blame/")) |
a0ac27c |
Jakob Wakeling |
2024-07-06 23:32:05 |
236
|
protect(http.HandlerFunc(repo.HandleBlame)).ServeHTTP(w, r) |
a0ac27c |
Jakob Wakeling |
2024-07-06 23:32:05 |
237
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
238
|
case strings.HasPrefix(spath, "/download"): |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
239
|
rctx.URLParams.Add("*", strings.TrimLeft(strings.TrimPrefix(spath, "/download"), "/")) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
240
|
protect(http.HandlerFunc(repo.HandleDownload)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
241
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
242
|
case spath == "/refs": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
243
|
protect(http.HandlerFunc(repo.HandleRefs)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
244
|
case spath == "/edit": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
245
|
protect(http.HandlerFunc(repo.HandleEdit)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
246
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
247
|
case spath == "/info/refs": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
248
|
goit.HandleInfoRefs(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
249
|
case spath == "/git-upload-pack": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
250
|
goit.HandleUploadPack(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
251
|
case spath == "/git-receive-pack": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
252
|
goit.HandleReceivePack(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
253
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
254
|
default: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
255
|
goit.HttpError(w, http.StatusNotFound) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
256
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
257
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
258
|
case http.MethodPost: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
259
|
switch { |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
260
|
case spath == "/edit": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
261
|
protect(http.HandlerFunc(repo.HandleEdit)).ServeHTTP(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
262
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
263
|
case spath == "/git-upload-pack": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
264
|
goit.HandleUploadPack(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
265
|
case spath == "/git-receive-pack": |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
266
|
goit.HandleReceivePack(w, r) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
267
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
268
|
|
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
269
|
default: |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
270
|
goit.HttpError(w, http.StatusNotFound) |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
271
|
} |
84906b4 |
Jakob Wakeling |
2023-12-06 18:33:45 |
272
|
} |
|
|
|
273
|
|