1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-10 00:20:02 +00:00
mondash/storage/file.go
Knut Ahlers f3c31476b4
Major: Rework frontend, API and improve code quality (#5)
* Update dependencies
* Improve code quality
* Apply linter advices
* Add gzip compression to all requests
* Rework frontend
* Apply bootswatch theme
* Hide historic details when not requested
* Remove debugging header
* Apply auto-migration of meta fields
* Fix broken "last update" time
* Pre-sort metrics for frontend / API
* Add tooltip with absolute time
* Some design fixes
* Add tooltip with absolute date to last ok
* Implement filters
* Apply eslint --fix
* Remove unused var
* Remove remains of old template engine
* Update baked in assets
* Update Dockerfile for new version

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-05-25 00:03:06 +02:00

73 lines
1.7 KiB
Go

package storage
import (
"io/ioutil"
"net/url"
"os"
"path"
log "github.com/sirupsen/logrus"
)
// FileStorage is a storage adapter storing the data into single local files
type FileStorage struct {
storagePath string
}
// NewFileStorage instanciates a new FileStorage
func NewFileStorage(uri *url.URL) *FileStorage {
// Create directory if not exists
if _, err := os.Stat(uri.Path); os.IsNotExist(err) {
if err := os.MkdirAll(uri.Path, 0700); err != nil {
log.WithError(err).Fatal("Could not create storage directory")
}
}
return &FileStorage{
storagePath: uri.Path,
}
}
// Put writes the given data to FS
func (f *FileStorage) Put(dashboardID string, data []byte) error {
err := ioutil.WriteFile(f.getFilePath(dashboardID), data, 0600)
return err
}
// Get loads the data for the given dashboard from FS
func (f *FileStorage) Get(dashboardID string) ([]byte, error) {
data, err := ioutil.ReadFile(f.getFilePath(dashboardID))
if err != nil {
return nil, DashboardNotFoundError{dashboardID}
}
return data, nil
}
// Delete deletes the given dashboard from FS
func (f *FileStorage) Delete(dashboardID string) error {
if exists, err := f.Exists(dashboardID); err != nil || !exists {
if err != nil {
return err
}
return DashboardNotFoundError{dashboardID}
}
return os.Remove(f.getFilePath(dashboardID))
}
// Exists checks for the existence of the given dashboard
func (f *FileStorage) Exists(dashboardID string) (bool, error) {
if _, err := os.Stat(f.getFilePath(dashboardID)); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func (f *FileStorage) getFilePath(dashboardID string) string {
return path.Join(f.storagePath, dashboardID+".txt")
}