2018-10-25 15:15:10 +02:00
package config
import (
"fmt"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-wordwrap"
2018-11-10 04:16:13 +01:00
"github.com/writeas/web-core/auth"
2018-10-25 15:15:10 +02:00
"strconv"
)
2018-11-10 04:16:13 +01:00
type SetupData struct {
User * UserCreation
Config * Config
}
2018-12-08 23:49:19 +01:00
func Configure ( fname string ) ( * SetupData , error ) {
2018-11-10 04:16:13 +01:00
data := & SetupData { }
var err error
2018-12-08 23:49:19 +01:00
if fname == "" {
fname = FileName
}
2018-11-10 04:16:13 +01:00
2018-12-08 23:49:19 +01:00
data . Config , err = Load ( fname )
2018-11-08 07:31:01 +01:00
var action string
2018-12-08 19:36:51 +01:00
isNewCfg := false
2018-10-25 15:15:10 +02:00
if err != nil {
2018-12-08 23:49:19 +01:00
fmt . Printf ( "No %s configuration yet. Creating new.\n" , fname )
2018-11-10 04:16:13 +01:00
data . Config = New ( )
2018-11-08 07:31:01 +01:00
action = "generate"
2018-12-08 19:36:51 +01:00
isNewCfg = true
2018-10-25 15:15:10 +02:00
} else {
2018-12-08 23:49:19 +01:00
fmt . Printf ( "Loaded configuration %s.\n" , fname )
2018-11-08 07:31:01 +01:00
action = "update"
2018-10-25 15:15:10 +02:00
}
2018-11-11 03:44:41 +01:00
title := color . New ( color . Bold , color . BgGreen ) . PrintFunc ( )
2018-10-25 15:15:10 +02:00
intro := color . New ( color . Bold , color . FgWhite ) . PrintlnFunc ( )
fmt . Println ( )
intro ( " ✍ Write Freely Configuration ✍" )
fmt . Println ( )
2018-12-08 23:49:19 +01:00
fmt . Println ( wordwrap . WrapString ( " This quick configuration process will " + action + " the application's config file, " + fname + ".\n\n It validates your input along the way, so you can be sure any future errors aren't caused by a bad configuration. If you'd rather configure your server manually, instead run: writefreely --create-config and edit that file." , 75 ) )
2018-10-25 15:15:10 +02:00
fmt . Println ( )
title ( " Server setup " )
fmt . Println ( )
2018-11-08 04:06:34 +01:00
tmpls := & promptui . PromptTemplates {
Success : "{{ . | bold | faint }}: " ,
2018-10-25 15:15:10 +02:00
}
2018-11-08 04:06:34 +01:00
selTmpls := & promptui . SelectTemplates {
2018-11-08 18:16:52 +01:00
Selected : fmt . Sprintf ( ` {{ .Label }} {{ . | faint }} ` ) ,
2018-10-25 15:15:10 +02:00
}
2018-11-22 00:26:19 +01:00
// Environment selection
selPrompt := promptui . Select {
Templates : selTmpls ,
Label : "Environment" ,
Items : [ ] string { "Development" , "Production, standalone" , "Production, behind reverse proxy" } ,
2018-10-25 15:15:10 +02:00
}
2018-11-22 00:26:19 +01:00
_ , envType , err := selPrompt . Run ( )
2018-10-25 15:15:10 +02:00
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-22 00:26:19 +01:00
isDevEnv := envType == "Development"
isStandalone := envType == "Production, standalone"
data . Config . Server . Dev = isDevEnv
var prompt promptui . Prompt
if isDevEnv || ! isStandalone {
// Running in dev environment or behind reverse proxy; ask for port
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Local port" ,
Validate : validatePort ,
Default : fmt . Sprintf ( "%d" , data . Config . Server . Port ) ,
}
port , err := prompt . Run ( )
if err != nil {
return data , err
}
data . Config . Server . Port , _ = strconv . Atoi ( port ) // Ignore error, as we've already validated number
}
if isStandalone {
selPrompt = promptui . Select {
Templates : selTmpls ,
Label : "Web server mode" ,
Items : [ ] string { "Insecure (port 80)" , "Secure (port 443)" } ,
}
sel , _ , err := selPrompt . Run ( )
if err != nil {
return data , err
}
if sel == 0 {
data . Config . Server . Port = 80
data . Config . Server . TLSCertPath = ""
data . Config . Server . TLSKeyPath = ""
} else if sel == 1 {
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
}
}
} else {
data . Config . Server . TLSCertPath = ""
data . Config . Server . TLSKeyPath = ""
}
2018-10-25 15:15:10 +02:00
fmt . Println ( )
title ( " Database setup " )
fmt . Println ( )
2018-12-08 19:36:51 +01:00
selPrompt = promptui . Select {
Templates : selTmpls ,
Label : "Database driver" ,
Items : [ ] string { "MySQL" , "SQLite" } ,
2018-10-25 15:15:10 +02:00
}
2018-12-08 19:36:51 +01:00
sel , _ , err := selPrompt . Run ( )
2018-10-25 15:15:10 +02:00
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-12-08 19:36:51 +01:00
if sel == 0 {
// Configure for MySQL
data . Config . UseMySQL ( isNewCfg )
2018-10-25 15:15:10 +02:00
2018-12-08 19:36:51 +01:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Username" ,
Validate : validateNonEmpty ,
Default : data . Config . Database . User ,
}
data . Config . Database . User , err = prompt . Run ( )
if err != nil {
return data , err
}
2018-10-25 15:15:10 +02:00
2018-12-08 19:36:51 +01:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Password" ,
Validate : validateNonEmpty ,
Default : data . Config . Database . Password ,
Mask : '*' ,
}
data . Config . Database . Password , err = prompt . Run ( )
if err != nil {
return data , err
}
2018-10-25 15:15:10 +02:00
2018-12-08 19:36:51 +01:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Database name" ,
Validate : validateNonEmpty ,
Default : data . Config . Database . Database ,
}
data . Config . Database . Database , err = prompt . Run ( )
if err != nil {
return data , err
}
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Host" ,
Validate : validateNonEmpty ,
Default : data . Config . Database . Host ,
}
data . Config . Database . Host , err = prompt . Run ( )
if err != nil {
return data , err
}
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Port" ,
Validate : validatePort ,
Default : fmt . Sprintf ( "%d" , data . Config . Database . Port ) ,
}
dbPort , err := prompt . Run ( )
if err != nil {
return data , err
}
data . Config . Database . Port , _ = strconv . Atoi ( dbPort ) // Ignore error, as we've already validated number
} else if sel == 1 {
// Configure for SQLite
data . Config . UseSQLite ( isNewCfg )
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Filename" ,
Validate : validateNonEmpty ,
Default : data . Config . Database . FileName ,
}
data . Config . Database . FileName , err = prompt . Run ( )
if err != nil {
return data , err
}
2018-10-25 15:15:10 +02:00
}
fmt . Println ( )
title ( " App setup " )
fmt . Println ( )
2018-11-22 00:26:19 +01:00
selPrompt = promptui . Select {
2018-11-08 04:06:34 +01:00
Templates : selTmpls ,
Label : "Site type" ,
Items : [ ] string { "Single user blog" , "Multi-user instance" } ,
2018-10-25 15:15:10 +02:00
}
_ , usersType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
}
data . Config . App . SingleUser = usersType == "Single user blog"
if data . Config . App . SingleUser {
data . User = & UserCreation { }
// prompt for username
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Admin username" ,
Validate : validateNonEmpty ,
}
data . User . Username , err = prompt . Run ( )
if err != nil {
return data , err
}
// prompt for password
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Admin password" ,
Validate : validateNonEmpty ,
}
newUserPass , err := prompt . Run ( )
if err != nil {
return data , err
}
data . User . HashedPass , err = auth . HashPass ( [ ] byte ( newUserPass ) )
if err != nil {
return data , err
}
2018-10-25 15:15:10 +02:00
}
siteNameLabel := "Instance name"
2018-11-10 04:16:13 +01:00
if data . Config . App . SingleUser {
2018-10-25 15:15:10 +02:00
siteNameLabel = "Blog name"
}
prompt = promptui . Prompt {
2018-11-08 04:06:34 +01:00
Templates : tmpls ,
Label : siteNameLabel ,
Validate : validateNonEmpty ,
2018-11-10 04:16:13 +01:00
Default : data . Config . App . SiteName ,
2018-10-25 15:15:10 +02:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . SiteName , err = prompt . Run ( )
2018-10-25 15:15:10 +02:00
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-08 04:06:34 +01:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Public URL" ,
Validate : validateDomain ,
2018-11-10 04:16:13 +01:00
Default : data . Config . App . Host ,
2018-11-08 04:06:34 +01:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . Host , err = prompt . Run ( )
2018-11-08 04:06:34 +01:00
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-11-08 04:06:34 +01:00
}
2018-11-10 04:16:13 +01:00
if ! data . Config . App . SingleUser {
2018-10-25 15:15:10 +02:00
selPrompt = promptui . Select {
2018-11-08 04:06:34 +01:00
Templates : selTmpls ,
Label : "Registration" ,
Items : [ ] string { "Open" , "Closed" } ,
2018-10-25 15:15:10 +02:00
}
_ , regType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . OpenRegistration = regType == "Open"
2018-11-08 04:06:34 +01:00
prompt = promptui . Prompt {
Templates : tmpls ,
Label : "Max blogs per user" ,
2018-11-10 04:16:13 +01:00
Default : fmt . Sprintf ( "%d" , data . Config . App . MaxBlogs ) ,
2018-11-08 04:06:34 +01:00
}
maxBlogs , err := prompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-11-08 04:06:34 +01:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . MaxBlogs , _ = strconv . Atoi ( maxBlogs ) // Ignore error, as we've already validated number
2018-10-25 15:15:10 +02:00
}
selPrompt = promptui . Select {
2018-11-08 04:06:34 +01:00
Templates : selTmpls ,
Label : "Federation" ,
Items : [ ] string { "Enabled" , "Disabled" } ,
2018-10-25 15:15:10 +02:00
}
_ , fedType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . Federation = fedType == "Enabled"
2018-10-25 15:15:10 +02:00
2018-11-10 04:16:13 +01:00
if data . Config . App . Federation {
2018-10-25 15:15:10 +02:00
selPrompt = promptui . Select {
2018-11-08 04:06:34 +01:00
Templates : selTmpls ,
Label : "Federation usage stats" ,
Items : [ ] string { "Public" , "Private" } ,
2018-10-25 15:15:10 +02:00
}
_ , fedStatsType , err := selPrompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . PublicStats = fedStatsType == "Public"
2018-10-25 15:15:10 +02:00
selPrompt = promptui . Select {
2018-11-08 04:06:34 +01:00
Templates : selTmpls ,
Label : "Instance metadata privacy" ,
Items : [ ] string { "Public" , "Private" } ,
2018-10-25 15:15:10 +02:00
}
_ , fedStatsType , err = selPrompt . Run ( )
if err != nil {
2018-11-10 04:16:13 +01:00
return data , err
2018-10-25 15:15:10 +02:00
}
2018-11-10 04:16:13 +01:00
data . Config . App . Private = fedStatsType == "Private"
2018-10-25 15:15:10 +02:00
}
2018-12-08 23:49:19 +01:00
return data , Save ( data . Config , fname )
2018-10-25 15:15:10 +02:00
}