<commit_before>package wsmaterials

import (
	"github.com/emicklei/go-restful"
	"github.com/materials-commons/materials"
	"net/http"
	"os"
	"time"
)

type adminResource struct {
	// empty for now
}

func newAdminResource(container *restful.Container) error {
	adminResource := adminResource{}
	adminResource.register(container)
	return nil
}

func (ar *adminResource) register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.Path("/admin")

	ws.Route(ws.GET("/restart").To(ar.restart).
		Doc("Restarts the materials service"))

	ws.Route(ws.GET("/update").To(ar.update).
		Doc("If updates are available downloads, installs and restarts the server."))

	ws.Route(ws.GET("/stop").To(ar.stop).
		Doc("Stops the server."))

	container.Add(ws)
}

func (ar *adminResource) restart(request *restful.Request, response *restful.Response) {
	response.WriteErrorString(http.StatusOK, "Restarting materials service\n")
	go func() {
		sleep(1)
		materials.Restart()
	}()
}

func (ar *adminResource) update(request *restful.Request, response *restful.Response) {

}

func (ar *adminResource) stop(request *restful.Request, response *restful.Response) {
	response.WriteErrorString(http.StatusOK, "Stopping materials service\n")
	go func() {
		sleep(1)
		os.Exit(0)
	}()
}

func sleep(seconds time.Duration) {
	time.Sleep(seconds * time.Millisecond)
}<commit_msg>Add updating, and config display.<commit_after>package wsmaterials

import (
	"fmt"
	"github.com/emicklei/go-restful"
	"github.com/materials-commons/materials"
	"github.com/materials-commons/materials/autoupdate"
	"net/http"
	"os"
	"time"
)

type adminResource struct {
	updater *autoupdate.Updater
}

func newAdminResource(container *restful.Container) error {
	adminResource := adminResource{
		updater: autoupdate.NewUpdater(),
	}
	adminResource.register(container)
	return nil
}

func (ar *adminResource) register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.Path("/admin").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON, restful.MIME_XML)

	ws.Route(ws.GET("/restart").To(ar.restart).
		Doc("Restarts the materials service"))

	ws.Route(ws.GET("/update").To(ar.update).
		Doc("If updates are available downloads, installs and restarts the server."))

	ws.Route(ws.GET("/stop").To(ar.stop).
		Doc("Stops the server."))

	ws.Route(ws.GET("/config").To(ar.config).
		Doc("Returns the configuration settings").
		Writes(materials.ConfigSettings{}))

	container.Add(ws)
}

func (ar *adminResource) restart(request *restful.Request, response *restful.Response) {
	response.WriteErrorString(http.StatusOK, "Restarting materials service\n")
	go func() {
		sleep(1)
		materials.Restart()
	}()
}

func (ar *adminResource) update(request *restful.Request, response *restful.Response) {
	websiteUpdate := "No"
	binaryUpdate := "No"

	if ar.updater.UpdatesAvailable() {
		if ar.updater.WebsiteUpdate() {
			websiteUpdate = "Yes"
		}

		if ar.updater.BinaryUpdate() {
			binaryUpdate = "Yes"
		}
	}

	msg := fmt.Sprintf("Website updated: %s/Binary updated: %s\n", websiteUpdate, binaryUpdate)
	response.WriteErrorString(http.StatusOK, msg)

	go func() {
		sleep(1)
		ar.updater.ApplyUpdates()
	}()

}

func (ar *adminResource) stop(request *restful.Request, response *restful.Response) {
	response.WriteErrorString(http.StatusOK, "Stopping materials service\n")
	go func() {
		sleep(1)
		os.Exit(0)
	}()
}

func (ar *adminResource) config(request *restful.Request, response *restful.Response) {
	response.WriteEntity(materials.Config)
}

func sleep(seconds time.Duration) {
	time.Sleep(seconds * time.Millisecond)
}
{'fraction_non_alphanumeric': 0.14966740576496673, 'fraction_numerical': 0.0019401330376940134, 'mean_word_length': 5.086003372681281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077433', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1241, 'n_words': 240}