Created
June 17, 2019 21:10
-
-
Save sashka/0489e2f17d42ad7098d6b74981535e42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"go.uber.org/zap" | |
"golang.org/x/net/netutil" | |
) | |
func main() { | |
testConfig := flag.Bool("t", false, "test the configuration file and exit") | |
configFile := flag.String("c", DefaultConfigPath, "configuration file to use instead of the default") | |
flag.Parse() | |
// Read server config | |
config, err := LoadConfig(*configFile) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "configuration file is invalid:", err) | |
os.Exit(1) | |
} | |
if *testConfig { | |
fmt.Fprintln(os.Stderr, "configuration file test passed successfully") | |
os.Exit(0) | |
} | |
// Basic server setup | |
srv := NewServerWithConfig(config) | |
defer srv.Close() | |
// Limit simultaneous requests. | |
listener, err := net.Listen("tcp", srv.config.Addr) | |
if err != nil { | |
panic(err) | |
} | |
defer listener.Close() | |
if srv.config.MaxConns > 0 { | |
listener = netutil.LimitListener(listener, srv.config.MaxConns) | |
} | |
srv.logger.Debug("starting web server", | |
zap.Int("limit", srv.config.MaxConns), | |
zap.String("addr", srv.config.Addr), | |
) | |
// Try to send notification to systemd. | |
// It fails when server has started manually, so don't panic on error. | |
if err := SdNotify("READY=1"); err != nil { | |
log.Printf("Failed to notify systemd: %s", err) | |
} | |
// Run web server | |
err = http.Serve(listener, nil) | |
log.Fatal(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment