예제 #1
0
        public static async Task GenerateHealthcheckFile(string projectDirectory, string name)
        {
            var mainFilePath = Path.Combine(projectDirectory, $"{name.Kebaberize()}.go");

            if (File.Exists(mainFilePath))
            {
                return;
            }

            // Package line
            var mainFileLines = new List <string> {
                GeneratePackage("main")
            };

            // Import block
            var importBlock = GenerateImports(new List <string> {
                "github.com/gin-gonic/gin"
            });

            mainFileLines = mainFileLines.Concat(importBlock).ToList();

            // Arguments
            var arguments = new List <string> {
                "c *gin.Context"
            };

            // Contents
            var contents = new List <string>
            {
                "c.JSON(200, gin.H{",
                "\"status\": \"running\",",
                "})"
            };

            // Main function
            mainFileLines = mainFileLines.Concat(GenerateFunction("healthCheck", contents, arguments)).ToList();

            // Write main file
            await File.WriteAllLinesAsync(mainFilePath, mainFileLines);

            GoCommands.FormatFile(mainFilePath);
        }
예제 #2
0
        public static async Task GenerateMainFile(string projectDirectory, string host, string port)
        {
            var mainFilePath = Path.Combine(projectDirectory, "main.go");

            if (!File.Exists(mainFilePath))
            {
                // Package line
                var mainFileLines = new List <string> {
                    GeneratePackage("main")
                };

                // Import block
                var importBlock = GenerateImports(new List <string> {
                    "github.com/gin-gonic/gin", "os"
                });
                mainFileLines = mainFileLines.Concat(importBlock).ToList();

                // Contents
                var contents = new List <string>
                {
                    "argsWithoutProg := os.Args[1:]",
                    "r := gin.Default()",
                    "r.GET(\"/health-check\", healthCheck)",
                    "// Router marker",
                    "runLoc := argsWithoutProg[0]",
                    $"r.Run(runLoc)",
                };

                // Main function
                mainFileLines = mainFileLines.Concat(GenerateFunction("main", contents)).ToList();

                // Write main file
                await File.WriteAllLinesAsync(mainFilePath, mainFileLines);

                GoCommands.FormatFile(mainFilePath);
            }
        }