forked from gophergala/blinky_light_display
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
123 lines (102 loc) · 2.84 KB
/
server.go
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
type displayPage struct {
RowCount int
Colsizes []int
Rows [][]string
}
var displayPagesString string = ""
var displayPages []displayPage = make([]displayPage, 0)
var currentPage displayPage = *new(displayPage)
func main() {
//Attempt to read configuration file
data, err := ioutil.ReadFile("conf.txt")
if err == nil {
configure(string(data), false)
}
//start managing the current url
go manageCurrentUrl()
//configuration REST endpoint
http.HandleFunc("/configure/", func(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
log.Println("Recieved post request.")
configure(req.FormValue("list"), true)
w.Write([]byte("Success! http://localhost:3030/config.html"))
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Quit being a nudge, Spew."))
}
})
//currentUrl REST endpoint
http.HandleFunc("/current/", serveCurrentUrl)
//allUrls REST endpoint
http.HandleFunc("/all/", serveAllUrls)
//static file server
clientfs := http.FileServer(http.Dir("client"))
http.Handle("/", clientfs)
//Listen for connections and serve
log.Println("Listening...")
err2 := http.ListenAndServe(":3030", nil)
if err2 != nil {
log.Println(err.Error())
}
}
func configure(list string, fromWeb bool) {
log.Println("configuring")
if fromWeb {
data := []byte(list)
ioutil.WriteFile("conf.txt", data, 0644)
}
displayPagesString = list
displayPages = parseResourceList(displayPagesString)
}
func parseResourceList(resourceList string) []displayPage {
log.Println("Parsing URLs.")
resourceList = strings.Replace(resourceList, "\r", "", -1)
rawPages := strings.Split(resourceList, "\n=\n")
pages := make([]displayPage, len(rawPages))
for i := range rawPages {
thisPage := new(displayPage)
rawRows := strings.Split(rawPages[i], "\n")
thisPage.RowCount = len(rawRows)
thisPage.Colsizes = make([]int, len(rawRows))
thisPage.Rows = make([][]string, len(rawRows))
for j := range rawRows {
rawCols := strings.Split(rawRows[j], " | ")
thisPage.Colsizes[j] = len(rawCols)
thisPage.Rows[j] = make([]string, len(rawCols))
for k := range rawCols {
thisPage.Rows[j][k] = rawCols[k]
}
}
pages[i] = *thisPage
}
return pages
}
func manageCurrentUrl() {
for {
if len(displayPages) > 0 {
thisPage := displayPages[time.Time.Minute(time.Now())%len(displayPages)]
currentPage = thisPage
log.Printf("Set current url to %s\n", thisPage)
time.Sleep(1 * time.Minute)
} else {
time.Sleep(1 * time.Second)
}
}
}
func serveCurrentUrl(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
urlJson, _ := json.Marshal(currentPage)
w.Write(urlJson)
}
func serveAllUrls(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(displayPagesString))
}