aboutsummaryrefslogtreecommitdiff
path: root/cmd/goget/main.go
blob: 3f17448fa0e9ac2738a837838fda190ebf3faaf1 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"slices"
	"strings"
	"syscall"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"golang.org/x/mod/semver"
)

const (
	forgeDomain = "code.fcuny.net"
	forgeUser   = "fcuny"
	goPkgDomain = "go.fcuny.net"
)

var (
	goGetReqs = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "goget_requests_total",
		Help: "go get requests processed, by repository name.",
	}, []string{"name"})

	modules = []string{"x"}
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	metricsMux := http.NewServeMux()
	metricsMux.Handle("/metrics", promhttp.Handler())
	metricsServer := &http.Server{
		Addr: ":9091", Handler: metricsMux,
		ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
	}

	go func() {
		log.Println("Starting metrics server on :9091")
		if err := metricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Printf("Metrics server error: %v", err)
		}
	}()

	s := &http.Server{
		Addr:         ":8070",
		Handler:      handler(),
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout:  1 * time.Minute,
	}

	go func() {
		log.Println("Starting main server on :8080")
		if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Printf("Main server error: %v", err)
		}
	}()

	<-ctx.Done()
	log.Println("Shutdown signal received, starting graceful shutdown...")

	shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer shutdownCancel()

	var shutdownErr error

	log.Println("Shutting down main server...")
	if err := s.Shutdown(shutdownCtx); err != nil {
		log.Printf("Main server shutdown error: %v", err)
		shutdownErr = err
	}

	log.Println("Shutting down metrics server...")
	if err := metricsServer.Shutdown(shutdownCtx); err != nil {
		log.Printf("Metrics server shutdown error: %v", err)
		if shutdownErr == nil {
			shutdownErr = err
		}
	}

	if shutdownErr != nil {
		log.Printf("Shutdown completed with errors")
		os.Exit(1)
	}

	log.Println("Shutdown completed successfully")
}

func handler() http.Handler {
	mux := http.NewServeMux()

	mux.Handle(goPkgDomain+"/{name}", siteHandler(modules))
	mux.Handle(goPkgDomain+"/{name}/", siteHandler(modules))

	goGetMux := http.NewServeMux()
	for _, name := range modules {
		module := goPkgDomain + "/" + name
		goGetMux.Handle(
			module+"/",
			goImportHandler(module, "https://"+forgeDomain+"/"+forgeUser+"/"+name),
		)
	}

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet {
			status := http.StatusMethodNotAllowed
			http.Error(rw, http.StatusText(status), status)
			return
		}

		if r.URL.Query().Get("go-get") == "1" {
			goGetMux.ServeHTTP(rw, r)
			return
		}
		mux.ServeHTTP(rw, r)
	})
}

func goImportHandler(module, repo string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		goGetReqs.WithLabelValues(module).Inc()
		w.Header().Set("Content-Type", "text/html; charset=UTF-8")
		if _, err := fmt.Fprintf(w, `<head><meta name="go-import" content="%s git %s">`, module, repo); err != nil {
			log.Printf("Error writing response: %v", err)
		}
	})
}

func siteHandler(names []string) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		name := r.PathValue("name")
		name, version, hasV := strings.Cut(name, "@")
		if !hasV {
			name, _, _ = strings.Cut(name, ".")
		}
		if hasV && !semver.IsValid(version) || !slices.Contains(names, name) {
			http.NotFound(rw, r)
			return
		}
		u := &url.URL{
			Scheme: "https",
			Host:   forgeDomain,
			Path:   forgeUser + r.URL.Path,
		}
		if !hasV {
			path, symbol, hasSymbol := strings.Cut(r.URL.Path, ".")
			if hasSymbol {
				u.Path = forgeUser + "/" + path
				u.Fragment = symbol
			}
		}
		http.Redirect(rw, r, u.String(), http.StatusFound)
	})
}