package cmd

import (
	"frostfire.io/dir2opdspse/app/routes"
	"github.com/gorilla/mux"
	"github.com/urfave/cli/v2"
	"net/http"
	"strconv"
	"time"
)

var Serve = cli.Command{
	Name:        "serve",
	Usage:       "Starts the webserver",
	Description: "Start the Flux webserver. That's all you need to run.",
	Action:      runServe,
	Flags: []cli.Flag{
		intFlag("port, p", 3000, "Web server port"),
	},
}

func runServe(c *cli.Context) error {
	r := mux.NewRouter()

	fs := http.FileServer(http.Dir("books/"))
	r.PathPrefix("/manga/").Handler(http.StripPrefix("/manga", fs))

	routes.Init(c, r)

	srv := &http.Server{
		Handler:      r,
		Addr:         ":" + strconv.Itoa(c.Int("port")),
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}

	return srv.ListenAndServe()
}
