Skip to content

Commit

Permalink
add storage caching optional
Browse files Browse the repository at this point in the history
- add flag for making storage of note on front-end optional
  • Loading branch information
Denis-Florin Rendler authored and rendler-denis committed Apr 11, 2021
1 parent 6752e28 commit 5f6c657
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
15 changes: 8 additions & 7 deletions configs/safenotes.example.yaml
Expand Up @@ -25,13 +25,14 @@ static-site:
index: index.html
resources: ./www
envjs:
web:
domain: "http://localhost/app"
storage-path: "http://localhost/api"
theme:
# logo:
# image: "/app/new-logo.png"
# imageCss: ['object-contain h-36 w-48 pt-2']
cache: true
web:
domain: "http://localhost/app"
storage-path: "http://localhost/api"
theme:
# logo:
# image: "/app/new-logo.png"
# imageCss: ['object-contain h-36 w-48 pt-2']

sketch:
templatesDir: resources/templates/email
Expand Down
3 changes: 2 additions & 1 deletion internal/config/definitions.go
Expand Up @@ -74,7 +74,8 @@ type StaticSite struct {
}

type EnvJs struct {
Web struct {
Cache bool `json:"cache"`
Web struct {
Domain string `json:"domain"`
StoragePath string `json:"storage_path" mapstructure:"storage-path"`
} `json:"web"`
Expand Down
24 changes: 13 additions & 11 deletions internal/config/parameters.go
Expand Up @@ -30,21 +30,21 @@ func NewConfigParams() *Parameters {
return &Parameters{
Verbose: false,
Server: ServerParams{
IP: defaultIp,
Port: defaultPort,
IP: defaultIp,
Port: defaultPort,
Https: Https{
Enable: true,
Port: defaultHttpsPort,
Cert: "./config/cert.pem",
CertKey: "./config/key.pem",
ServerName: "localhost",
Enable: true,
Port: defaultHttpsPort,
Cert: "./config/cert.pem",
CertKey: "./config/key.pem",
ServerName: "localhost",
EnableRedirect: true,
RedirectTo: "https://localhost",
RedirectTo: "https://localhost",
},
Auth: Auth{
Realm: "localhost - Restricted",
User: "",
Pass: "",
User: "",
Pass: "",
},
},
Api: ApiParams{
Expand All @@ -56,7 +56,9 @@ func NewConfigParams() *Parameters {
Serve: false,
Index: "index.html",
Resources: "./www",
EnvJs: EnvJs{},
EnvJs: EnvJs{
Cache: true,
},
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/react/.env.dev
Expand Up @@ -3,4 +3,4 @@ HTTPS=true

ENV=dev

REACT_APP_SAFENOTES_BASE_PATH=https://localhost:3000/app
REACT_APP_SAFENOTES_BASE_PATH=/app
7 changes: 5 additions & 2 deletions webapp/react/src/index.js
Expand Up @@ -20,17 +20,20 @@ import ReactDOM from 'react-dom';
import { config, ConfigContext } from './SafeNotes/context/Config';
import StorageEngine from './lib/StorageEngine';
import SafeNotes from './SafeNotes';
import NullCache from './lib/NullCache';
import Cache from './lib/Cache';

const cacheEngine = window.snenv.cache ? new Cache(): new NullCache();
let AppConfig = {
...config,
...window.snenv,
storage: new StorageEngine(window.snenv.web.storage_path),
storage: new StorageEngine(window.snenv.web.storage_path, cacheEngine),
};

ReactDOM.render(
<React.StrictMode>
<ConfigContext.Provider value={AppConfig}>
<SafeNotes/>
<SafeNotes />
</ConfigContext.Provider>
</React.StrictMode>,
document.getElementById('root'),
Expand Down
37 changes: 37 additions & 0 deletions webapp/react/src/lib/NullCache.js
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020. Denis Rendler <connect@rendler.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default class NullCache {
payload = '';

constructor () {
this.payload = '';
}

fetch (key, defaultReturn) {
if ('' === this.payload) {
return defaultReturn;
}

return this.payload;
}

store (key, payload) {
console.log(payload, this.payload);
this.payload = {'payload': payload};
}
}

5 changes: 2 additions & 3 deletions webapp/react/src/lib/StorageEngine.js
Expand Up @@ -15,20 +15,19 @@
*/

import Axios from 'axios';
import Cache from './Cache';

class StorageEngine {
static #ROUTE_NOTES = '/notes';

client = '';
cache = new Cache();

constructor (storagePath) {
constructor (storagePath, cacheEngine) {
this.client = Axios.create({
baseURL: storagePath,
timeout: 1000,
headers: { 'X-App': 'SafeNotes' },
});
this.cache = cacheEngine;
}

store (params) {
Expand Down

0 comments on commit 5f6c657

Please sign in to comment.