mirror of
https://github.com/dpup/meshstream.git
synced 2026-08-07 17:22:54 +02:00
Add web server using prefab framework
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/dpup/prefab"
|
||||
)
|
||||
|
||||
// Config holds server configuration
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
// Server encapsulates the HTTP server functionality
|
||||
type Server struct {
|
||||
config Config
|
||||
server *prefab.Server
|
||||
}
|
||||
|
||||
// New creates a new server instance
|
||||
func New(config Config) *Server {
|
||||
return &Server{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// Start initializes and starts the web server
|
||||
func (s *Server) Start() error {
|
||||
// Get port as integer
|
||||
port, err := strconv.Atoi(s.config.Port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a new prefab server
|
||||
s.server = prefab.New(
|
||||
prefab.WithHost(s.config.Host),
|
||||
prefab.WithPort(port),
|
||||
prefab.WithHTTPHandlerFunc("/api/status", s.handleStatus),
|
||||
prefab.WithStaticFiles("/", "./server/static"),
|
||||
)
|
||||
|
||||
// Start the server
|
||||
log.Printf("Starting server on %s:%s", s.config.Host, s.config.Port)
|
||||
return s.server.Start()
|
||||
}
|
||||
|
||||
// Stop shuts down the server
|
||||
func (s *Server) Stop() error {
|
||||
if s.server != nil {
|
||||
return s.server.Shutdown()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleStatus is a placeholder API endpoint that returns server status
|
||||
func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
status := map[string]interface{}{
|
||||
"status": "ok",
|
||||
"message": "Meshtastic Stream API is running",
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(status)
|
||||
}
|
||||
Reference in New Issue
Block a user