Add server subcommand

This commit is contained in:
raul 2024-05-22 10:42:00 +02:00
parent b06ba4edf1
commit 9a70dc0a91
2 changed files with 69 additions and 0 deletions

38
cmd/server.go Normal file
View File

@ -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")
}

31
cmd/serverFunc.go Normal file
View File

@ -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(),
})
}