2023-12-30 16:30:23 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.metznet.ca/MetzNet/htmxmqtt"
|
|
|
|
"errors"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-12-30 16:39:30 -07:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"fmt"
|
2023-12-30 16:30:23 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-12-30 16:39:30 -07:00
|
|
|
handler_client, err := htmxmqtt.NewMQTTHandlerClient("tcp://localhost:1883", "", "", "htmx")
|
2023-12-30 16:30:23 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-30 16:39:30 -07:00
|
|
|
handler_1, err := handler_client.NewHandler("test", htmxmqtt.PayloadFormatFunc(`<p id="test">%s</p>`))
|
2023-12-30 16:30:23 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", http.FileServer(http.Dir("./site")))
|
|
|
|
mux.Handle("/ws", handler_1)
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
Addr: ":8080",
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGTERM, syscall.SIGKILL)
|
|
|
|
go func(sigs chan os.Signal, server *http.Server) {
|
|
|
|
<- sigs
|
|
|
|
server.Close()
|
|
|
|
}(sigs, server)
|
|
|
|
|
|
|
|
err = server.ListenAndServe()
|
|
|
|
if errors.Is(err, http.ErrServerClosed) == true {
|
|
|
|
os.Stderr.WriteString("Server closed on signal\n")
|
|
|
|
} else if err != nil {
|
|
|
|
os.Stderr.WriteString(fmt.Sprintf("Server error: %s\n", err))
|
|
|
|
} else {
|
|
|
|
os.Stderr.WriteString("Server closed\n")
|
|
|
|
}
|
|
|
|
}
|