ExampleNewServer demonstrates how to set up an authentication server.
()
| 54 | |
| 55 | // ExampleNewServer demonstrates how to set up an authentication server. |
| 56 | func ExampleNewServer() { |
| 57 | // Set up a logger. |
| 58 | logger := log.MustNewLogger(config.LogConfig{ |
| 59 | Level: config.LogLevelWarning, |
| 60 | Format: config.LogFormatText, |
| 61 | Destination: config.LogDestinationStdout, |
| 62 | Stdout: os.Stdout, |
| 63 | }) |
| 64 | |
| 65 | // Create a new auth webhook server. |
| 66 | srv, err := webhook.NewServer( |
| 67 | config.HTTPServerConfiguration{ |
| 68 | Listen: "0.0.0.0:8001", |
| 69 | }, |
| 70 | // Pass your handler here. |
| 71 | &myAuthReqHandler{}, |
| 72 | logger, |
| 73 | ) |
| 74 | if err != nil { |
| 75 | // Handle error |
| 76 | panic(err) |
| 77 | } |
| 78 | |
| 79 | // Set up and run the web server service. |
| 80 | lifecycle := service.NewLifecycle(srv) |
| 81 | |
| 82 | go func() { |
| 83 | //Ignore error, handled later. |
| 84 | _ = lifecycle.Run() |
| 85 | }() |
| 86 | |
| 87 | // Sleep for 30 seconds as a test. |
| 88 | time.Sleep(30 * time.Second) |
| 89 | |
| 90 | // Set up a shutdown context to give a deadline for graceful shutdown. |
| 91 | shutdownContext, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 92 | defer cancel() |
| 93 | // Stop the server. |
| 94 | lifecycle.Stop(shutdownContext) |
| 95 | |
| 96 | // Wait for the server to stop. |
| 97 | lastError := lifecycle.Wait() |
| 98 | if lastError != nil { |
| 99 | // Server stopped abnormally. |
| 100 | panic(lastError) |
| 101 | } |
| 102 | |
| 103 | // Output: |
| 104 | } |
nothing calls this directly
no test coverage detected