grim/devweb

Add the initial bits of a storage module
draft
2020-07-15, Gary Kramlich
70152f6b3743
Parents c470b1ba24a1
Children 85e354e5e64a
Add the initial bits of a storage module
--- a/.hgignore Wed Jul 15 02:38:25 2020 -0500
+++ b/.hgignore Wed Jul 15 23:22:27 2020 -0500
@@ -2,4 +2,5 @@
*.db
devweb
db/scheme/embedded.go
+data
--- a/server/cmd.go Wed Jul 15 02:38:25 2020 -0500
+++ b/server/cmd.go Wed Jul 15 23:22:27 2020 -0500
@@ -7,16 +7,23 @@
"syscall"
"keep.imfreedom.org/grim/devweb/db"
+ "keep.imfreedom.org/grim/devweb/storage"
)
type Cmd struct {
db.Options
- ListenAddr string `kong:"flag,name='listen-addr',help='The address to listen on.',default=':1234'"`
+ ListenAddr string `kong:"flag,name='listen-addr',help='The address to listen on.',default=':1234'"`
+ StoragePath string `kong:"flag,name='storage-path',help='The path to store files in.',default='data'"`
}
func (c *Cmd) Run() error {
- err := db.Setup(c.Options.Driver, c.Options.Options)
+ storage, err := storage.Create(c.StoragePath)
+ if err != nil {
+ return err
+ }
+
+ err = db.Setup(c.Options.Driver, c.Options.Options)
if err != nil {
return err
}
@@ -27,7 +34,7 @@
errChan := make(chan error, 10)
- server := NewServer(c.ListenAddr)
+ server := NewServer(c.ListenAddr, storage)
defer server.Close()
go func() {
--- a/server/server.go Wed Jul 15 02:38:25 2020 -0500
+++ b/server/server.go Wed Jul 15 23:22:27 2020 -0500
@@ -7,18 +7,21 @@
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
+ "keep.imfreedom.org/grim/devweb/storage"
"keep.imfreedom.org/grim/devweb/v1"
)
type Server struct {
listenAddr string
+ storage storage.Storage
server *http.Server
}
-func NewServer(listenAddr string) *Server {
+func NewServer(listenAddr string, storage storage.Storage) *Server {
return &Server{
listenAddr: listenAddr,
+ storage: storage,
server: &http.Server{
Addr: listenAddr,
ReadTimeout: 10 * time.Second,
@@ -28,10 +31,13 @@
}
func (s *Server) Listen() error {
- gin.SetMode(gin.ReleaseMode)
+ // gin.SetMode(gin.ReleaseMode)
router := gin.Default()
+ // add our storage routes
+ s.storage.Routes(&router.RouterGroup)
+
// create the api group
api := router.Group("/api")
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/storage/router.go Wed Jul 15 23:22:27 2020 -0500
@@ -0,0 +1,9 @@
+package storage
+
+import (
+ "github.com/gin-gonic/gin"
+)
+
+func (s Storage) Routes(router *gin.RouterGroup) {
+ router.Static("/raw", s.path)
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/storage/storage.go Wed Jul 15 23:22:27 2020 -0500
@@ -0,0 +1,38 @@
+package storage
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ log "github.com/sirupsen/logrus"
+)
+
+type Storage struct {
+ path string
+}
+
+func Create(path string) (Storage, error) {
+ log.Infof("setting up storage path %q\n", path)
+ info, err := os.Stat(path)
+ if os.IsNotExist(err) {
+ // path doesn't exist, so create it
+ if err := os.MkdirAll(path, 0755); err != nil {
+ return Storage{}, err
+ }
+ } else {
+ // The path exists so lets make sure it's a directory.
+ if !info.IsDir() {
+ err := fmt.Errorf("storage path %q exists and is not a directory", path)
+ return Storage{}, err
+ }
+ }
+
+ return Storage{path: path}, nil
+}
+
+func (s Storage) Remove(path string) error {
+ realPath := filepath.Join(s.path, path)
+
+ return os.RemoveAll(realPath)
+}