/** * AltGit - altgit * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2026 */ package main import ( "altgit/internal/config" "altgit/internal/handler" "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("/", handler.HandleRepoList(cfg)) repo := r.Group("/:repo") { repo.GET("/", handler.HandleIndex(cfg)) repo.GET("/refs", handler.HandleRefs(cfg)) repo.GET("/log", handler.HandleLog(cfg)) repo.GET("/commit", handler.HandleCommit(cfg)) repo.GET("/history", handler.HandleHistory(cfg)) repo.GET("/view", handler.HandleView(cfg)) repo.GET("/raw", handler.HandleRaw(cfg)) // repo.GET("/diff", handler.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 NewReadOnlyNoAuth(root string) *githttp.GitHttp { return &githttp.GitHttp{ ProjectRoot: root, GitBinPath: "/usr/bin/git", UploadPack: true, ReceivePack: false, } } func addGitDataHandler(cfg config.AltGitConfiguration, r *gin.Engine) { var gitHandler http.Handler if cfg.Authentication == "false" { if cfg.Readonly == "false" { gitHandler = githttp.New(cfg.Root) // No auth, RW } else { gitHandler = NewReadOnlyNoAuth(cfg.Root) // No auth, RO } } if cfg.Readonly != "false" && cfg.Authentication != "false" { GitAuthenticator := auth.Authenticator(func(info auth.AuthInfo) (bool, error) { return webauth.AuthorizeReadOnly(info) }) gitHandler = GitAuthenticator(githttp.New(cfg.Root)) //Http auth, RW } 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) }) }