generated from ElnuDev/go-project
Rearrange repo as monorepo
This commit is contained in:
parent
2a59a96493
commit
848175efde
11 changed files with 216 additions and 172 deletions
34
shiritori/api/events.go
Normal file
34
shiritori/api/events.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.elnu.com/ElnuDev/shiritori-go/httputils"
|
||||
. "git.elnu.com/ElnuDev/shiritori-go/shiritori"
|
||||
)
|
||||
|
||||
func GenerateApiEvents(clients *ClientSet) httputils.Handler {
|
||||
return httputils.GenerateSseHandler(func(w http.ResponseWriter, r *http.Request) {
|
||||
sendEvent := func(events ...httputils.SseEvent) {
|
||||
for _, event := range events {
|
||||
fmt.Fprint(w, event.Encode())
|
||||
}
|
||||
w.(http.Flusher).Flush()
|
||||
}
|
||||
sendEvent(clients.PlayerCountEvent(1))
|
||||
ctx := r.Context()
|
||||
channel := make(Client)
|
||||
clients.Connect(channel)
|
||||
outer:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
clients.Disconnect(channel)
|
||||
break outer
|
||||
case events := <-channel:
|
||||
sendEvent(events...)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
20
shiritori/api/submit.go
Normal file
20
shiritori/api/submit.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.elnu.com/ElnuDev/shiritori-go/httputils"
|
||||
. "git.elnu.com/ElnuDev/shiritori-go/shiritori"
|
||||
)
|
||||
|
||||
func GenerateApiSubmit(clients *ClientSet) httputils.Handler {
|
||||
return httputils.GenerateHandler(
|
||||
"",
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseMultipartForm(0)
|
||||
clients.BroadcastWord(r.FormValue("word"))
|
||||
},
|
||||
nil,
|
||||
[]string{http.MethodPost},
|
||||
)
|
||||
}
|
Reference in a new issue