1
0
Fork 0

Add http package

This commit is contained in:
Gregory Eremin 2016-07-26 23:52:48 +02:00
parent c36016486d
commit a5766e57f8
4 changed files with 123 additions and 0 deletions

View File

@ -133,6 +133,10 @@ func (d *BaseDaemon) Logf(format string, v ...interface{}) {
}
}
// Startup is the empty implementation of the daemons' Startup function that
// is inherited and used by default.
func (d *BaseDaemon) Startup() {}
// Shutdown is the empty implementation of the daemons' Shutdown function that
// is inherited and used by default.
func (d *BaseDaemon) Shutdown() {}

21
examples/http/main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/localhots/shezmu"
shezttp "github.com/localhots/shezmu/http"
)
func main() {
sv := shezmu.Summon()
server := shezttp.NewServer(sv, ":2255")
server.Get("/", func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
w.Write([]byte("It works!"))
})
go server.Start()
sv.StartDaemons()
sv.HandleSignals()
}

82
http/server.go Normal file
View File

@ -0,0 +1,82 @@
package http
import (
"fmt"
"net/http"
hr "github.com/julienschmidt/httprouter"
"github.com/localhots/shezmu"
)
// Server is an implementation of HTTP server that executes requests using
// Shezmu actors.
type Server struct {
sv *shezmu.Shezmu
address string
router *hr.Router
}
type handler struct {
shezmu.BaseDaemon
name string
handle hr.Handle
}
// NewServer creates a new server.
func NewServer(sv *shezmu.Shezmu, address string) *Server {
return &Server{
sv: sv,
address: address,
router: hr.New(),
}
}
// Start starts the server.
func (s *Server) Start() error {
s.sv.Logger.Printf("Starting server at %s", s.address)
return http.ListenAndServe(s.address, s.router)
}
// Get installs a handler for GET requests.
func (s *Server) Get(path string, handle hr.Handle) {
s.router.GET(path, s.addHandlerDaemon(path, handle))
}
// Post installs a handler for POST requests.
func (s *Server) Post(path string, handle hr.Handle) {
s.router.POST(path, s.addHandlerDaemon(path, handle))
}
// Put installs a handler for PUT requests.
func (s *Server) Put(path string, handle hr.Handle) {
s.router.PUT(path, s.addHandlerDaemon(path, handle))
}
// Delete installs a handler for DELETE requests.
func (s *Server) Delete(path string, handle hr.Handle) {
s.router.DELETE(path, s.addHandlerDaemon(path, handle))
}
func (s *Server) addHandlerDaemon(path string, handle hr.Handle) hr.Handle {
h := &handler{
name: fmt.Sprintf("HTTP[%s]", path),
handle: handle,
}
s.sv.AddDaemon(h)
return h.process
}
func (h *handler) process(w http.ResponseWriter, r *http.Request, params hr.Params) {
wait := make(chan struct{})
h.Process(func() {
defer close(wait)
h.handle(w, r, params)
})
<-wait
}
func (h *handler) String() string {
return h.name
}

View File

@ -4,8 +4,10 @@ import (
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"
"github.com/localhots/shezmu/stats"
@ -114,6 +116,20 @@ func (s *Shezmu) StopDaemons() {
fmt.Println(s.runtimeStats.Fetch(stats.Latency))
}
func (s *Shezmu) HandleSignals() {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT)
for {
switch sig := <-ch; sig {
case syscall.SIGINT:
s.StopDaemons()
return
default:
s.Logger.Printf("Signal ignored: %s", sig)
}
}
}
func (s *Shezmu) setupDaemon(d Daemon) {
defer func() {
if err := recover(); err != nil {