47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"git.metznet.ca/MetzNet/htmxmqtt"
|
||
|
"errors"
|
||
|
"os/signal"
|
||
|
"syscall"
|
||
|
)
|
||
|
|
||
|
|
||
|
func main() {
|
||
|
handler_client, err := NewMQTTHandlerClient("tcp://localhost:1883", "", "", "htmx")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
handler_1, err := handler_client.NewHandler("test", PayloadFormatFunc(`<p id="test">%s</p>`))
|
||
|
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")
|
||
|
}
|
||
|
}
|