package minecraft_log

import (
	"net/http"

	"go.uber.org/zap"
	"git.ssns.se/git/frozendragon/minecraft-ui/internal/rest"
	"io/ioutil"
)

type Resource struct {
	Logger *zap.Logger
}

type Response struct {
	Log string `json:"log"`
}

func (r *Resource) Get(w http.ResponseWriter, req *http.Request) {
	data, err := ioutil.ReadFile("/srv/minecraft/logs/latest.log")

	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(err.Error()))
		return
	}

	response := &Response{
		Log: string(data),
	}
	rest.JSON(w, req, response)
}
