Goit

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

Goit/src/goit/http.go (65 lines, 2.1 KiB) -rw-r--r-- file download

6727af8 Jakob Wakeling 2023-07-19 23:43:37
0
// Copyright (C) 2023, Jakob Wakeling
6727af8 Jakob Wakeling 2023-07-19 23:43:37
1
// All rights reserved.
6727af8 Jakob Wakeling 2023-07-19 23:43:37
2
8abd343 Jakob Wakeling 2023-07-19 00:40:45
3
package goit
8abd343 Jakob Wakeling 2023-07-19 00:40:45
4
8abd343 Jakob Wakeling 2023-07-19 00:40:45
5
import (
8abd343 Jakob Wakeling 2023-07-19 00:40:45
6
	"fmt"
8abd343 Jakob Wakeling 2023-07-19 00:40:45
7
	"html/template"
e530f2c Jakob Wakeling 2023-12-17 22:28:16
8
	"net"
8abd343 Jakob Wakeling 2023-07-19 00:40:45
9
	"net/http"
8abd343 Jakob Wakeling 2023-07-19 00:40:45
10
8abd343 Jakob Wakeling 2023-07-19 00:40:45
11
	"github.com/Jamozed/Goit/res"
8abd343 Jakob Wakeling 2023-07-19 00:40:45
12
)
8abd343 Jakob Wakeling 2023-07-19 00:40:45
13
64718e1 Jakob Wakeling 2023-07-24 21:56:07
14
var Tmpl = template.Must(template.New("error").Parse(res.Error))
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
15
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
16
func init() {
64718e1 Jakob Wakeling 2023-07-24 21:56:07
17
	template.Must(Tmpl.New("index").Parse(res.Index))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
18
	template.Must(Tmpl.New("base/head").Parse(res.BaseHead))
d631c5e Jakob Wakeling 2023-07-20 23:13:39
19
3c55d1d Jakob Wakeling 2023-11-27 21:07:45
20
	template.Must(Tmpl.New("admin/header").Parse(res.AdminHeader))
83800a4 Jakob Wakeling 2023-12-25 23:04:58
21
	template.Must(Tmpl.New("admin/status").Parse(res.AdminStatus))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
22
	template.Must(Tmpl.New("admin/users").Parse(res.AdminUsers))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
23
	template.Must(Tmpl.New("admin/user/create").Parse(res.AdminUserCreate))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
24
	template.Must(Tmpl.New("admin/user/edit").Parse(res.AdminUserEdit))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
25
	template.Must(Tmpl.New("admin/repos").Parse(res.AdminRepos))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
26
	template.Must(Tmpl.New("admin/repo/edit").Parse(res.AdminRepoEdit))
594ec41 Jakob Wakeling 2023-12-29 15:18:46
27
	template.Must(Tmpl.New("admin/cron").Parse(res.AdminCron))
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
28
64718e1 Jakob Wakeling 2023-07-24 21:56:07
29
	template.Must(Tmpl.New("user/header").Parse(res.UserHeader))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
30
	template.Must(Tmpl.New("user/login").Parse(res.UserLogin))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
31
	template.Must(Tmpl.New("user/sessions").Parse(res.UserSessions))
d0994fe Jakob Wakeling 2023-09-07 22:53:16
32
	template.Must(Tmpl.New("user/edit").Parse(res.UserEdit))
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
33
64718e1 Jakob Wakeling 2023-07-24 21:56:07
34
	template.Must(Tmpl.New("repo/header").Parse(res.RepoHeader))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
35
	template.Must(Tmpl.New("repo/create").Parse(res.RepoCreate))
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
36
	template.Must(Tmpl.New("repo/edit").Parse(res.RepoEdit))
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
37
64718e1 Jakob Wakeling 2023-07-24 21:56:07
38
	template.Must(Tmpl.New("repo/log").Parse(res.RepoLog))
3e159d3 Jakob Wakeling 2023-08-06 23:29:48
39
	template.Must(Tmpl.New("repo/commit").Parse(res.RepoCommit))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
40
	template.Must(Tmpl.New("repo/tree").Parse(res.RepoTree))
5aa454c Jakob Wakeling 2023-07-27 17:45:56
41
	template.Must(Tmpl.New("repo/file").Parse(res.RepoFile))
a0ac27c Jakob Wakeling 2024-07-06 23:32:05
42
	template.Must(Tmpl.New("repo/blame").Parse(res.RepoBlame))
64718e1 Jakob Wakeling 2023-07-24 21:56:07
43
	template.Must(Tmpl.New("repo/refs").Parse(res.RepoRefs))
17c34f4 Jakob Wakeling 2023-07-19 22:07:36
44
}
8abd343 Jakob Wakeling 2023-07-19 00:40:45
45
8abd343 Jakob Wakeling 2023-07-19 00:40:45
46
func HttpError(w http.ResponseWriter, code int) {
8abd343 Jakob Wakeling 2023-07-19 00:40:45
47
	w.WriteHeader(code)
8abd343 Jakob Wakeling 2023-07-19 00:40:45
48
	s := fmt.Sprint(code) + " " + http.StatusText(code)
64718e1 Jakob Wakeling 2023-07-24 21:56:07
49
	Tmpl.ExecuteTemplate(w, "error", struct{ Status string }{s})
8abd343 Jakob Wakeling 2023-07-19 00:40:45
50
}
8abd343 Jakob Wakeling 2023-07-19 00:40:45
51
8abd343 Jakob Wakeling 2023-07-19 00:40:45
52
func HttpNotFound(w http.ResponseWriter, r *http.Request) {
8abd343 Jakob Wakeling 2023-07-19 00:40:45
53
	HttpError(w, http.StatusNotFound)
8abd343 Jakob Wakeling 2023-07-19 00:40:45
54
}
e530f2c Jakob Wakeling 2023-12-17 22:28:16
55
e530f2c Jakob Wakeling 2023-12-17 22:28:16
56
func Ip(r *http.Request) string {
e530f2c Jakob Wakeling 2023-12-17 22:28:16
57
	if fip := r.Header.Get("X-Forwarded-For"); Conf.IpForwarded && fip != "" {
e530f2c Jakob Wakeling 2023-12-17 22:28:16
58
		return fip
e530f2c Jakob Wakeling 2023-12-17 22:28:16
59
	}
e530f2c Jakob Wakeling 2023-12-17 22:28:16
60
e530f2c Jakob Wakeling 2023-12-17 22:28:16
61
	ip, _, _ := net.SplitHostPort(r.RemoteAddr)
e530f2c Jakob Wakeling 2023-12-17 22:28:16
62
	return ip
e530f2c Jakob Wakeling 2023-12-17 22:28:16
63
}
64