diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..e5b9190 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,21 @@ +/* +Copyright © 2024 raul +*/ + +package cmd + +import ( + "github.com/spf13/cobra" +) + +var serverCmd = &cobra.Command{ + Use: "server", + Short: "Spawn the Drahoot webserver", + Run: func(cmd *cobra.Command, args []string) { + server() + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) +} diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go new file mode 100644 index 0000000..0d9e223 --- /dev/null +++ b/cmd/serverFunc.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/spf13/viper" +) + +var ( + ListenPort = "8080" +) + +func server() { + gin.SetMode(gin.ReleaseMode) + + p := viper.GetString("Server.Port") + if p != "" { + ListenPort = p + } + + r := gin.Default() + r.GET("/", helloWorld) + + r.Run(":" + ListenPort) +} + +func helloWorld(c *gin.Context) { + ua := c.Request.UserAgent() + message := fmt.Sprintf("Hello %v!\n", ua) + c.String(http.StatusOK, message) +}