From 1f7a0f0122f30e697d188baab832b9c8e049e04b Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 20 Jul 2019 21:46:10 -0400 Subject: [PATCH] Add option for automated cert in config process This adds a new "Secure (port 443), auto certificate" option to the "Web server mode" prompt when running `writefreely --config`. When chosen, it'll set `autocert` to `true` and set the path for certs and keys to `certs`. Ref T542 --- config/setup.go | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/config/setup.go b/config/setup.go index b1c0c37..fd5a632 100644 --- a/config/setup.go +++ b/config/setup.go @@ -101,39 +101,48 @@ func Configure(fname string, configSections string) (*SetupData, error) { selPrompt = promptui.Select{ Templates: selTmpls, Label: "Web server mode", - Items: []string{"Insecure (port 80)", "Secure (port 443)"}, + Items: []string{"Insecure (port 80)", "Secure (port 443), manual certificate", "Secure (port 443), auto certificate"}, } sel, _, err := selPrompt.Run() if err != nil { return data, err } if sel == 0 { + data.Config.Server.Autocert = false data.Config.Server.Port = 80 data.Config.Server.TLSCertPath = "" data.Config.Server.TLSKeyPath = "" - } else if sel == 1 { + } else if sel == 1 || sel == 2 { data.Config.Server.Port = 443 - - prompt = promptui.Prompt{ - Templates: tmpls, - Label: "Certificate path", - Validate: validateNonEmpty, - Default: data.Config.Server.TLSCertPath, - } - data.Config.Server.TLSCertPath, err = prompt.Run() - if err != nil { - return data, err - } - - prompt = promptui.Prompt{ - Templates: tmpls, - Label: "Key path", - Validate: validateNonEmpty, - Default: data.Config.Server.TLSKeyPath, - } - data.Config.Server.TLSKeyPath, err = prompt.Run() - if err != nil { - return data, err + data.Config.Server.Autocert = sel == 2 + + if sel == 1 { + // Manual certificate configuration + prompt = promptui.Prompt{ + Templates: tmpls, + Label: "Certificate path", + Validate: validateNonEmpty, + Default: data.Config.Server.TLSCertPath, + } + data.Config.Server.TLSCertPath, err = prompt.Run() + if err != nil { + return data, err + } + + prompt = promptui.Prompt{ + Templates: tmpls, + Label: "Key path", + Validate: validateNonEmpty, + Default: data.Config.Server.TLSKeyPath, + } + data.Config.Server.TLSKeyPath, err = prompt.Run() + if err != nil { + return data, err + } + } else { + // Automatic certificate + data.Config.Server.TLSCertPath = "certs" + data.Config.Server.TLSKeyPath = "certs" } } } else {