func (s *Server) Stop(ctx context.Context) error {
	if !atomic.CompareAndSwapUint32(&s.started, 1, 0) {
		return fmt.Errorf("cannot stop HTTP server since it hasn't been started")
	}
	return s.httpServer.Shutdown(ctx)
}
Stop stops the REST service.
package httpserver
import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"sync/atomic"
	"time"
	"github.com/gorilla/mux"
	"github.com/rs/cors"
	"github.com/trustbloc/edge-core/pkg/log"
	"github.com/trustbloc/sidetree-core-go/pkg/restapi/common"
)
var logger = log.New("httpserver")
const healthCheckEndpoint = "/healthcheck"
type Server struct {
	httpServer *http.Server
	started    uint32
	certFile   string
	keyFile    string
}
func New(url, certFile, keyFile string, handlers ...common.HTTPHandler) *Server {
	router := mux.NewRouter()
	for _, handler := range handlers {
		logger.Infof("Registering handler for [%s]", handler.Path())
		router.HandleFunc(handler.Path(), handler.Handler()).
			Methods(handler.Method()).
			Queries(params(handler)...)
	}
	router.HandleFunc(healthCheckEndpoint, healthCheckHandler).Methods(http.MethodGet)
	handler := cors.New(
		cors.Options{
			AllowedMethods: []string{
				http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions,
			},
			AllowedHeaders: []string{"*"},
		},
	).Handler(router)
	return &Server{
		httpServer: &http.Server{
			Addr:    url,
			Handler: handler,
		},
		certFile: certFile,
		keyFile:  keyFile,
	}
}
func (s *Server) Start() error {
	if !atomic.CompareAndSwapUint32(&s.started, 0, 1) {
		return fmt.Errorf("server already started")
	}
	go func() {
		logger.Infof("listening for requests on [%s]", s.httpServer.Addr)
		var err error
		if s.keyFile != "" && s.certFile != "" {
			err = s.httpServer.ListenAndServeTLS(s.certFile, s.keyFile)
		} else {
			err = s.httpServer.ListenAndServe()
		}
		if err != nil && !errors.Is(err, http.ErrServerClosed) {
			panic(fmt.Sprintf("Failed to start server on [%s]: %s", s.httpServer.Addr, err))
		}
		atomic.StoreUint32(&s.started, 0)
		logger.Infof("server has stopped")
	}()
	return nil
}