Home

ugit @65f464aacac0e3d1e7f340cd6417636f2ece259f - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / cmd / ugitd / main.go
- raw
 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
package main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"os/signal"

	"go.jolheiser.com/ugit/internal/http"
	"go.jolheiser.com/ugit/internal/ssh"

	"github.com/charmbracelet/log"
	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-git/go-git/v5/utils/trace"
)

func main() {
	args, err := parseArgs(os.Args[1:])
	if err != nil {
		if errors.Is(err, flag.ErrHelp) {
			return
		}
		panic(err)
	}

	if args.Debug {
		trace.SetTarget(trace.Packet)
		log.SetLevel(log.DebugLevel)
	} else {
		middleware.DefaultLogger = http.NoopLogger
		ssh.DefaultLogger = ssh.NoopLogger
	}

	if err := os.MkdirAll(args.RepoDir, os.ModePerm); err != nil {
		panic(err)
	}

	sshSettings := ssh.Settings{
		AuthorizedKeys: args.SSH.AuthorizedKeys,
		CloneURL:       args.SSH.CloneURL,
		Port:           args.SSH.Port,
		HostKey:        args.SSH.HostKey,
		RepoDir:        args.RepoDir,
	}
	sshSrv, err := ssh.New(sshSettings)
	if err != nil {
		panic(err)
	}
	go func() {
		fmt.Printf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
		if err := sshSrv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	httpSettings := http.Settings{
		Title:       args.Meta.Title,
		Description: args.Meta.Description,
		CloneURL:    args.HTTP.CloneURL,
		Port:        args.HTTP.Port,
		RepoDir:     args.RepoDir,
		Profile: http.Profile{
			Username: args.Profile.Username,
			Email:    args.Profile.Email,
		},
	}
	for _, link := range args.Profile.Links {
		httpSettings.Profile.Links = append(httpSettings.Profile.Links, http.Link{
			Name: link.Name,
			URL:  link.URL,
		})
	}
	httpSrv := http.New(httpSettings)
	go func() {
		fmt.Printf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
		if err := httpSrv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Kill, os.Interrupt)
	<-ch
}