generated from pluveto/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
283 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package app | ||
|
||
type Args struct { | ||
Names []string `arg:"positional,required" help:"names to flydav"` | ||
Seperately bool `arg:"-s,--seperately" help:"flydav each name seperately" default:"false"` | ||
Verbose bool `arg:"-v,--verbose" help:"verbose output" default:"false"` | ||
Config string `arg:"-c,--config" help:"config file" default:"config.toml"` | ||
Host string `arg:"-H,--host" help:"host address"` | ||
Port int `arg:"-p,--port" help:"port"` | ||
Username string `arg:"-u,--user" help:"username"` | ||
Verbose bool `arg:"-v,--verbose" help:"verbose output"` | ||
Config string `arg:"-c,--config" help:"config file"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pluveto/flydav/cmd/flydav/conf" | ||
"github.com/pluveto/flydav/cmd/flydav/service" | ||
) | ||
|
||
func Run(conf conf.Conf) { | ||
|
||
fmt.Println("Serving on: ", fmt.Sprintf("%s:%d", conf.Server.Host, conf.Server.Port)) | ||
fmt.Println("Username: ", conf.Auth.User[0].Username) | ||
// fmt.Println("Password(Encrypted): ", conf.Auth.User[0].PasswordHash) | ||
|
||
server := &WebdavServer{ | ||
AuthService: &service.BasicAuthService{Users: conf.Auth.User}, | ||
Host: conf.Server.Host, | ||
Port: conf.Server.Port, | ||
} | ||
server.Listen() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"path" | ||
"strings" | ||
|
||
"github.com/pluveto/flydav/pkg/logger" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/net/webdav" | ||
) | ||
|
||
type AuthService interface { | ||
Authenticate(username, password string) error | ||
} | ||
|
||
type WebdavServer struct { | ||
AuthService AuthService | ||
Host string | ||
Port int | ||
MappedDir string | ||
} | ||
|
||
func (s *WebdavServer) Listen() { | ||
if nil == s.AuthService { | ||
logger.Fatal("AuthService is nil") | ||
} | ||
if s.MappedDir == "" { | ||
logger.Fatal("MappedDir is empty") | ||
} | ||
if !path.IsAbs(s.MappedDir) { | ||
logger.Fatal("MappedDir is not an absolute path") | ||
} | ||
if !strings.HasPrefix(s.MappedDir, "/home/") { | ||
logger.Warn("You're using a path which isn't under /home/ as mapped directory. This may cause security issues.") | ||
} | ||
|
||
davfs := &webdav.Handler{ | ||
FileSystem: webdav.Dir(s.MappedDir), | ||
LockSystem: webdav.NewMemLS(), | ||
Logger: func(r *http.Request, err error) { | ||
ent := logger.WithFields(logrus.Fields{ | ||
"method": r.Method, | ||
"path": r.URL.Path, | ||
}) | ||
if err != nil { | ||
ent.Error(err) | ||
} else { | ||
ent.Info() | ||
} | ||
}, | ||
} | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
username, password, ok := r.BasicAuth() | ||
if !ok { | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) | ||
http.Error(w, "Unauthorized.", http.StatusUnauthorized) | ||
return | ||
} | ||
err := s.AuthService.Authenticate(username, password) | ||
if err != nil { | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) | ||
http.Error(w, "Unauthorized.", http.StatusUnauthorized) | ||
logger.Error("Unauthorized: ", err) | ||
return | ||
} | ||
davfs.ServeHTTP(w, r) | ||
}) | ||
addr := fmt.Sprintf("%s:%d", s.Host, s.Port) | ||
err := http.ListenAndServe(addr, nil) | ||
logger.Fatal("failed to listen and serve on", addr, ":", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.