github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/main.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"os/exec"
     7  	"strconv"
     8  	"time"
     9  
    10  	log "github.com/Sirupsen/logrus"
    11  	"github.com/vedicsoft/vamps-core/commons"
    12  	"github.com/vedicsoft/vamps-core/routes"
    13  )
    14  
    15  var logHandler http.Handler
    16  
    17  func main() {
    18  	os.Chdir(commons.ServerConfigurations.Home)
    19  	serverLogFile, err := os.OpenFile(commons.ServerConfigurations.LogsDirectory+"/"+commons.SERVER_LOG_FILE_NAME,
    20  		os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    21  	if err != nil {
    22  		log.Fatalf("Error while opening server log file: %v", err)
    23  	}
    24  
    25  	// Log as JSON instead of the default ASCII formatter.
    26  	log.SetFormatter(&log.JSONFormatter{})
    27  	log.SetOutput(serverLogFile)
    28  
    29  	switch commons.ServerConfigurations.LogLevel {
    30  	case "debug":
    31  		log.SetLevel(log.DebugLevel)
    32  	case "info":
    33  		log.SetLevel(log.InfoLevel)
    34  	case "error":
    35  		log.SetLevel(log.ErrorLevel)
    36  	default:
    37  		log.SetLevel(log.InfoLevel)
    38  	}
    39  
    40  	defer serverLogFile.Close()
    41  
    42  	commons.ConstructConnectionPool(commons.ServerConfigurations.DBConfigMap)
    43  
    44  	// Starting caddy server to server static files
    45  	args := []string{"bin/caddy", "--conf=" + commons.ServerConfigurations.CaddyFile, "-pidfile=bin/caddy.pid"}
    46  
    47  	if err := exec.Command("nohup", args...).Start(); err != nil {
    48  		log.Fatalln("Error occourred while starting caddy server : ", err.Error())
    49  		os.Exit(1)
    50  	}
    51  
    52  	//Starting the API server
    53  	router := routes.NewRouter()
    54  
    55  	httpsServer := &http.Server{
    56  		Addr: ":" + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
    57  			commons.ServerConfigurations.PortOffset),
    58  		Handler:        router,
    59  		ReadTimeout:    time.Duration(commons.ServerConfigurations.ReadTimeOut) * time.Second,
    60  		WriteTimeout:   time.Duration(commons.ServerConfigurations.WriteTimeOut) * time.Second,
    61  		MaxHeaderBytes: 1 << 20,
    62  	}
    63  	log.Info("Starting server on port : " + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
    64  		commons.ServerConfigurations.PortOffset))
    65  	log.Fatal("HTTP Server error: ", httpsServer.ListenAndServeTLS(commons.ServerConfigurations.SSLCertificateFile,
    66  		commons.ServerConfigurations.SSLKeyFile))
    67  }