From 9a70dc0a9153c71d20efc22ba1a0bd3c0500e408 Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 22 May 2024 10:42:00 +0200 Subject: [PATCH] Add server subcommand --- cmd/server.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/serverFunc.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 cmd/server.go create mode 100644 cmd/serverFunc.go diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..6eafbdb --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,38 @@ +/* +Copyright © 2024 raul +*/ + +package cmd + +import ( + "github.com/spf13/cobra" +) + +// serverCmd represents the server command +var serverCmd = &cobra.Command{ + Use: "server", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + server() + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // serverCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go new file mode 100644 index 0000000..634737d --- /dev/null +++ b/cmd/serverFunc.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "embed" + "fmt" + "net/http" + + //"github.com/gin-contrib/static" + "github.com/gin-gonic/gin" +) + +var ( + listenPort = "1302" +) + +//go:embed templates/* +var templatesFolder embed.FS + +func server() { + r := gin.Default() + //r.Use(static.Serve("/", static.EmbedFolder(templatesFolder, "templates"))) + r.GET("/", returnIndex) + fmt.Printf("Listening on port %v...\n", listenPort) + r.Run(":" + listenPort) +} + +func returnIndex(c *gin.Context) { + c.HTML(http.StatusOK, templatesFolder, gin.H{ + "UserAgent": c.Request.UserAgent(), + }) +}