Add webserver files
This commit is contained in:
parent
10b771bd82
commit
87928b8bca
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue