12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package api
- import (
- "log"
- "net/http"
- "strings"
- )
- // APIKeyHeader in this header thr right api key should be inserted
- const APIKeyHeader = "X-mcs-apikey"
- /*
- SysAPIKey defining a handler for checking system id and api key
- */
- type SysAPIKey struct {
- log *log.Logger
- Apikey string
- }
- /*
- NewSysAPIHandler creates a new SysApikeyHandler
- */
- func NewSysAPIHandler(apikey string) *SysAPIKey {
- c := &SysAPIKey{
- Apikey: apikey,
- }
- return c
- }
- /*
- Handler the handler checks systemid and apikey headers
- */
- func (s *SysAPIKey) Handler(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- path := strings.TrimSuffix(r.URL.Path, "/")
- if !strings.HasPrefix(path, "/health") {
- if s.Apikey != strings.ToLower(r.Header.Get(APIKeyHeader)) {
- Msg(w, http.StatusBadRequest, "either system id or apikey not correct")
- return
- }
- }
- next.ServeHTTP(w, r)
- })
- }
- /*
- AddHeader adding gefault header for system and apikey
- */
- func AddHeader(response http.ResponseWriter, apikey string, system string) {
- response.Header().Add(APIKeyHeader, apikey)
- }
|