💾 Archived View for 80h.dev › projects › gemserv › files › src › config.rs.gemini captured on 2022-06-12 at 00:48:40. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-03-01)

-=-=-=-=-=-=-

01 extern crate serde_derive;

02 extern crate toml;

03 use std::collections::HashMap;

04 use std::path::Path;

05 use toml::de::Error;

06

07 #[derive(Debug, Deserialize, Clone)]

08 pub struct Config {

09 pub port: u16,

10 pub host: String,

11 pub log: Option<String>,

12 pub server: Vec<Server>,

13 }

14

15 #[derive(Debug, Deserialize, Clone)]

16 pub struct Server {

17 pub hostname: String,

18 pub dir: String,

19 pub key: String,

20 pub cert: String,

21 pub index: Option<String>,

22 pub lang: Option<String>,

23 #[cfg(feature = "cgi")]

24 pub cgi: Option<bool>,

25 #[cfg(feature = "cgi")]

26 pub cgipath: Option<String>,

27 #[cfg(any(feature = "cgi", feature = "scgi"))]

28 pub cgienv: Option<HashMap<String, String>>,

29 pub usrdir: Option<bool>,

30 #[cfg(feature = "proxy")]

31 pub proxy: Option<HashMap<String, String>>,

32 #[cfg(feature = "proxy")]

33 pub proxy_all: Option<String>,

34 pub redirect: Option<HashMap<String, String>>,

35 #[cfg(feature = "scgi")]

36 pub scgi: Option<HashMap<String, String>>,

37 }

38

39 #[derive(Debug, Clone)]

40 pub struct ServerCfg {

41 pub port: u16,

42 pub server: Server,

43 }

44

45 impl Config {

46 pub fn new(file: &Path) -> Result<Config, Error> {

47 let fd = std::fs::read_to_string(file).unwrap();

48 let config: Config = match toml::from_str(&fd) {

49 Ok(c) => c,

50 Err(e) => return Err(e),

51 };

52 return Ok(config);

53 }

54 pub fn to_map(&self) -> HashMap<String, ServerCfg> {

55 let mut map = HashMap::new();

56 for srv in &self.server {

57 map.insert(

58 srv.hostname.clone(),

59 ServerCfg {

60 port: self.port.clone(),

61 server: srv.clone(),

62 },

63 );

64 }

65 map

66 }

67 }