Add webserver files

This commit is contained in:
raul 2024-12-06 10:38:37 +01:00
parent 10b771bd82
commit 87928b8bca
Signed by: raul
GPG Key ID: C1AA797073F17129
2 changed files with 54 additions and 0 deletions

21
cmd/server.go Normal file
View File

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

33
cmd/serverFunc.go Normal file
View File

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