summary refs log tree commit diff
path: root/http.go
blob: ca3c4133dd369348c8b3dbb7229da9833d28f929 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"

	TLE "git.darknebu.la/Satellite/TLE"
	"github.com/gorilla/mux"
)

// indexHandler displays an index page
func indexHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "<h1>Hello World</h1>")
}

// dumpallHandler dumps all categories
func dumpallHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] Dumping all TLEs")

	// read the name of the categories from the categories.txt file and write
	// the result to the categories slice
	content, err := ioutil.ReadFile("categories.txt")
	if err != nil {
		log.Printf("%s", "could not read categories.txt file!")
	}
	lines := strings.Split(string(content), "\n")

	for _, line := range lines {
		categories = append(categories, line)
	}

	// download all the categories to the data/<category name> directory
	for _, category := range categories {
		downloadCategory(category)
	}

	log.Println("[+] Done dumping all TLEs")
}

// get Handler returns the specified TLE
func getHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	// read the TLEs from the categories file
	//content, err := ioutil.ReadFile("categories.txt")
	//if err != nil {
	//	log.Println("Could note read categories.txt")
	//}

	// handle the response according to the query parameter "format"
	// if ?format=json is given, return
	if r.URL.Query().Get("format") == "json" {
		w.Header().Set("Content-Type", "application/json")

		RawTLE := `ISS (ZARYA)             
1 25544U 98067A   19229.39083552  .00000228  00000-0  11917-4 0  9993
2 25544  51.6447  57.6210 0007373 294.0868 138.8050 15.50381554184754`

		JSONTLE, err := TLE.NewTLE(RawTLE)
		if err != nil {
			fmt.Println(err)
		}

		// convert the TLE to json
		b, err := json.MarshalIndent(JSONTLE, "", " ")
		if err != nil {
			fmt.Println("error: ", err)
		}
		fmt.Fprintf(w, string(b))
		//fmt.Fprintf(w, "{\"response\": \"Getting the TLE for %s/%s as json\"}", vars["station"], vars["name"])
	} else {
		fmt.Fprintf(w, "Getting the TLE for %s/%s raw", vars["station"], vars["name"])
	}

}