mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 09:44:21 +01:00
sync
This commit is contained in:
52
pkg/server/server.go
Normal file
52
pkg/server/server.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/sst/opencode/pkg/app"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
app *app.App
|
||||
mux *http.ServeMux
|
||||
http *http.Server
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func New(app *app.App) (*Server, error) {
|
||||
result := &Server{
|
||||
app: app,
|
||||
mux: http.NewServeMux(),
|
||||
log: slog.With("service", "server"),
|
||||
}
|
||||
result.mux.HandleFunc("/rpc", func(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
port := listener.Addr().(*net.TCPAddr).Port
|
||||
listener.Close()
|
||||
result.log.Info("listening on port", "port", port)
|
||||
|
||||
result.http = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: result.mux,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Server) Start(ctx context.Context) error {
|
||||
s.log.Info("starting server")
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
s.http.Shutdown(context.Background())
|
||||
}()
|
||||
return s.http.ListenAndServe()
|
||||
}
|
||||
Reference in New Issue
Block a user