From 6115a99c0c21b04c6443fbb41fadb3fc83004ff3 Mon Sep 17 00:00:00 2001
From: Sandeep Shewalkar <sandeepss@mkcl.org>
Date: Mon, 6 Aug 2018 18:28:41 +0530
Subject: [PATCH] Get Gin server with Static middleware configuration

---
 constantmdl/constantmdl.go     |  4 +++
 httpservermdl/httpservermdl.go | 57 ++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/constantmdl/constantmdl.go b/constantmdl/constantmdl.go
index 828697b..2587f02 100755
--- a/constantmdl/constantmdl.go
+++ b/constantmdl/constantmdl.go
@@ -48,3 +48,7 @@ const (
 
 // HTTP400ERROR is used to check 400 status
 const HTTP400ERROR = 400
+
+//Configuration for GetServerWithStaticConfig
+const STATIC_DIRECTORY_NAME = "dsf"
+const STATIC_URL = "/dsf"
diff --git a/httpservermdl/httpservermdl.go b/httpservermdl/httpservermdl.go
index 4f82476..7bcba19 100644
--- a/httpservermdl/httpservermdl.go
+++ b/httpservermdl/httpservermdl.go
@@ -1,7 +1,13 @@
 package httpservermdl
 
 import (
+	"path/filepath"
+
+	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/constantmdl"
+	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/filemdl"
+	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
 	"github.com/gin-gonic/gin"
+	"github.com/kardianos/osext"
 )
 
 // GetServer will return the webserver pointer
@@ -10,3 +16,54 @@ func GetServer() *gin.Engine {
 	server := gin.New()
 	return server
 }
+
+// GetServerWithStaticConfig will return the webserver pointer.
+// It will also register static middleware
+func GetServerWithStaticConfig(staticDirectoryRelativePath string) *gin.Engine {
+	// TODO: use sync.Once
+	server := gin.New()
+	staticPath := checkAndCreateStaticDirectory(staticDirectoryRelativePath)
+
+	if staticPath != "" {
+		staticGroup := server.Group(constantmdl.STATIC_URL)
+		// 			//staticGroup.Use(jwt.Auth("models.JWTKey"))
+		staticGroup.Static("/", staticPath)
+	}
+
+	return server
+}
+
+// checkAndCreateStaticDirectory will create static directory to
+// serve the static contents. Directory will be created at the same location where executable is located
+func checkAndCreateStaticDirectory(staticDirectoryRelativePath string) string {
+
+	staticDirectoryPath := ""
+	if staticDirectoryRelativePath == "" {
+		staticDirectoryRelativePath = constantmdl.STATIC_DIRECTORY_NAME
+	}
+	executablePath, exePathError := osext.Executable()
+	if exePathError != nil {
+		loggermdl.LogError("error while fetching executable path : ", exePathError)
+		return ""
+	}
+
+	dirPath := filepath.Dir(executablePath)
+	staticDirectoryPath = dirPath + "/" + staticDirectoryRelativePath
+
+	exists := filemdl.FileAvailabilityCheck(staticDirectoryPath)
+
+	if exists {
+		loggermdl.LogInfo("static directory already exists: ", staticDirectoryPath)
+		return staticDirectoryPath
+	}
+	loggermdl.LogInfo("static directory does not exists. creating the new : ", staticDirectoryPath)
+	directoryCreationError := filemdl.CreateDirectory(staticDirectoryPath)
+
+	if directoryCreationError != nil {
+		loggermdl.LogError("error while creating static directory : ", directoryCreationError)
+		return ""
+	}
+	return staticDirectoryPath
+
+	return ""
+}
-- 
GitLab