forked from jakecoffman/gorunner
/
main.go
58 lines (49 loc) · 1.21 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/jakecoffman/gorunner/handlers"
"github.com/jakecoffman/gorunner/hub"
"github.com/jakecoffman/gorunner/models"
"log"
"net"
"net/http"
"os"
)
const port = ":8090"
var r *mux.Router
// This filter enables messing with the request/response before and after the normal handler
func filter(w http.ResponseWriter, req *http.Request) {
r.ServeHTTP(w, req) // calls the normal handler
log.Printf("%s %s %s\n", req.RemoteAddr, req.Method, req.URL)
}
// TODO: Move to handlers package when more websocket handling is required.
func getRecentRuns() []byte {
runsList := models.GetRunListSorted()
recent := runsList.GetRecent(0, 10)
bytes, err := json.Marshal(recent)
if err != nil {
panic(err.Error())
}
return bytes
}
func main() {
wd, _ := os.Getwd()
println("Working directory", wd)
hub.NewHub(getRecentRuns)
go hub.Run()
// start the server and routes
server := &http.Server{Addr: port, Handler: nil}
r = mux.NewRouter()
handlers.Install(r)
models.InitDatabase()
http.HandleFunc("/", filter)
fmt.Println("Running on " + port)
l, e := net.Listen("tcp", port)
if e != nil {
panic(e)
}
defer l.Close()
server.Serve(l)
}