134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
//go:embed templates/*
|
|
var tplFolder embed.FS
|
|
|
|
//go:embed images/*
|
|
var imgFolder embed.FS
|
|
|
|
var tmpl *template.Template
|
|
var listNew = []New{}
|
|
|
|
type New struct {
|
|
DX string
|
|
Status string
|
|
NewDXCC bool
|
|
NewMode bool
|
|
NewBand bool
|
|
Worked bool
|
|
MyCall bool
|
|
}
|
|
|
|
type HTTPServer struct {
|
|
router *mux.Router
|
|
Log4OMRepo Log4OMContactsRepository
|
|
Repo FlexDXClusterRepository
|
|
Log *log.Logger
|
|
TCPServer TCPServer
|
|
SpotChanToHTTPServer chan TelnetSpot
|
|
}
|
|
|
|
func NewHTTPServer(cRepo Log4OMContactsRepository, fRepo FlexDXClusterRepository, TCPServer *TCPServer, SpotChanToHTTPServer chan TelnetSpot, log *log.Logger) *HTTPServer {
|
|
|
|
gRouter := mux.NewRouter()
|
|
|
|
return &HTTPServer{
|
|
router: gRouter,
|
|
Log4OMRepo: cRepo,
|
|
Repo: fRepo,
|
|
Log: log,
|
|
TCPServer: *TCPServer,
|
|
SpotChanToHTTPServer: SpotChanToHTTPServer,
|
|
}
|
|
}
|
|
|
|
func (s *HTTPServer) SetRoutes() {
|
|
s.router.HandleFunc("/", s.Homepage)
|
|
s.router.HandleFunc("/spots", s.GetSpots).Methods("GET")
|
|
s.router.HandleFunc("/spotscount", s.GetSpotsCount).Methods("GET")
|
|
s.router.HandleFunc("/spotters", s.GetSpotters).Methods("GET")
|
|
s.router.HandleFunc("/new", s.GetNew).Methods("GET")
|
|
|
|
s.router.PathPrefix("/images/").Handler(http.StripPrefix("/images/", http.FileServer(http.Dir("./images/"))))
|
|
}
|
|
|
|
func (s *HTTPServer) StartHTTPServer() {
|
|
|
|
go func() {
|
|
for spot := range s.SpotChanToHTTPServer {
|
|
s.GetListofNew(spot)
|
|
}
|
|
}()
|
|
|
|
tmpl, _ = template.ParseGlob("templates/*.html")
|
|
|
|
s.SetRoutes()
|
|
s.Log.Infof("starting HTTP server on %s:%s", Cfg.HTTPServer.Host, Cfg.HTTPServer.Port)
|
|
err := http.ListenAndServe(Cfg.HTTPServer.Host+":"+Cfg.HTTPServer.Port, s.router)
|
|
if err != nil {
|
|
s.Log.Warn("cannot start HTTP server: ", err)
|
|
}
|
|
}
|
|
|
|
func (s *HTTPServer) Homepage(w http.ResponseWriter, r *http.Request) {
|
|
err := tmpl.ExecuteTemplate(w, "home.html", nil)
|
|
if err != nil {
|
|
s.Log.Error("error executing home template: ", err)
|
|
}
|
|
}
|
|
|
|
func (s *HTTPServer) GetSpots(w http.ResponseWriter, r *http.Request) {
|
|
spots := s.Repo.GetAllSpots("35")
|
|
tmpl.ExecuteTemplate(w, "spot", spots)
|
|
}
|
|
|
|
func (s *HTTPServer) GetSpotsCount(w http.ResponseWriter, r *http.Request) {
|
|
spots := s.Repo.GetAllSpots("0")
|
|
count := len(spots)
|
|
tmpl.ExecuteTemplate(w, "spotCount", count)
|
|
}
|
|
|
|
func (s *HTTPServer) GetSpotters(w http.ResponseWriter, r *http.Request) {
|
|
spotters := s.Repo.GetSpotters()
|
|
tmpl.ExecuteTemplate(w, "spotters", spotters)
|
|
}
|
|
|
|
func (s *HTTPServer) GetNew(w http.ResponseWriter, r *http.Request) {
|
|
tmpl.ExecuteTemplate(w, "new", listNew)
|
|
}
|
|
|
|
func (s *HTTPServer) GetListofNew(spot TelnetSpot) {
|
|
new := New{}
|
|
|
|
new.DX = spot.DX
|
|
|
|
if spot.NewDXCC {
|
|
new.Status = fmt.Sprintf("New DXCC (%s) (%s)", spot.Band, spot.Mode)
|
|
new.NewDXCC = true
|
|
} else if !spot.NewBand && spot.NewMode && spot.Mode != "" {
|
|
new.Status = fmt.Sprintf("New Mode (%s) (%s)", spot.Band, spot.Mode)
|
|
new.NewMode = true
|
|
} else if spot.NewBand && spot.NewMode && spot.Mode != "" {
|
|
new.Status = fmt.Sprintf("New Band (%s) & Mode (%s)", spot.Band, spot.Mode)
|
|
new.NewBand = true
|
|
new.NewMode = true
|
|
}
|
|
|
|
if new.Status != "" {
|
|
if len(listNew) > 10 {
|
|
listNew = append(listNew[:0], listNew[1:]...)
|
|
}
|
|
listNew = append(listNew, new)
|
|
}
|
|
}
|