import express, { Express, Request, Response } from "express";
import { HitCounter } from "./hit-counter";

const app: Express = express();
const PORT: number = 8000;
const hitCounter = new HitCounter();
const imageMimeType = hitCounter.getMimeType();

app.get("/", (req: Request, res: Response) => {
    hitCounter.increment();
    res.send("Hello dear interlocutor! Your visit has been noted.");
  
});

app.get("/hits", async (req: Request, res: Response) => {
    const hits = await hitCounter.getHits();
    res.send(`This site has ${hits} hits!`);
});

app.get("/reset", async (req: Request, res: Response) => {
    await hitCounter.reset();
    res.send("Hit counter has been reset!");
});

app.get("/image", async (req: Request, res: Response) => {
    const image = await hitCounter.render();
    res.setHeader("Content-Type", imageMimeType);
    res.send(image);
});

app.listen(PORT, () => {
    console.log(`[server]: Express server started on port ${PORT}`);
});
