main.go 2.09 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
package main

import (
	"flag"
	"fmt"
	"log"
ale's avatar
ale committed
7
	"net/http"
ale's avatar
ale committed
8
9
10
	"os"
	"os/signal"
	"syscall"
11
	"time"
ale's avatar
ale committed
12

13
	"git.autistici.org/ai3/go-common/unix"
ale's avatar
ale committed
14
	"github.com/coreos/go-systemd/daemon"
ale's avatar
ale committed
15
	"github.com/prometheus/client_golang/prometheus/promhttp"
16
17

	"git.autistici.org/id/auth/server"
ale's avatar
ale committed
18
19
20
)

var (
21
	configPath              = flag.String("config", "/etc/auth-server/config.yml", "configuration `file`")
ale's avatar
ale committed
22
23
	socketPath              = flag.String("socket", "/run/auth/socket", "`path` to the UNIX socket to listen on")
	systemdSocketActivation = flag.Bool("systemd-socket", false, "use SystemD socket activation")
ale's avatar
ale committed
24
	httpAddr                = flag.String("http-addr", "", "if not nil, bind an HTTP server to this `addr` for Prometheus metrics")
25
	requestTimeout          = flag.Duration("timeout", 5*time.Second, "timeout for incoming requests")
ale's avatar
ale committed
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
)

func usage() {
	fmt.Fprintf(os.Stderr, `authserv - a simple authentication server
Usage: authserv [<OPTIONS>]

Known options:
`)
	flag.PrintDefaults()
}

func main() {
	log.SetFlags(0)
	flag.Usage = usage
	flag.Parse()

	syscall.Umask(007)

	config, err := server.LoadConfig(*configPath)
	if err != nil {
		log.Fatalf("could not load configuration: %v", err)
	}

	authSrv, err := server.NewServer(config, nil)
	if err != nil {
		log.Fatalf("configuration error: %v", err)
	}

ale's avatar
ale committed
54
55
56
57
58
59
60
61
	if *httpAddr != "" {
		h := http.NewServeMux()
		h.Handle("/metrics", promhttp.Handler())
		go func() {
			log.Fatal(http.ListenAndServe(*httpAddr, h))
		}()
	}

62
	srv := unix.NewLineServer(server.NewSocketServer(authSrv))
63
	srv.RequestTimeout = *requestTimeout
64
65

	var sockSrv *unix.SocketServer
ale's avatar
ale committed
66
	if *systemdSocketActivation {
67
		sockSrv, err = unix.NewSystemdSocketServer(srv)
ale's avatar
ale committed
68
	} else {
69
		sockSrv, err = unix.NewUNIXSocketServer(*socketPath, srv)
ale's avatar
ale committed
70
71
72
73
74
	}
	if err != nil {
		log.Fatalf("error: %v", err)
	}

75
	done := make(chan struct{})
ale's avatar
ale committed
76
77
78
79
80
	sigCh := make(chan os.Signal, 1)
	go func() {
		<-sigCh
		log.Printf("terminating")
		sockSrv.Close()
81
		close(done)
ale's avatar
ale committed
82
83
84
85
86
	}()
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)

	log.Printf("starting")
	daemon.SdNotify(false, "READY=1")
87
88
89
	if err := sockSrv.Serve(); err != nil {
		log.Fatal(err)
	}
90
91

	<-done
ale's avatar
ale committed
92
}