mirror of
https://github.com/dpup/meshstream.git
synced 2026-03-28 17:42:37 +01:00
Add web server using prefab framework
This commit is contained in:
69
server/server.go
Normal file
69
server/server.go
Normal file
@@ -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)
|
||||
}
|
||||
40
server/static/index.html
Normal file
40
server/static/index.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Meshtastic Stream</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
}
|
||||
#messages {
|
||||
margin-top: 20px;
|
||||
border: 1px solid #ddd;
|
||||
padding: 10px;
|
||||
height: 400px;
|
||||
overflow-y: auto;
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Meshtastic Stream</h1>
|
||||
<div id="messages">
|
||||
<p>Waiting for messages...</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Placeholder for future WebSocket/API integration
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
console.log('Page loaded, ready to connect to API');
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user