func main() {
	flag.Parse()
	s, err := susigo.NewSusi(*susiaddr, *cert, *key)
	if err != nil {
		log.Printf("Error while creating susi connection: %v", err)
		return
	}
	susi = s
	log.Println("successfully create susi connection")
	sessionTimeouts = make(map[string]time.Time)

	if *user == "" && *pass == "" {
		http.HandleFunc("/publish", publishHandler)
		http.HandleFunc("/upload", uploadHandler)
		http.Handle("/ws", websocket.Handler(websocketHandler))
		http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(*assetDir))))
		http.HandleFunc("/", redirectToIndex)
	} else {
		http.HandleFunc("/publish", BasicAuth(publishHandler))
		http.HandleFunc("/upload", BasicAuth(uploadHandler))
		http.HandleFunc("/ws", BasicAuth(websocket.Handler(websocketHandler).ServeHTTP))
		http.HandleFunc("/assets/", BasicAuth(http.StripPrefix("/assets/", http.FileServer(http.Dir(*assetDir))).ServeHTTP))
		http.HandleFunc("/", BasicAuth(redirectToIndex))
	}

	log.Printf("starting http server on %v...", *webaddr)
	if *useHTTPS {
		log.Fatal(http.ListenAndServeTLS(*webaddr, *cert, *key, context.ClearHandler(http.DefaultServeMux)))
	} else {
		log.Fatal(http.ListenAndServe(*webaddr, context.ClearHandler(http.DefaultServeMux)))
	}
}
Esempio n. 2
0
func main() {
	flag.Parse()
	s, err := susigo.NewSusi(*susiaddr, *cert, *key)
	if err != nil {
		log.Printf("Error while creating susi connection: %v", err)
		return
	}
	susi = s
	log.Println("successfully create susi connection")

	file, err := os.Open(*mappingFile)
	if err != nil {
		log.Printf("Error while opening the mapping file: %v", err)
		return
	}
	dec := json.NewDecoder(file)
	var mapping map[string]map[string]string
	err = dec.Decode(&mapping)
	if err != nil {
		log.Printf("Error while decoding the mapping file: %v", err)
		return
	}

	r := mux.NewRouter()

	for endpoint, specifier := range mapping {
		var spec = specifier
		r.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
			topic := spec[r.Method]
			if _, ok := spec[r.Method]; !ok {
				w.WriteHeader(http.StatusNotFound)
				return
			}
			event := susigo.Event{
				Topic: topic,
			}
			payload := make(map[string]interface{})
			for key, value := range mux.Vars(r) {
				payload[key] = value
			}
			event.Payload = payload
			if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
				bodyPayload := make(map[string]interface{})
				payloadDecoder := json.NewDecoder(r.Body)
				err = payloadDecoder.Decode(&bodyPayload)
				if err != nil {
					w.WriteHeader(http.StatusNotAcceptable)
					fmt.Fprintf(w, "Error while parsing body: %v", err)
					return
				}
				for key, value := range bodyPayload {
					payload[key] = value
				}
				event.Payload = payload
			}
			result := make(chan interface{})
			err = susi.Publish(event, func(event *susigo.Event) {
				result <- event.Payload
			})
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				fmt.Fprintf(w, "Error while publishing event: %v", err)
				return
			}
			resultPayload := <-result
			w.Header().Set("Content-Type", "application/json")
			enc := json.NewEncoder(w)
			enc.Encode(resultPayload)
		})
	}

	http.Handle("/", r)

	log.Printf("starting REST server on %v...", *webaddr)
	if *useHTTPS {
		log.Fatal(http.ListenAndServeTLS(*webaddr, *cert, *key, context.ClearHandler(http.DefaultServeMux)))
	} else {
		log.Fatal(http.ListenAndServe(*webaddr, context.ClearHandler(http.DefaultServeMux)))
	}
}