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.
 
 
gancio/installer/main.go

146 lines
3.4 KiB

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)
}