Adaptación del Docker publicado en https://git.sindominio.net/gancio/docker-gancio para su uso en Estibadores
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
4.7 KiB
205 lines
4.7 KiB
package main |
|
|
|
import ( |
|
"html/template" |
|
"net/http" |
|
"fmt" |
|
"encoding/json" |
|
"io/ioutil" |
|
"regexp" |
|
"strings" |
|
"strconv" |
|
"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"` |
|
SMTP SMTPConfig `json:"smtp"` |
|
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 SMTPConfig struct { |
|
Auth AuthSMTPConfig `json:"auth"` |
|
Secure bool `json:"secure"` |
|
Host string `json:"host"` |
|
Port int `json:"port"` |
|
} |
|
|
|
type AuthSMTPConfig struct { |
|
User string `json:"user"` |
|
Pass string `json:"pass"` |
|
} |
|
|
|
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" |
|
var LISTEN_PORT = ":13120" |
|
var LOG_LEVEL = "debug" |
|
var SMTP_USER = "gancio@sindominio.net" |
|
var SMTP_PASS = "secret" |
|
var SMTP_SECURE = true |
|
var SMTP_PORT = 465 |
|
var SMTP_HOST = "smtp.sindominio.net" |
|
var TEMPLATE_PATH = "." |
|
|
|
func main() { |
|
// Environment |
|
// Template |
|
if os.Getenv("TEMPLATE") != "" { |
|
TEMPLATE_PATH = os.Getenv("TEMPLATE") |
|
} |
|
// SMTP host |
|
if os.Getenv("SMTP_HOST") != "" { |
|
SMTP_HOST = os.Getenv("SMTP_HOST") |
|
} |
|
// SMTP port |
|
if os.Getenv("SMTP_PORT") != "" { |
|
SMTP_PORT,_ = strconv.Atoi(os.Getenv("SMTP_PORT")) |
|
} |
|
// SMTP user |
|
if os.Getenv("SMTP_USER") != "" { |
|
SMTP_USER = os.Getenv("SMTP_USER") |
|
} |
|
// SMTP pass |
|
if os.Getenv("SMTP_PASS") != "" { |
|
SMTP_PASS = os.Getenv("SMTP_PASS") |
|
} |
|
// SMTP TLS |
|
if os.Getenv("SMTP_SECURE") != "" { |
|
SMTP_SECURE,_ = strconv.ParseBool(os.Getenv("SMTP_SECURE")) |
|
} |
|
|
|
fmt.Println("Gancio Installer") |
|
tmpl := template.Must(template.ParseFiles(TEMPLATE_PATH+"/templates/_base.html",TEMPLATE_PATH+"/templates/install.html")) |
|
|
|
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 |
|
// Server |
|
serverConfig := ServerConfig{ |
|
Host: "0.0.0.0", |
|
Port: 13120, |
|
} |
|
// Database |
|
dbConfig := DBConfig{ |
|
Dialec: "sqlite", |
|
Storage: DB_PATH, |
|
Logging: false, |
|
} |
|
// SMTP |
|
smtpConfig := SMTPConfig{ |
|
Auth: AuthSMTPConfig{ |
|
User: SMTP_USER, |
|
Pass: SMTP_PASS, |
|
}, |
|
Secure: SMTP_SECURE, |
|
Host: SMTP_HOST, |
|
Port: SMTP_PORT, |
|
} |
|
// Gancio |
|
gancioConfig := InstallerConfig{ |
|
Title: r.FormValue("title"), |
|
BaseUrl: r.FormValue("url"), |
|
Server: serverConfig, |
|
UploadPath: UPLOADS_PATH, |
|
LogPath: LOGS_PATH, |
|
DB: dbConfig, |
|
SMTP: smtpConfig, |
|
AdminEmail: msg.Email, |
|
LogLevel: LOG_LEVEL, |
|
} |
|
|
|
// 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(5 * time.Second) |
|
os.Exit(3) |
|
}() |
|
|
|
}) |
|
|
|
fmt.Println("Runing server "+LISTEN_PORT) |
|
http.ListenAndServe(LISTEN_PORT, nil) |
|
}
|
|
|