read cache path from config file

This commit is contained in:
Aravinth Manivannan 2021-11-02 15:47:55 +05:30
parent af3c43dbf5
commit 817c997d4a
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 16 additions and 2 deletions

View file

@ -1,5 +1,6 @@
debug = true debug = true
source_code = "https://github.com/realaravinth/libmedium" source_code = "https://github.com/realaravinth/libmedium"
cache = "/var/lib/libmedium"
[server] [server]
# The port at which you want authentication to listen to # The port at which you want authentication to listen to

View file

@ -14,11 +14,15 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::path::Path;
use actix_web::web; use actix_web::web;
use graphql_client::{reqwest::post_graphql, GraphQLQuery}; use graphql_client::{reqwest::post_graphql, GraphQLQuery};
use reqwest::Client; use reqwest::Client;
use sled::{Db, Tree}; use sled::{Db, Tree};
use crate::SETTINGS;
#[derive(Clone)] #[derive(Clone)]
pub struct Data { pub struct Data {
pub client: Client, pub client: Client,
@ -40,7 +44,8 @@ pub type AppData = web::Data<Data>;
impl Data { impl Data {
pub fn new() -> AppData { pub fn new() -> AppData {
let cache = sled::open("posts_cache").unwrap(); let path = Path::new(&SETTINGS.cache).join("posts_cache");
let cache = sled::open(path).unwrap();
let posts = cache.open_tree("posts").unwrap(); let posts = cache.open_tree("posts").unwrap();
AppData::new(Self { AppData::new(Self {
client: Client::new(), client: Client::new(),

View file

@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::env; use std::env;
use std::fs;
use std::path::Path; use std::path::Path;
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
@ -41,7 +42,7 @@ impl Server {
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Settings { pub struct Settings {
pub debug: bool, pub debug: bool,
// pub database: Database, pub cache: String,
pub server: Server, pub server: Server,
pub source_code: String, pub source_code: String,
} }
@ -83,6 +84,13 @@ impl Settings {
let settings: Settings = s.try_into()?; let settings: Settings = s.try_into()?;
let cache_path = Path::new(&settings.cache);
if !cache_path.exists() {
fs::create_dir(&cache_path).unwrap();
}
if !cache_path.is_dir() {
panic!("Cache path {} must be a directory", &settings.cache);
}
Ok(settings) Ok(settings)
} }
} }