Sparqlpuppy wants to be a SOLID server when it grows up
// sparqlpuppy - an LDP/SOLID Server
// Copyright (C) 2020 Azure <azure@fox.blue>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// 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/>.

#![forbid(unsafe_code)]

mod files;
mod setup;
mod store;
mod urls;

use crate::setup::Config;
use async_std::{
    net::{TcpListener, TcpStream},
    prelude::*,
    task,
};
use fehler::throws;
use http_types::{Error, Request, Response, StatusCode};
use once_cell::sync::OnceCell;
use oxigraph::store::rocksdb::RocksDbStore;

static SETTINGS: OnceCell<Config> = OnceCell::new();
static STORE: OnceCell<RocksDbStore> = OnceCell::new();

#[throws]
#[async_std::main]
async fn main() {
    setup::setup().await?;
    let settings = SETTINGS.get().unwrap();
    let listener = TcpListener::bind(&settings.addr).await?;

    // For each incoming TCP connection, spawn a task and call `accept`.
    let mut incoming = listener.incoming();
    while let Some(stream) = incoming.next().await {
        let stream = stream?;
        task::spawn(async {
            if let Err(err) = accept(stream).await {
                eprintln!("{}", err);
            }
        });
    }
}

#[throws]
async fn accept(stream: TcpStream) {
    async_h1::accept(stream.clone(), |req| async { req_handler(req).await }).await?;
}

pub static BASIC_CONTAINER: &str = "<http://www.w3.org/ns/ldp#BasicContainer>; rel=\"type\"";

// static RESOURCE: &str =
//     "<http://www.w3.org/ns/ldp#Resource>; rel=\"type\"";

// static TURTLE_TYPE: &str = "text/turtle; charset=UTF-8";

// static ALLOWABLES: &str = "OPTIONS,HEAD,GET,POST,PUT,PATCH";

// static PATCHABLES: &str = "text/ldpatch";

// static POSTABLES: &str = "text/turtle, application/ld+json, image/bmp, image/jpeg";

#[throws]
async fn req_handler(_req: Request) -> Response {
    // let reqpath = req.url().path().trim_start_matches('/');
    return Response::new(StatusCode::Ok);
}