diff options
author | Julien Dessaux | 2021-04-05 20:58:28 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-04-05 20:58:28 +0200 |
commit | 309d17b921f75b16298cfe88ada9ba89bf646ac5 (patch) | |
tree | dd65823647fbb0769a851f34948af131b097fc60 /internal | |
parent | Fixed scope of test helpers in navitia_api_client package (diff) | |
download | trains-309d17b921f75b16298cfe88ada9ba89bf646ac5.tar.gz trains-309d17b921f75b16298cfe88ada9ba89bf646ac5.tar.bz2 trains-309d17b921f75b16298cfe88ada9ba89bf646ac5.zip |
Added basic webui to show results
Diffstat (limited to 'internal')
-rw-r--r-- | internal/webui/app.go | 74 | ||||
-rw-r--r-- | internal/webui/html/index.html | 24 | ||||
-rw-r--r-- | internal/webui/static/favicon.png | bin | 0 -> 1044 bytes | |||
-rw-r--r-- | internal/webui/static/main.css | 30 |
4 files changed, 128 insertions, 0 deletions
diff --git a/internal/webui/app.go b/internal/webui/app.go new file mode 100644 index 0000000..f510074 --- /dev/null +++ b/internal/webui/app.go @@ -0,0 +1,74 @@ +package webui + +import ( + "embed" + "html/template" + "log" + "net/http" + "time" + + "git.adyxax.org/adyxax/trains/pkg/config" + "git.adyxax.org/adyxax/trains/pkg/navitia_api_client" +) + +// the api client object +var client *navitia_api_client.Client + +//go:embed html/* +var templatesFS embed.FS + +//go:embed static/* +var staticFS embed.FS + +// The page template variable +type Page struct { + Departures []Departure + Title string +} +type Departure struct { + DisplayName string + Arrival string + Odd bool +} + +// The root handler of the webui +func rootHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + var p Page + if d, err := client.GetDepartures(); err != nil { + log.Printf("%+v\n%s\n", d, err) + } else { + for i := 0; i < len(d.Departures); i++ { + t, err := time.Parse("20060102T150405", d.Departures[i].StopDateTime.ArrivalDateTime) + if err != nil { + panic(err) + } + p.Departures = append(p.Departures, Departure{d.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05"), i%2 == 1}) + } + w.Header().Set("Cache-Control", "no-store, no-cache") + } + p.Title = "Horaires des prochains trains à Crépieux la Pape" + renderTemplate(w, "index", p) + } else { + http.Error(w, "404 Not Found", http.StatusNotFound) + } +} + +var templates = template.Must(template.ParseFS(templatesFS, "html/index.html")) + +func renderTemplate(w http.ResponseWriter, tmpl string, p Page) { + err := templates.ExecuteTemplate(w, tmpl+".html", p) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + +func Run(c *config.Config) { + client = navitia_api_client.NewClient(c.Token) + http.Handle("/static/", http.FileServer(http.FS(staticFS))) + http.HandleFunc("/", rootHandler) + + listenStr := c.Address + ":" + c.Port + log.Printf("Starting webui on %s", listenStr) + log.Fatal(http.ListenAndServe(listenStr, nil)) +} diff --git a/internal/webui/html/index.html b/internal/webui/html/index.html new file mode 100644 index 0000000..a0a7d82 --- /dev/null +++ b/internal/webui/html/index.html @@ -0,0 +1,24 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>{{ .Title }}</title> + <meta name="viewport" content="width=device-width, initial-scale=1"> + + <link rel="icon" type="image/png" href="/static/favicon.png" /> + <link rel="stylesheet" href="/static/main.css"> + </head> + <body> + <h3>Horaires des prochains trains à Crépieux la Pape</h3> + <table> + <thead> + <tr><th>Arrivée en gare</th><th>Direction</th></tr> + </thead> + <tbody> + {{ range .Departures }} + <tr{{ if .Odd }} style="color:#111111;"{{ end }}><td>{{ .Arrival }}</td><td>{{ .DisplayName }}</td></tr> + {{ end }} + </tbody> + </table> + </body> +</html> diff --git a/internal/webui/static/favicon.png b/internal/webui/static/favicon.png Binary files differnew file mode 100644 index 0000000..7668d76 --- /dev/null +++ b/internal/webui/static/favicon.png diff --git a/internal/webui/static/main.css b/internal/webui/static/main.css new file mode 100644 index 0000000..7cb5779 --- /dev/null +++ b/internal/webui/static/main.css @@ -0,0 +1,30 @@ +* { + box-sizing: border-box; +} +body { + font-family: open sans,-apple-system,BlinkMacSystemFont,segoe ui,Roboto,helvetica neue,Arial,sans-serif,apple color emoji,segoe ui emoji,segoe ui symbol; + font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; + display: grid; + grid-template-rows: auto 1fr auto; +} +@media only screen and (min-width: 60rem) { + body { + max-width:60rem; + margin-left: auto; + margin-right: auto; + } +} +table { + border-collapse: collapse; +} +th, td { + text-align: center; + border-bottom: 1px solid #ddd; +} +th { + background-color: darkgray; + color: white; +} +tr:nth-child(even) { + background-color: #f2f2f2; +} |