github.com/christoph-karpowicz/db_mediator@v0.0.0-20210207102849-61a28a1071d8/internal/server/application/stop_synch_handler.go (about)

     1  package application
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  )
     9  
    10  type stopSynchHandler struct {
    11  	app *Application
    12  }
    13  
    14  func (h *stopSynchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    15  	stop, ok := r.URL.Query()["stop"]
    16  	if !ok || len(stop[0]) < 1 {
    17  		log.Fatalln("[http request] ERROR: URL param 'stop' is missing.")
    18  	}
    19  
    20  	// allStr, ok := r.URL.Query()["all"]
    21  	// if !ok {
    22  	// 	allStr = []string{"false"}
    23  	// }
    24  	// all, err := strconv.ParseBool(allStr[0])
    25  	// if err != nil {
    26  	// 	log.Fatalln("[http request] ERROR: Wrong 'all' URL param value.")
    27  	// }
    28  
    29  	resChan := createResponseChannel()
    30  	go h.app.stopSynch(resChan, stop[0])
    31  
    32  	response := <-resChan
    33  	responseJSON, err := json.Marshal(response)
    34  	if err != nil {
    35  		panic("Error while marshalling response.")
    36  	}
    37  
    38  	fmt.Fprintf(w, "%s", responseJSON)
    39  }