diff --git a/installer/REAME.md b/installer/REAME.md new file mode 100644 index 0000000..243a0e0 --- /dev/null +++ b/installer/REAME.md @@ -0,0 +1,2 @@ +https://gowebexamples.com/forms/ +https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/04.1.html diff --git a/installer/css/style.css b/installer/css/style.css new file mode 100644 index 0000000..f34cba5 --- /dev/null +++ b/installer/css/style.css @@ -0,0 +1,10 @@ +* { font-family:Arial;} +body { background:#000; color:#fff; margin:0 10vw;} +h1,h2,h3 {padding:12px 10px;color:#0f0;} +strong {color:#0f0;} +p { padding:12px 10px;} +p.error {background:#f00;} +input { border:0; background-color:transparent;color:#fff;border-bottom:1px solid #fff; margin-bottom:24px; padding:12px 10px; width:100%;} +input[type=submit] { border:0;font-size:1.2em; cursor:pointer; background:#fff;color:#000;transition:0.1s;} +input[type=submit]:hover { background:#0f0;} +footer { font-size:0.8em; padding:12px 10px; opacity:0.8;} diff --git a/installer/main.go b/installer/main.go new file mode 100644 index 0000000..3dd15b4 --- /dev/null +++ b/installer/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "html/template" + "net/http" + "fmt" + "encoding/json" + "io/ioutil" + "regexp" + "strings" + "os" + "time" +) + +type InstallerConfig struct { + Title string `json:"title"` + BaseUrl string `json:"baseurl"` + Server ServerConfig `json:"server"` + UploadPath string `json:"upload_path"` + LogLevel string `json:"log_level"` + LogPath string `json:"log_path"` + DB DBConfig `json:"db"` + AdminEmail string `json:"admin_email"` +} + +type ServerConfig struct { + Host string `json:"host"` + Port int `json:"port"` +} + +type DBConfig struct { + Dialec string `json:"dialect"` + Storage string `json:"storage"` + Logging bool `json:"logging"` +} + +type Message struct { + Success bool + Title string + BaseUrl string + Email string + Errors map[string]string +} + +var rxEmail = regexp.MustCompile(".+@.+\\..+") +var rxUrl = regexp.MustCompile(`^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)`) +var CONFIG_PATH = "/data/config.json" +var DB_PATH = "/data/db.sqlite" +var UPLOADS_PATH = "/data/uploads" +var LOGS_PATH = "/data/logs" + +func main() { + fmt.Println("Gancio Installer") + tmpl := template.Must(template.ParseFiles("templates/_base.html","templates/install.html")) + http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css")))) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + + msg := Message{} + + if r.Method != http.MethodPost { + // TODO: GET INSTANCE URL FROM DOCKER GANCIO + msg.BaseUrl = "https://intancia.convoca.la" + msg.Success = false + tmpl.ExecuteTemplate(w,"base",msg) + return + } + + // Validation + msg.Errors = make(map[string]string) + msg.Email = r.FormValue("adminemail") + msg.Title = r.FormValue("title") + msg.BaseUrl = r.FormValue("url") + msg.Success = false + + // Email + match := rxEmail.Match([]byte(msg.Email)) + if match == false { + msg.Errors["Email"] = "Introduce una dirección de correo correcta" + tmpl.ExecuteTemplate(w,"base",msg) + return + } + + // Url + match = rxUrl.Match([]byte(msg.BaseUrl)) + if match == false { + msg.Errors["BaseUrl"] = "Introduce una URL correcta" + tmpl.ExecuteTemplate(w,"base",msg) + return + } + + // Title + if strings.TrimSpace(msg.Title) == "" { + msg.Errors["Title"] = "Introduce un nombre para tu instancia" + tmpl.ExecuteTemplate(w,"base",msg) + return + } + + // Gancio Config + serverConfig := ServerConfig{ + Host: "0.0.0.0", + Port: 13120, + } + + dbConfig := DBConfig{ + Dialec: "sqlite", + Storage: DB_PATH, + Logging: false, + } + + gancioConfig := InstallerConfig{ + Title: r.FormValue("title"), + BaseUrl: r.FormValue("url"), + Server: serverConfig, + UploadPath: UPLOADS_PATH, + LogLevel: "debug", + LogPath: LOGS_PATH, + DB: dbConfig, + AdminEmail: msg.Email, + } + + // Write file + file, err := json.MarshalIndent(gancioConfig, "", " ") + err = ioutil.WriteFile(CONFIG_PATH, file, 0644) + + if err!=nil { + msg.Errors["WriteConfig"] = "Fallo al escribir el fichero de configuracion" + tmpl.ExecuteTemplate(w,"base",msg) + return + } + + // Success + msg.Success = true + tmpl.ExecuteTemplate(w,"base",msg) + + //Bye + go func() { + time.Sleep(10 * time.Second) + os.Exit(3) + }() + + }) + + fmt.Println("Runing server http://localhost:8080") + http.ListenAndServe(":8080", nil) +} diff --git a/installer/templates/._base.html.swp b/installer/templates/._base.html.swp new file mode 100644 index 0000000..5e78512 Binary files /dev/null and b/installer/templates/._base.html.swp differ diff --git a/installer/templates/_base.html b/installer/templates/_base.html new file mode 100644 index 0000000..9808019 --- /dev/null +++ b/installer/templates/_base.html @@ -0,0 +1,15 @@ +{{define "base"}} + + + + + + +

Herramienta de configuración de Gancio

+{{template "content" .}} + + + +{{end}} diff --git a/installer/templates/install.html b/installer/templates/install.html new file mode 100644 index 0000000..d36359e --- /dev/null +++ b/installer/templates/install.html @@ -0,0 +1,53 @@ +{{define "content"}} + + +{{with .Errors.Email}} +

{{.}}

+{{end}} +{{with .Errors.Title}} +

{{.}}

+{{end}} +{{with .Errors.BaseUrl}} +

{{.}}

+{{end}} +{{with .Errors.WriteConfig}} +

{{.}}

+{{end}} + + +{{if .Success}} +

Instalación completada

+

¡Felicidades! Tu instancia {{.Title}} ha sido configurada correctamente.

+

Un correo electrónico ha sido enviado a {{.Email}} con los pasos para recuperar la contraseña de administración.

+

En breve podrás acceder a {{.BaseUrl}} para publicar los eventos.

+

Disfruta de un café y vuelve en . segundos

+ + {{else}} +
+
+
+
+
+
+
+ +
+ +{{end}} + +{{end}}