htmxmqtt/cmd/server/main.go

50 lines
1.1 KiB
Go

package main
import (
"git.metznet.ca/MetzNet/htmxmqtt"
"errors"
"os/signal"
"syscall"
"net/http"
"os"
"fmt"
)
func main() {
handler_client, err := htmxmqtt.NewMQTTHandlerClient("tcp://localhost:1883", "", "", "htmx")
if err != nil {
panic(err)
}
handler_1, err := handler_client.NewHandler("test", htmxmqtt.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")
}
}