2 godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 "github.com/davecgh/go-spew/spew"
35 "golang.org/x/net/netutil"
37 "go.stargrave.org/godlighty"
38 _ "go.stargrave.org/godlighty/rc/cfg"
43 var GracefulTime = 10 * time.Second
46 bind := flag.String("bind", "[::]:80", "Address to bind and listen on")
47 doTLS := flag.Bool("tls", false, "Enable TLS")
48 doSetUID := flag.Int("setuid", 0, "Set that UID after binding the socket")
49 doSetGID := flag.Int("setgid", 0, "Set that GID after binding the socket")
50 doSetGIDs := flag.String("setgids", "", "Comma-separated GIDs to set")
51 log.SetFlags(log.Lshortfile)
52 log.SetOutput(os.Stdout)
55 godlighty.LoadCertificates()
57 shutdown := make(chan os.Signal)
58 signal.Notify(shutdown, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
59 exitErr := make(chan error)
60 l, err := net.Listen("tcp", *bind)
66 if err := syscall.Setregid(*doSetGID, *doSetGID); err != nil {
72 if err := syscall.Setgroups([]int{*doSetGID}); err != nil {
78 for _, g := range strings.Split(*doSetGIDs, ",") {
79 gid, err := strconv.Atoi(g)
83 gids = append(gids, gid)
85 if err := syscall.Setgroups(gids); err != nil {
90 if err := syscall.Setreuid(*doSetUID, *doSetUID); err != nil {
95 info := make(chan os.Signal)
96 signal.Notify(info, syscall.SIGINFO)
100 spew.Fdump(os.Stdout, godlighty.Hosts)
104 srv := http.Server{Handler: godlighty.MainHandler}
107 log.Println("shutting down")
108 ctx, cancel := context.WithTimeout(context.TODO(), GracefulTime)
109 exitErr <- srv.Shutdown(ctx)
114 tlsCfg := godlighty.NewTLSConfig()
115 ll = tls.NewListener(netutil.LimitListener(l, MaxConns), tlsCfg)
117 ll = netutil.LimitListener(l, MaxConns)
123 "hosts:", len(godlighty.Hosts),
125 if err = srv.Serve(ll); err != http.ErrServerClosed {
128 if err := <-exitErr; err != nil {