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
|
/**
* AltGit - altgit
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paolo Lulli <kevwe.com>
* @copyright Paolo Lulli 2026
*/
package main
import (
"altgit/internal/config"
"altgit/internal/routes"
"altgit/internal/webauth"
"embed"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
githttp "github.com/AaronO/go-git-http"
"github.com/AaronO/go-git-http/auth"
"github.com/gin-gonic/gin"
)
//go:embed "static"
var EmbeddedStaticFiles embed.FS
func main() {
var instanceName string
args := os.Args
if len(args) < 2 {
instanceName = "default"
fmt.Println("Loading default config")
} else {
instanceName = args[1]
fmt.Printf("Loading instance: [%s] config", instanceName)
}
configPath := os.Getenv("HOME") + "/.config/altgit-" + instanceName + ".json"
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
fmt.Printf("Config file %s doesn't exist\n", configPath)
os.Exit(1)
}
cfg := config.GetClientConfig(configPath)
//var c = RepositoryBrowserApp{Config: cfg}
r := gin.Default()
err := r.SetTrustedProxies([]string{"127.0.0.1"})
if err != nil {
return
}
// templates must exist on the target filesystem
//r.LoadHTMLGlob("templates/*")
if cfg.UiOverrideStatic != "" {
if _, err := os.Stat(cfg.UiOverrideStatic); err != nil {
log.Fatal(err)
}
r.Static("/static", cfg.UiOverrideStatic)
} else {
staticFs, err := fs.Sub(EmbeddedStaticFiles, "static")
if err != nil {
log.Fatal(err)
}
r.StaticFS("/static", http.FS(staticFs))
}
addGitDataHandler(cfg, r)
//---------------------------------
r.GET("/", routes.HandleRepoList(cfg))
repo := r.Group("/:repo")
{
repo.GET("/", routes.HandleIndex(cfg))
repo.GET("/refs", routes.HandleRefs(cfg))
repo.GET("/log", routes.HandleLog(cfg))
repo.GET("/commit", routes.HandleCommit(cfg))
repo.GET("/history", routes.HandleHistory(cfg))
repo.GET("/view", routes.HandleView(cfg))
repo.GET("/raw", routes.HandleRaw(cfg))
//
repo.GET("/diff", routes.HandleDiff(cfg))
}
var iface string
if "" == cfg.Interface {
iface = "0.0.0.0:8080"
} else {
iface = cfg.Interface
}
fmt.Printf("Starting altgit on: -> http://%s\n", iface)
if cfg.TlsCertificate == "" || cfg.TlsKeyFile == "" {
err := r.Run(iface)
if err != nil {
log.Fatal("Could not start server", err)
}
} else {
err := r.RunTLS(cfg.Interface, cfg.TlsCertificate, cfg.TlsKeyFile)
if err != nil {
log.Fatal("Could not start TLS server", err)
}
}
}
func addGitDataHandler(cfg config.AltGitConfiguration, r *gin.Engine) {
var gitHandler http.Handler
if cfg.Readonly == "false" && cfg.Authentication == "false" {
gitHandler = githttp.New(cfg.Root)
} else {
GitAuthenticator := auth.Authenticator(func(info auth.AuthInfo) (bool, error) {
return webauth.AuthorizeReadOnly(info)
})
gitHandler = GitAuthenticator(githttp.New(cfg.Root))
}
r.Any("/git/*repoPath", func(c *gin.Context) {
c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, "/git")
c.Request.RequestURI = strings.TrimPrefix(c.Request.RequestURI, "/git")
gitHandler.ServeHTTP(c.Writer, c.Request)
})
}
|