Webdav endpoint

This commit is contained in:
Andrea Fazzi 2025-01-22 13:25:00 +01:00
commit d01d020994
8 changed files with 74 additions and 25 deletions

View file

@ -1,6 +1,5 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
@ -26,7 +25,7 @@ to quickly create a Cobra application.`,
}
func init() {
remote.AddCommand(pullCmd)
remoteCmd.AddCommand(pullCmd)
// Here you will define your flags and configuration settings.

View file

@ -1,40 +1,35 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"git.andreafazzi.eu/andrea/probo/cmd/util"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"github.com/studio-b12/gowebdav"
)
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
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) {
fmt.Println("push called")
},
Short: "Push local sessions on the webdav remote endpoint",
Long: util.RenderMarkdownTemplates("cli/*.tmpl", "cli/push/*.tmpl"),
Run: runPush,
}
func init() {
remote.AddCommand(pushCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pushCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pushCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
remoteCmd.AddCommand(pushCmd)
}
func runPush(cmd *cobra.Command, args []string) {
root := "http://localhost:3000/webdav"
user := "davuser"
password := "pass"
c := gowebdav.NewClient(root, user, password)
err := c.Connect()
if err != nil {
log.Fatal(err)
}
}

View file

@ -4,6 +4,7 @@ Copyright © 2025 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"errors"
"fmt"
"git.andreafazzi.eu/andrea/probo/cmd/util"
@ -15,6 +16,12 @@ var remoteCmd = &cobra.Command{
Use: "remote",
Short: "Manage the resources on the remote endpoint",
Long: util.RenderMarkdownTemplates("cli/*.tmpl", "cli/remote/*.tmpl"),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("At least an argument is required!")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("remote called")
},

View file

@ -88,6 +88,13 @@ func runServer(cmd *cobra.Command, args []string) {
mux.Handle("GET /public/", http.StripPrefix("/public", http.FileServer(http.Dir("public"))))
// mux.Handle("GET /webdav/", &webdav.Handler{
// Prefix: "/webdav",
// FileSystem: webdav.Dir(viper.GetString("sessions_path")),
// LockSystem: webdav.NewMemLS(),
// })
mux.Handle("GET /webdav/", serve.WebdavServer())
port := viper.Get("port")
log.Info("Listening...", "port", port)

33
cmd/serve/webdav.go Normal file
View file

@ -0,0 +1,33 @@
package serve
import (
"net/http"
"github.com/charmbracelet/log"
"github.com/spf13/viper"
"golang.org/x/net/webdav"
)
func WebdavServer() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, _ := r.BasicAuth()
log.Info("Connecting to webdav", "username", username, "password", password)
if username == "davuser" && password == "pass" {
handler := &webdav.Handler{
Prefix: "/webdav",
FileSystem: webdav.Dir(viper.GetString("sessions_path")),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Fatal("WebDAV", "method", r.Method, "url", r.URL, "err", err)
} else {
log.Info("WebDAV", "method", r.Method, "url", r.URL, "err")
}
},
}
handler.ServeHTTP(w, r)
}
})
}

View file

@ -0,0 +1,5 @@
{{define "description"}}
**Push exam sessions on the remote webdav endpoint**
The `push` command pushes sessions on the webdav endpoint.
{{end}}

3
go.mod
View file

@ -23,6 +23,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/yuin/goldmark v1.5.6
golang.org/x/net v0.23.0
gopkg.in/yaml.v2 v2.4.0
)
@ -70,13 +71,13 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/studio-b12/gowebdav v0.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark-emoji v1.0.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect

2
go.sum
View file

@ -168,6 +168,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/studio-b12/gowebdav v0.10.0 h1:Yewz8FFiadcGEu4hxS/AAJQlHelndqln1bns3hcJIYc=
github.com/studio-b12/gowebdav v0.10.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=