aboutsummaryrefslogtreecommitdiff
path: root/internal/webui/webui.go
blob: 63173c6e61f9c36bb99e8ffd18ed769e889956c1 (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
package webui

import (
	"log"
	"net/http"

	"git.adyxax.org/adyxax/trains/pkg/config"
	"git.adyxax.org/adyxax/trains/pkg/database"
	"git.adyxax.org/adyxax/trains/pkg/navitia_api_client"
)

func Run(c *config.Config, dbEnv *database.DBEnv) {
	e := env{
		conf:    c,
		dbEnv:   dbEnv,
		navitia: navitia_api_client.NewClient(c.Token),
	}
	http.Handle("/", handler{&e, rootHandler})
	http.Handle("/login", handler{&e, loginHandler})
	http.Handle("/static/", http.FileServer(http.FS(staticFS)))
	http.Handle("/stop", handler{&e, stopHandler})
	http.Handle("/stop/", handler{&e, specificStopHandler})

	if i, err := dbEnv.CountStops(); err == nil && i == 0 {
		log.Printf("No trains stops data found, updating...")
		if stops, err := e.navitia.GetStops(); err == nil {
			log.Printf("Updated trains stops data from navitia api, got %d results", len(stops))
			if err = dbEnv.ReplaceAndImportStops(stops); err != nil {
				if dberr, ok := err.(*database.QueryError); ok {
					log.Printf("%+v", dberr.Unwrap())
				}
			}
		} else {
			log.Printf("Failed to get trains stops data from navitia api : %+v", err)
		}
	}

	listenStr := c.Address + ":" + c.Port
	log.Printf("Starting webui on %s", listenStr)
	log.Fatal(http.ListenAndServe(listenStr, nil))
}