about summary refs log tree commit diff
path: root/http.go
blob: 48c8dcae77fc1309ef458c2fc367b877db3e922d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main

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

	tle "git.darknebu.la/Satellite/tle"
)

// indexHandler displays an index page
func indexHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "<h1>TLE2JSON api</h1>")
	fmt.Fprintf(w, "%s", "Usage: Make a get request to /{station}/{name}")
}

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

// get Handler returns the specified TLE
func getHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	// define where the file containing the spoecified categories lies
	categoryPath := fmt.Sprintf("data/%s.txt", r.FormValue("category"))

	// this will only download the category if the file does not exist or if it
	// is to old
	downloadCategory(r.FormValue("category"))

	// open the file containing the data for the individual station
	content, err := ioutil.ReadFile(categoryPath)
	if err != nil {
		log.Printf("Could not open %s", categoryPath)
	}

	lines := strings.Split(string(content), "\n")

	// iterate over all lines searching for the satellite
	var j int
	for i, line := range lines {
		if strings.Contains(line, r.FormValue("name")) {
			j = i
			break
		}
	}

	RawTLE := strings.Join(lines[j:j+3], "\n")

	w.Header().Set("Content-Type", "application/json")

	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))
}

func printAllHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	dumpall()
	w.Header().Set("Content-Type", "application/json")

	// 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\n", "could not read categories.txt file!")
	}
	lines := strings.Split(string(content), "\n")

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

	fmt.Fprintf(w, "[")

	// read the content of all files
	for j, category := range categories {

		// define where the file containing the spoecified categories lies
		categoryPath := fmt.Sprintf("data/%s.txt", category)

		// this will only download the category if the file does not exist or if it
		// is to old
		downloadCategory(category)

		// open the file containing the data for the individual station
		content, err := ioutil.ReadFile(categoryPath)
		if err != nil {
			log.Printf("Could not open %s", categoryPath)
		}

		lines := strings.Split(string(content), "\n")

		for i := 0; i < len(lines)-3; i += 3 {
			RawTLE := strings.Join(lines[i:i+3], "\n")

			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)
			}

			if i == len(lines)-4 && j == len(categories)-1 {
				fmt.Fprintf(w, "%s", string(b))
			} else {
				fmt.Fprintf(w, "%s,", string(b))
			}
		}
	}
	fmt.Fprintf(w, "]")
}