server is getting a web interface

main
Johannes Bülow 2023-10-26 21:32:21 +02:00
parent 88a98c977c
commit 17e39b20f7
Signed by untrusted user who does not match committer: jmb
GPG Key ID: B56971CF7B8F83A6
7 changed files with 106 additions and 3 deletions

View File

@ -4,12 +4,35 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"git.jmbit.de/jmb/patchman/client/utils"
"git.jmbit.de/jmb/patchman/common"
"github.com/google/uuid"
"github.com/spf13/viper"
"io"
"log"
"net/http"
"os"
"github.com/spf13/cobra"
)
var token string
var output string
var serverUrl string
type Registration struct {
hostname string
token string
secret string
}
type ApiResponse struct {
uuid uuid.UUID
}
// registerCmd represents the register command
var registerCmd = &cobra.Command{
Use: "register",
@ -18,10 +41,39 @@ var registerCmd = &cobra.Command{
patchman register --output /path/to/config --server https://patchman.example.com:443 --token <InsertRegistrationTokenHere>
`,
Run: func(cmd *cobra.Command, args []string) {
secret, err := common.RandomString(64)
if err != nil {
fmt.Fprintln(os.Stderr, "could not generate secret, please provide it manually!")
secret = "CHANGEME"
}
fmt.Println("registering with Patchman Server")
viper.SetDefault("server", cmd.Flag("server"))
viper.SetDefault("server", serverUrl)
viper.SetDefault("secret", secret)
viper.SetConfigName("client.yaml")
viper.SetConfigType("yaml")
registration := Registration{
hostname: utils.GetHostname(),
token: token,
secret: secret,
}
jsondata, err := json.Marshal(registration)
utils.HandleErrorFatal(err, "Could not parse registration Data")
request, err := http.NewRequest("POST", fmt.Sprintf("%s/v0/endpoint/register", serverUrl), bytes.NewBuffer(jsondata))
utils.HandleErrorFatal(err, "Could not generate registration request")
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
utils.HandleErrorFatal(err, "Error making registration request")
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
utils.HandleError(err, "Could not close reader")
}
}(response.Body)
if response.StatusCode != http.StatusCreated {
log.Printf("Request failed with response %s", response.Status)
}
},
}
@ -38,7 +90,7 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// registerCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
registerCmd.Flags().String("output", "/etc/patchman/client.yaml", "Config file to create")
registerCmd.Flags().StringVar(&output, "output", "/etc/patchman/client.yaml", "Config file to create")
registerCmd.Flags().String("server", "", "URL to Patchman server")
registerCmd.Flags().String("token", "", "Registration token from Patchman Server")
}

View File

@ -0,0 +1,15 @@
package utils
import "log"
func HandleError(err error, msg string) {
if err != nil {
log.Printf("%s: %v", msg, err)
}
}
func HandleErrorFatal(err error, msg string) {
if err != nil {
log.Fatalf("%s: %v", msg, err)
}
}

11
client/utils/hostname.go Normal file
View File

@ -0,0 +1,11 @@
package utils
import "os"
func GetHostname() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
return hostname
}

View File

@ -49,6 +49,6 @@ func Register(c *gin.Context) {
result = database.DB.First(&usedToken)
utils.HandleError(result.Error, http.StatusBadRequest, "Could not create Endpoint DB entry", c)
c.JSON(http.StatusOK, gin.H{"uuid": newEndpoint.ID})
c.JSON(http.StatusCreated, gin.H{"uuid": newEndpoint.ID})
}

13
server/web/router.go Normal file
View File

@ -0,0 +1,13 @@
package web
import (
"git.jmbit.de/jmb/patchman/server/web/static"
"github.com/gin-gonic/gin"
"net/http"
)
func GroupWeb(router *gin.Engine) *gin.Engine {
router.StaticFS("/static", http.FS(static.StaticFS))
return router
}

View File

@ -0,0 +1,6 @@
package static
import "embed"
//go:embed *
var StaticFS embed.FS

View File

@ -0,0 +1,6 @@
package templates
import "embed"
//go:embed *
var TemplatesFS embed.FS