main.go (1119B)
1 package main 2 3 import ( 4 "os" 5 "os/signal" 6 "syscall" 7 8 "code.dwrz.net/src/cmd/web/config" 9 "code.dwrz.net/src/cmd/web/site" 10 "code.dwrz.net/src/pkg/log" 11 "code.dwrz.net/src/pkg/server" 12 ) 13 14 func main() { 15 var l = log.New(os.Stderr) 16 17 // Get the config. 18 cfg, err := config.New() 19 if err != nil { 20 l.Error.Fatalf("failed to get config: %v", err) 21 } 22 23 // Setup the site. 24 site, err := site.New(site.Params{ 25 Debug: cfg.Debug, 26 Log: l, 27 }) 28 if err != nil { 29 l.Error.Fatalf("failed to setup API: %v", err) 30 } 31 32 // Setup the HTTP(S) server(s). 33 srv, err := server.New(server.Parameters{ 34 Config: *cfg.Server, 35 Handler: site, 36 Log: l, 37 }) 38 if err != nil { 39 l.Error.Fatalf("failed to create server: %v", err) 40 } 41 42 // Serve the site. 43 srv.Serve() 44 45 // Listen for OS signals. 46 osListener := make(chan os.Signal, 1) 47 signal.Notify( 48 osListener, 49 syscall.SIGTERM, syscall.SIGINT, 50 ) 51 52 // Block until we receive a signal. 53 s := <-osListener 54 l.Debug.Printf("received signal: %s", s) 55 56 if err := srv.Shutdown(); err != nil { 57 l.Error.Fatalf("failed to shutdown server: %v", err) 58 } 59 60 l.Debug.Printf("terminating") 61 }