Skip to content
Snippets Groups Projects
Select Git revision
  • 1b4021d0dacaacc8c8670ead0a535c84279cc96b
  • master default protected
  • v1.22.9
  • v1.22.8
  • v1.22.7
  • v1.22.6
  • v1.22.5
  • v1.22.4
  • v1.22.3
  • v1.22.1
  • v1.22.0
  • v1.21.0
  • v1.20.5
  • v1.20.4
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.0
  • v1.18.3
  • v1.18.2
  • v1.18.1
22 results

http-handler.go

Blame
  • Volker Schukai's avatar
    84732226
    History
    http-handler.go 2.77 KiB
    // Copyright 2022 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    package configuration
    
    import (
    	"bytes"
    	"context"
    	negotiation "gitlab.schukai.com/oss/libraries/go/network/http-negotiation"
    	"net/http"
    )
    
    // ContextKey is the key used to store the configuration in the request context
    // This is used by the middleware
    func (s *Settings[C]) Middleware(next http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		ctx := r.Context()
    		ctx = context.WithValue(ctx, contextKey, s.config)
    		r = r.WithContext(ctx)
    		next.ServeHTTP(w, r)
    	})
    }
    
    // serveGet handles GET requests
    func (s *Settings[C]) serveGet(w http.ResponseWriter, r *http.Request) {
    
    	n := negotiation.New(r.Header)
    	m := n.Type("application/json", "text/json", "application/yaml", "text/yaml", "application/toml", "text/toml", "application/properties", "text/properties", "text/x-java-properties", "text/x-properties")
    
    	if m == "" {
    		w.WriteHeader(http.StatusNotAcceptable)
    		return
    	}
    
    	buf := new(bytes.Buffer)
    
    	switch m {
    	case "application/json", "text/json":
    		w.Header().Set("Content-Type", "application/json")
    		s.Write(buf, Json)
    	case "application/yaml", "text/yaml":
    		w.Header().Set("Content-Type", "application/yaml")
    		s.Write(buf, Yaml)
    	case "application/toml", "text/toml":
    		w.Header().Set("Content-Type", "application/toml")
    		s.Write(buf, Toml)
    	case "application/properties", "text/properties", "text/x-java-properties", "text/x-properties":
    		w.Header().Set("Content-Type", "application/properties")
    		s.Write(buf, Properties)
    
    	}
    
    	if s.HasErrors() {
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    
    	w.Write(buf.Bytes())
    
    }
    
    func (s *Settings[C]) servePost(w http.ResponseWriter, r *http.Request) {
    
    	n := negotiation.New(r.Header)
    	m := n.ContentType("application/json", "text/json", "application/yaml", "text/yaml", "application/toml", "text/toml", "application/properties", "text/properties", "text/x-java-properties", "text/x-properties")
    
    	rs := reader{
    		reader: r.Body,
    	}
    
    	switch m {
    	case "application/json", "text/json":
    		rs.format = Json
    	case "application/yaml", "text/yaml":
    		rs.format = Yaml
    	case "application/toml", "text/toml":
    		rs.format = Toml
    	case "application/properties", "text/properties", "text/x-java-properties", "text/x-properties":
    		rs.format = Properties
    	default:
    		w.WriteHeader(http.StatusUnsupportedMediaType)
    		return
    	}
    
    	s.Lock()
    	defer s.Unlock()
    
    	b := s.config
    	s.importStream(rs)
    
    	x := s.config
    	s.config = b
    
    	s.setConfigInternal(x, false)
    
    }
    
    // ServeHTTP implements the http.Handler interface
    func (s *Settings[C]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    
    	switch r.Method {
    	case http.MethodGet:
    		s.serveGet(w, r)
    	case http.MethodPost:
    		s.servePost(w, r)
    	default:
    		w.WriteHeader(http.StatusMethodNotAllowed)
    	}
    
    }