about summary refs log tree commit diff
path: root/http.go
blob: 91c75e3bd87a045857c0ae8676212cba104495c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"fmt"
	"log"
	"net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
	responseString := `
<html>
	<head>
		<title>Distributor Container</title>
	</head>
	<body>
		<h1>Distributor</h1>
		<p>
			<a href="/distributor">Distributor</a>
		</p>
	</body>
</html>`
	_, _ = fmt.Fprintf(w, responseString)
}

func distributorHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("The distributorHandler was accessed")

	// if the starIdBufferChannel is not filled yet, fill it
	if len(idBufferChannel) == 0 {
		log.Println("The idBufferChannel is empty, fetching new stars")
		fillStarIdBufferChannel()
	}

	// get a single id from the idBufferChannel
	log.Println("Getting an id from the idBufferChannel")
	id := <-idBufferChannel
	log.Println("Done...")

	// return the id using the http.ResponseWriter w
	_, _ = fmt.Fprintf(w, "%d", id)

	log.Printf("Done providing a starID (%d) from the StarBufferHandler", id)

}