main.cpp 2.6 KB
Newer Older
Noah Olsen's avatar
commit  
Noah Olsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*************************************************************************
 *
 * Homework 4:  Exploring continuous deployment.
 *
 * File Name: quote.cpp
 * Course:    CPTR 245
 *
 */

#include <cpp-httplib/httplib.h>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Quotes.h"

std::vector<std::string> explode(const std::string &s, const char &c) {
    std::string buff;
    std::vector<std::string> v;

    for (auto n : s) {
        if (n != c) {
            buff += n;
        } else if (n == c && !buff.empty()) {
            v.push_back(buff);
            buff = "";
        }
    }

    if (!buff.empty()) {
        v.push_back(buff);
    }

    return v;
}

int main(int argc, char **argv) {
    if (argc != 2) {
        std::cerr << "You must provide a port to run on" << std::endl;
        return -1;
    }
    int port = 0;

    try {
        port = std::stoi(argv[1]);
    } catch (const std::invalid_argument &e) {
        std::cerr << "Invalid port specified" << std::endl;
        return -1;
    }

    if (port < 1024 || port > 65535) {
        std::cerr << "Port is not in allowable range" << std::endl;
        return -1;
    }

    httplib::Server svr;

    svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
        res.set_content("Go to /quote/# or /random to get a quote.", "text/plain");
    });

    svr.Get("/random",
            [](const httplib::Request &req, httplib::Response &res) {
                Quotes myQuotes(time(NULL));
                Quote quote = myQuotes.getRandomQuote();

                std::string output = "Today's Random Quote was submitted by " +
                                quote.student + ".\n\n" + "\"" + quote.quote +
                                "\"\n  ~ " + quote.author + "\n";

                res.set_content(output, "text/plain");
            });

    svr.Get(R"(/quote/([0-9]+))",
            [](const httplib::Request &req, httplib::Response &res) {
                auto urlExploded = explode(req.matches[0], '/');
                auto numbers = std::vector<std::string>(urlExploded.begin() + 1,
                                                        urlExploded.end());

                Quotes myQuotes(time(NULL));
                Quote quote = myQuotes.getQuote(std::stoi(numbers[0]));
                std::string output = "Today's Quote was submitted by " +
                                quote.student + ".\n\n" + "\"" + quote.quote +
                                "\"\n  ~ " + quote.author + "\n";

                res.set_content(output, "text/plain");
            });

    svr.listen("0.0.0.0", port);

    return 0;
}