2020-08-04 14:28:25 +08:00
package main
import (
"bufio"
2020-09-25 20:05:25 +08:00
"bytes"
2020-08-04 14:28:25 +08:00
"database/sql"
"fmt"
"github.com/joho/godotenv"
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/route"
"github.com/zhenorzz/goploy/task"
"github.com/zhenorzz/goploy/utils"
"github.com/zhenorzz/goploy/ws"
"log"
"net/http"
"os"
2020-09-25 20:05:25 +08:00
"os/exec"
2020-08-04 14:28:25 +08:00
"time"
_ "github.com/go-sql-driver/mysql"
)
func main ( ) {
2020-08-15 13:38:06 +08:00
println ( `
______ __
/ ____ / ___ ____ / / ___ __ __
/ / __ / __ \ / __ \ / / __ \ / / / /
/ / _ / / / _ / / / _ / / / / _ / / / _ / /
\ ____ / \ ____ / . ___ / _ / \ ____ / \ __ , /
/ _ / / ____ /
` )
2020-08-15 16:35:00 +08:00
install ( )
2020-08-21 10:31:36 +08:00
println ( time . Now ( ) . String ( ) )
2020-09-04 09:36:23 +08:00
godotenv . Load ( core . GetAppPath ( ) + ".env" )
2020-08-04 14:28:25 +08:00
core . CreateValidator ( )
model . Init ( )
ws . Init ( )
route . Init ( )
task . Init ( )
2020-08-15 13:38:06 +08:00
err := http . ListenAndServe ( ":" + os . Getenv ( "PORT" ) , nil )
2020-08-04 14:28:25 +08:00
if err != nil {
log . Fatal ( "ListenAndServe: " , err )
}
}
func install ( ) {
2020-08-15 13:38:06 +08:00
println ( "Check if it is installed for the first time" )
2020-08-04 14:28:25 +08:00
_ , err := os . Stat ( ".env" )
if err == nil || os . IsExist ( err ) {
2020-08-15 13:38:06 +08:00
println ( "The configuration file already exists, no need to reinstall (if you need to reinstall, please back up the database goploy first, delete the .env file)" )
2020-08-04 14:28:25 +08:00
return
}
2020-09-25 20:05:25 +08:00
var stdout bytes . Buffer
var stderr bytes . Buffer
cmd := exec . Command ( "rsync" , "--version" )
cmd . Stdout = & stdout
cmd . Stderr = & stderr
if err := cmd . Run ( ) ; err != nil {
println ( err . Error ( ) + ", detail: " + stderr . String ( ) )
panic ( "Please check if rsync is installed correctly, see https://rsync.samba.org/download.html" )
}
git := utils . GIT { }
if err := git . Run ( "--version" ) ; err != nil {
println ( err . Error ( ) + ", detail: " + git . Err . String ( ) )
panic ( "Please check if git is installed correctly, see https://git-scm.com/downloads" )
}
2020-08-04 14:28:25 +08:00
inputReader := bufio . NewReader ( os . Stdin )
2020-08-15 13:38:06 +08:00
println ( "Installation guidelines (Enter to confirm input)" )
println ( "Please enter the mysql user:" )
2020-08-04 14:28:25 +08:00
mysqlUser , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
mysqlUser = utils . ClearNewline ( mysqlUser )
2020-08-15 13:38:06 +08:00
println ( "Please enter the mysql password:" )
2020-08-04 14:28:25 +08:00
mysqlPassword , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
mysqlPassword = utils . ClearNewline ( mysqlPassword )
if len ( mysqlPassword ) != 0 {
mysqlPassword = ":" + mysqlPassword
}
2020-08-15 13:38:06 +08:00
println ( "Please enter the mysql host(default 127.0.0.1, without port):" )
2020-08-04 14:28:25 +08:00
mysqlHost , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
mysqlHost = utils . ClearNewline ( mysqlHost )
if len ( mysqlHost ) == 0 {
mysqlHost = "127.0.0.1"
}
2020-08-15 13:38:06 +08:00
println ( "Please enter the mysql port(default 3306):" )
2020-08-04 14:28:25 +08:00
mysqlPort , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
mysqlPort = utils . ClearNewline ( mysqlPort )
if len ( mysqlPort ) == 0 {
mysqlPort = "3306"
}
2020-08-15 13:38:06 +08:00
println ( "Please enter the absolute path of the log directory(default /tmp/):" )
2020-08-04 14:28:25 +08:00
logPath , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
logPath = utils . ClearNewline ( logPath )
if len ( logPath ) == 0 {
logPath = "/tmp/"
}
2020-08-15 13:38:06 +08:00
println ( "Please enter the absolute path of the ssh-key directory(default /root/.ssh/id_rsa):" )
2020-08-04 14:28:25 +08:00
sshFile , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
sshFile = utils . ClearNewline ( sshFile )
if len ( sshFile ) == 0 {
sshFile = "/root/.ssh/id_rsa"
}
2020-08-15 13:38:06 +08:00
println ( "Please enter the listening port(default 80):" )
2020-08-04 14:28:25 +08:00
port , err := inputReader . ReadString ( '\n' )
if err != nil {
panic ( "There were errors reading, exiting program." )
}
port = utils . ClearNewline ( port )
if len ( port ) == 0 {
port = "80"
}
2020-08-15 13:38:06 +08:00
println ( "Start to install the database..." )
2020-08-04 14:28:25 +08:00
db , err := sql . Open ( "mysql" , fmt . Sprintf (
"%s%s@tcp(%s:%s)/?charset=utf8mb4,utf8\n" ,
mysqlUser ,
mysqlPassword ,
mysqlHost ,
mysqlPort ) )
if err != nil {
panic ( err )
}
defer db . Close ( )
if err := model . ImportSQL ( db ) ; err != nil {
panic ( err )
}
2020-08-15 13:38:06 +08:00
println ( "Database installation is complete" )
2020-08-04 14:28:25 +08:00
envContent := ""
envContent += "DB_TYPE=mysql\n"
envContent += fmt . Sprintf (
"DB_CONN=%s%s@tcp(%s:%s)/goploy?charset=utf8mb4,utf8\n" ,
mysqlUser ,
mysqlPassword ,
mysqlHost ,
mysqlPort )
envContent += fmt . Sprintf ( "SIGN_KEY=%d\n" , time . Now ( ) . Unix ( ) )
envContent += fmt . Sprintf ( "LOG_PATH=%s\n" , logPath )
envContent += fmt . Sprintf ( "SSHKEY_PATH=%s\n" , sshFile )
envContent += "ENV=production\n"
envContent += fmt . Sprintf ( "PORT=%s\n" , port )
2020-08-15 13:38:06 +08:00
println ( "Start writing configuration file..." )
2020-08-04 14:28:25 +08:00
file , err := os . Create ( ".env" )
if err != nil {
panic ( err )
}
defer file . Close ( )
file . WriteString ( envContent )
2020-08-15 13:38:06 +08:00
println ( "Write configuration file completed" )
2020-08-04 14:28:25 +08:00
}