You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: server.go
    path of file: ./repos/caffeine/service
    
    the code of the file until where you have to start completion: package service

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/gorilla/mux"
	"github.com/itchyny/gojq"
	"github.com/xeipuuv/gojsonschema"

	"github.com/rehacktive/caffeine/database"
)

type Database interface {
	Init()
	Upsert(namespace string, key string, value []byte) *database.DbError
	Get(namespace string, key string) ([]byte, *database.DbError)
	GetAll(namespace string) (map[string][]byte, *database.DbError)
	Delete(namespace string, key string) *database.DbError
	DeleteAll(namespace string) *database.DbError
	GetNamespaces() []string
}

const (
	NamespacePattern = "/ns/{namespace:[a-zA-Z0-9]+}"
	KeyValuePattern  = "/ns/{namespace:[a-zA-Z0-9]+}/{key:[a-zA-Z0-9]+}"
	SearchPattern    = "/search/{namespace:[a-zA-Z0-9]+}"
	SchemaPattern    = "/schema/{namespace:[a-zA-Z0-9]+}"
	OpenAPIPattern   = "/{openapi|swagger}.json"
	BrokerPattern    = "/broker"
	SwaggerUIPattern = "/swaggerui/"
	SchemaId         = "_schema"

	EVENT_ITEM_ADDED        = "ITEM_ADDED"
	EVENT_ITEM_DELETED      = "ITEM_DELETED"
	EVENT_NAMESPACE_DELETED = "NAMESPACE_DELETED"

	certsPublicKey = "./certs/pu
    
func (s *Server) keyValueHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "*")
	if r.Method == http.MethodOptions {
		return
	}

	userId := r.Header.Get(USER_HEADER)

	vars := mux.Vars(r)
	namespace := vars["namespace"]
	key := vars["key"]

	switch r.Method {
	case http.MethodPost:
		defer r.Body.Close()
		r.Body = http.MaxBytesReader(w, r.Body, 1048576)
		data, err := ioutil.ReadAll(r.Body)
		if err != nil {
			respondWithError(w, http.StatusInternalServerError, err.Error())
			return
		}
		parsedData, err := s.validate(namespace, data)
		if err != nil {
			respondWithError(w, http.StatusBadRequest, err.Error())
			return
		}

		if s.AuthEnabled {
			// override data with a payload
			payload := Payload{
				User: userId,
				Data: parsedData,
			}
			data, err = payload.wrap()

			if err != nil {
				respondWithError(w, http.StatusInternalServerError, err.Error())
				return
			}
		}

		dbErr := s.db.Upsert(namespace, key, data)
		if dbErr != nil {
			switch dbErr.ErrorCode {
			case database.NAMESPACE_NOT_FOUND:
				respondWithError(w, http.StatusBadRequest, dbErr.Error())
			default:
				respondWithError(w, http.StatusInternalServerError, dbErr.Error())
			}
			return
		}
		s.Notify(BrokerEvent{
			Event:     EVENT_ITEM_ADDED,
			User:      userId,
			Namespace: namespace,
			Key:       key,
			Value:     parsedData,
		})
		respondWithJSON(w, http.StatusCreated, string(data))
	case http.MethodGet:
		data, dbErr := s.db.Get(namespace, key)
		if dbErr != nil {
			switch dbErr.ErrorCode {
			case database.ID_NOT_FOUND:
				respondWithError(w, http.StatusNotFound, dbErr.Error())
			case database.NAMESPACE_NOT_FOUND:
				respondWithError(w, http.StatusBadRequest, dbErr.Error())
			default:
				respondWithError(w, http.StatusInternalServerError, dbErr.Error())
			}
			return
		}
		respondWithJSON(w, http.StatusOK, string(data))
	case http.MethodDelete:
		err := s.db.Delete(namespace, key)
		if err != nil {

			switch err.ErrorCode {
			case database.ID_NOT_FOUND:
				respondWithError(w, http.StatusNotFound, err.Error())
			case database.NAMESPACE_NOT_FOUND:
				respondWithError(w, http.StatusBadRequest, err.Error())
			default:
				respondWithError(w, http.StatusInternalServerError, err.Error())
			}
			return
		}
		s.Notify(BrokerEvent{
			Event:     EVENT_ITEM_DELETED,
			User:      userId,
			Namespace: namespace,
			Key:       key,
			Value:     nil,
		})
		respondWithJSON(w, http.StatusAccepted, "{}")
	}
}