mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 15:09:59 +00:00
Added handler for filesystem and logic for rastering pages.
This commit is contained in:
83
webserver.go
83
webserver.go
@@ -2,11 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
Title string
|
||||
StaticStylesheet template.HTML
|
||||
Body []byte
|
||||
Error string
|
||||
}
|
||||
|
||||
const (
|
||||
coverCSS = `<link rel="stylesheet" href="web/static/css/cover.css">`
|
||||
dashboardCSS = `<link rel="stylesheet" href="web/static/css/dashboard.css">`
|
||||
)
|
||||
|
||||
func GetWebserverHost() string {
|
||||
host := SplitStrings(bot.config.Webserver.ListenAddress, ":")[0]
|
||||
if host == "" {
|
||||
@@ -22,6 +35,8 @@ func GetWebserverPort() int {
|
||||
}
|
||||
|
||||
func StartWebserver() error {
|
||||
fs := http.FileServer(http.Dir("web"))
|
||||
http.Handle("/web/", http.StripPrefix("/web/", fs))
|
||||
http.HandleFunc("/", index)
|
||||
var err error
|
||||
go func() {
|
||||
@@ -39,16 +54,62 @@ func ServerHTTPError(w http.ResponseWriter, err error) {
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
tmpl, err := template.ParseFiles("web/index.html", "web/header.html", "web/footer.html")
|
||||
if err != nil {
|
||||
ServerHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
tmplValues := map[string]interface{}{"title": "Home"}
|
||||
tmpl.Execute(w, tmplValues)
|
||||
if err != nil {
|
||||
ServerHTTPError(w, err)
|
||||
return
|
||||
switch r.URL.Path {
|
||||
case "/":
|
||||
renderTemplate(w, "index.html", readPage("/index"))
|
||||
case "/login":
|
||||
renderTemplate(w, "login.html", readPage(r.URL.Path))
|
||||
case "/logout":
|
||||
renderTemplate(w, "index.html", readPage("/index"))
|
||||
case "/dashboard-marketdepth":
|
||||
renderTemplate(w, "dashboard-marketdepth.html", readPage(r.URL.Path))
|
||||
case "/dashboard-ordermanagement":
|
||||
renderTemplate(w, "dashboard-ordermanagement.html", readPage(r.URL.Path))
|
||||
case "/dashboard-contact":
|
||||
renderTemplate(w, "dashboard-contact.html", readPage(r.URL.Path))
|
||||
case "/dashboard-settings":
|
||||
renderTemplate(w, "dashboard-settings.html", readPage(r.URL.Path))
|
||||
case "/dashboard-reports":
|
||||
renderTemplate(w, "dashboard-reports.html", readPage(r.URL.Path))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
renderTemplate(w, "error.html", readPage("/error"))
|
||||
}
|
||||
}
|
||||
|
||||
func readPage(client string) *Page {
|
||||
filename := "web/" + client[1:] + ".html"
|
||||
body, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Println("Webserver: Failed to open file -- ", err, "client string is: ", client)
|
||||
return nil
|
||||
}
|
||||
stylesheet := setStylesheet(client)
|
||||
return &Page{Title: client[1:], StaticStylesheet: stylesheet, Body: body}
|
||||
}
|
||||
|
||||
func setStylesheet(client string) template.HTML {
|
||||
if len(client) >= 10 {
|
||||
if client[:10] == "/dashboard" {
|
||||
return template.HTML(dashboardCSS)
|
||||
}
|
||||
}
|
||||
return template.HTML(coverCSS)
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, pageName string, p *Page) {
|
||||
tmpl, err := template.ParseFiles("web/index.html", "web/header.html",
|
||||
"web/footer.html", "web/dashboard-marketdepth.html", "web/login.html",
|
||||
"web/dashboard-ordermanagement.html", "web/dashboard-reports.html",
|
||||
"web/dashboard-settings.html", "web/dashboard-contact.html", "web/error.html")
|
||||
|
||||
if err != nil {
|
||||
log.Println("Webserver: Could not parsefile -- ", err)
|
||||
ServerHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
err = tmpl.ExecuteTemplate(w, pageName, p)
|
||||
if err != nil {
|
||||
log.Println("Webserver: Could not execute template -- ", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user