diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2022-11-24 13:14:34 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2022-11-24 14:46:00 +0300 |
commit | 87f7fceed34bcafb8aaff351dd493a35c916986f (patch) | |
tree | 26809ec8f550aba8eb019e59adc3d48e51913eb2 /library/cpp/http/simple/ut/https_server/main.go | |
parent | 11bc4015b8010ae201bf3eb33db7dba425aca35e (diff) | |
download | ydb-87f7fceed34bcafb8aaff351dd493a35c916986f.tar.gz |
Ydb stable 22-4-4322.4.43
x-stable-origin-commit: 8d49d46cc834835bf3e50870516acd7376a63bcf
Diffstat (limited to 'library/cpp/http/simple/ut/https_server/main.go')
-rw-r--r-- | library/cpp/http/simple/ut/https_server/main.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/library/cpp/http/simple/ut/https_server/main.go b/library/cpp/http/simple/ut/https_server/main.go new file mode 100644 index 0000000000..4282810675 --- /dev/null +++ b/library/cpp/http/simple/ut/https_server/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +type Opts struct { + Port uint16 + KeyFile string + CertFile string +} + +func handler(writer http.ResponseWriter, request *http.Request) { + res := "pong.my" + + writer.Header().Set("Content-Type", "text/plain") + writer.WriteHeader(http.StatusOK) + + _, _ = writer.Write([]byte(res)) +} + +func runServer(opts *Opts) error { + mainMux := http.NewServeMux() + mainMux.Handle("/ping", http.HandlerFunc(handler)) + + server := &http.Server{ + Addr: fmt.Sprintf("localhost:%d", opts.Port), + Handler: mainMux, + ErrorLog: log.New(os.Stdout, "", log.LstdFlags), + } + + return server.ListenAndServeTLS(opts.CertFile, opts.KeyFile) +} + +func markFlagRequired(flags *pflag.FlagSet, names ...string) { + for _, n := range names { + name := n + if err := cobra.MarkFlagRequired(flags, name); err != nil { + panic(err) + } + } +} + +func main() { + opts := Opts{} + + cmd := cobra.Command{ + RunE: func(cmd *cobra.Command, args []string) error { + return runServer(&opts) + }, + } + + flags := cmd.Flags() + flags.Uint16Var(&opts.Port, "port", 0, "") + flags.StringVar(&opts.KeyFile, "keyfile", "", "path to key file") + flags.StringVar(&opts.CertFile, "certfile", "", "path to cert file") + + markFlagRequired(flags, "port", "keyfile", "certfile") + + if err := cmd.Execute(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Exit with err: %s", err) + os.Exit(1) + } +} |