aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: aa2ce53001a6341a91219415a8cec11650a5966b (plain)
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
// SPDX-FileCopyrightText: © 2019 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC

//go:build !aws

package main

import (
	"context"
	"flag"
	"log"
	"net/http"
	"os"
	"os/signal"
	"regexp"
	"syscall"
	"time"

	"go.awhk.org/core"
)

var (
	addr = flag.String("addr", "localhost:8080", "address to listen on")
	from = flag.String("from", "", "package prefix to remove")
	to   = flag.String("to", "", "repository prefix to add")
	vcs  = flag.String("vcs", "git", "version control system to signal")
)

func main() {
	flag.Parse()

	done := make(chan os.Signal, 1)
	signal.Notify(done, os.Interrupt, syscall.SIGTERM)

	srv := http.Server{Handler: &redirector{regexp.MustCompile(*from), *to, *vcs}}
	go func() {
		if err := srv.Serve(core.Must(core.Listen(*addr))); err != nil && err != http.ErrServerClosed {
			log.Fatalln("server.Serve:", err)
		}
	}()

	<-done
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
		log.Fatalln("server.Shutdown:", err)
	}
}