diff --git a/server/engine/engine.go b/server/engine/engine.go new file mode 100644 index 0000000..d01ff82 --- /dev/null +++ b/server/engine/engine.go @@ -0,0 +1,33 @@ +package engine + +import ( + "fmt" + "github.com/imkira/go-observer" + "sirlab.de/go/knyc/handler" + "time" +) + +type Engine struct { + obs observer.Property +} + +func NewEngine() *Engine { + engine := Engine{ + obs: observer.NewProperty(handler.Value{}), + } + return &engine +} + +func (engine *Engine) Run() { + for { + value := engine.obs.Value().(handler.Value) + fmt.Printf("sleep: %d\n", value.Value) + time.Sleep(1 * time.Second) + value.Value++ + engine.obs.Update(value) + } +} + +func (engine *Engine) GetHttpHandler() handler.HandlerFunc { + return handler.SyncFactory(engine.obs) +} diff --git a/server/handler/authmux.go b/server/handler/authmux.go index 028b969..02a27e2 100644 --- a/server/handler/authmux.go +++ b/server/handler/authmux.go @@ -4,13 +4,15 @@ import ( "net/http" ) +type HandlerFunc func(http.ResponseWriter, *http.Request) + type AuthMux struct { - mux *http.ServeMux + mux *http.ServeMux } func NewAuthMux() *AuthMux { return &(AuthMux{ - mux: http.NewServeMux(), + mux: http.NewServeMux(), }) } @@ -18,7 +20,7 @@ func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { authMux.mux.ServeHTTP(w, r) } -func (authMux *AuthMux) PublicHandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) { +func (authMux *AuthMux) PublicHandleFunc(pattern string, handlerFunc HandlerFunc) { authMux.mux.HandleFunc(pattern, handlerFunc) } diff --git a/server/handler/internal.go b/server/handler/internal.go index 1f785f9..6a59abe 100644 --- a/server/handler/internal.go +++ b/server/handler/internal.go @@ -7,13 +7,11 @@ import ( "time" ) -type HttpHandler func(http.ResponseWriter, *http.Request) - func Exit(_ http.ResponseWriter, _ *http.Request) { os.Exit(0) } -func CreateVersionHandler() HttpHandler { +func CreateVersionHandler() HandlerFunc { startTime := time.Now().Unix() return func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%d\n", startTime) diff --git a/server/handler/sync.go b/server/handler/sync.go index 5ee89ae..3b6af8d 100644 --- a/server/handler/sync.go +++ b/server/handler/sync.go @@ -11,7 +11,7 @@ type Value struct { Value int } -func SyncFactory(obs observer.Property) HttpHandler { +func SyncFactory(obs observer.Property) HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { refId, err := strconv.Atoi(r.URL.Query().Get("ref")) if err != nil { diff --git a/server/knyc.go b/server/knyc.go index 66d0def..31e5dbc 100644 --- a/server/knyc.go +++ b/server/knyc.go @@ -2,45 +2,24 @@ package main import ( "fmt" - "github.com/imkira/go-observer" "net/http" + "sirlab.de/go/knyc/engine" "sirlab.de/go/knyc/handler" - "time" ) -type Engine struct { - obs observer.Property -} - -var engine Engine - -func (engine Engine) runEngine() { - for { - value := engine.obs.Value().(handler.Value) - fmt.Printf("sleep: %d\n", value.Value) - time.Sleep(1 * time.Second) - value.Value++ - engine.obs.Update(value) - } -} - func main() { app := handler.NewAuthMux() http.Handle("/", app) app.PublicHandleFunc("/__intern__/exit", handler.Exit) - obs := observer.NewProperty(handler.Value{Value: 19}) - app.PublicHandleFunc("/sync", handler.SyncFactory(obs)) - - // hanlde login page - fsHandler := http.FileServer(http.Dir("../client/dist/")) - // default handler + fsHandler := http.FileServer(http.Dir("../client/dist/")) app.PublicHandle("/", fsHandler) - engine.obs = obs - go engine.runEngine() + eng := engine.NewEngine() + app.PublicHandleFunc("/sync", eng.GetHttpHandler()) + go eng.Run() // start listening fmt.Println("http://localhost:32039")