refactor code

This commit is contained in:
BuckarooBanzay 2021-09-03 08:02:15 +02:00
parent 402ad73377
commit 081b968fc2
7 changed files with 128 additions and 77 deletions

66
api.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"auth_proxy/types"
"encoding/json"
"net/http"
"time"
)
func MinetestEndpoint(w http.ResponseWriter, req *http.Request) {
logger.Printf("Got mod-request from %s, method: %s", req.Host, req.Method)
if req.Method == http.MethodGet {
select {
case msg := <-loginMessages:
logger.Printf("Relaying message to mod: name=%s", msg.Username)
err := json.NewEncoder(w).Encode(msg)
if err != nil {
SendError(w, err.Error())
return
}
case <-time.After(30 * time.Second):
// timed out without data
w.WriteHeader(204)
}
} else if req.Method == http.MethodPost {
msg := &types.ModMessage{}
err := json.NewDecoder(req.Body).Decode(msg)
if err != nil {
SendError(w, err.Error())
return
}
logger.Printf("Received message from mod: name=%s", msg.Name)
ee.Emit("mod-message", msg)
}
}
func LoginEndpoint(w http.ResponseWriter, req *http.Request) {
msg := &types.LoginMessage{}
err := json.NewDecoder(req.Body).Decode(&msg)
if err != nil {
SendError(w, err.Error())
return
}
logger.Printf("Got request from host=%s, username=%s", req.Host, msg.Username)
done := make(chan bool)
lh := &LoginHandler{
done: done,
username: msg.Username,
w: w,
}
ee.AddListener(lh)
logger.Printf("Sending loginmessage to event channel")
loginMessages <- msg
select {
case <-time.After(5 * time.Second):
SendError(w, "timed out")
case <-done:
}
ee.RemoveListener(lh)
}

View File

@ -20,7 +20,7 @@ func NewEventEmitter() *EventEmitter {
}
}
func (ee *EventEmitter) AddListener(name string, handler EventHandler) {
func (ee *EventEmitter) AddListener(handler EventHandler) {
ee.mutex.Lock()
defer ee.mutex.Unlock()
@ -36,7 +36,7 @@ func (ee *EventEmitter) Emit(name string, payload interface{}) {
}
}
func (ee *EventEmitter) RemoveListener(name string, remove_handler EventHandler) {
func (ee *EventEmitter) RemoveListener(remove_handler EventHandler) {
ee.mutex.Lock()
defer ee.mutex.Unlock()

View File

@ -26,9 +26,9 @@ func TestEventEmitter(t *testing.T) {
if handler.count != 0 {
t.Fatal("Count not 0")
}
ee.AddListener("my-event", handler)
ee.AddListener(handler)
ee.Emit("my-event", "test")
ee.RemoveListener("my-event", handler)
ee.RemoveListener(handler)
if handler.count != 1 {
t.Fatal("Count not 0")

39
loginhandler.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"auth_proxy/types"
"encoding/json"
"net/http"
)
type LoginHandler struct {
username string
w http.ResponseWriter
done chan bool
}
func (lh *LoginHandler) OnEvent(name string, payload interface{}) {
if name != "mod-message" {
// Wrong event
return
}
msg, ok := payload.(*types.ModMessage)
if !ok {
// Wrong type
return
}
if msg.Name != lh.username {
// Wrong username
return
}
lh.w.WriteHeader(200)
err := json.NewEncoder(lh.w).Encode(msg)
if err != nil {
SendError(lh.w, err.Error())
}
lh.done <- true
}

77
main.go
View File

@ -1,13 +1,15 @@
package main
import (
"encoding/json"
"auth_proxy/eventemitter"
"auth_proxy/types"
"log"
"net/http"
"time"
)
var logger = log.Default()
var loginMessages = make(chan *types.LoginMessage)
var ee = eventemitter.NewEventEmitter()
func main() {
mux := http.NewServeMux()
@ -20,74 +22,3 @@ func main() {
panic(err)
}
}
var toMt = make(chan json.RawMessage)
var fromMt = make(chan json.RawMessage)
func SendError(w http.ResponseWriter, msg string) {
logger.Printf("Error: %s", msg)
w.WriteHeader(500)
w.Write([]byte(msg))
}
//TODO: check for race conditions
func MinetestEndpoint(w http.ResponseWriter, req *http.Request) {
logger.Printf("Got mod-request from %s, method: %s", req.Host, req.Method)
if req.Method == http.MethodGet {
select {
case msg := <-toMt:
logger.Printf("Relaying message to mod")
err := json.NewEncoder(w).Encode(msg)
if err != nil {
SendError(w, err.Error())
return
}
case <-time.After(30 * time.Second):
// timed out without data
w.WriteHeader(204)
}
} else if req.Method == http.MethodPost {
msg := json.RawMessage{}
err := json.NewDecoder(req.Body).Decode(&msg)
if err != nil {
SendError(w, err.Error())
return
}
logger.Printf("Received message from mod: %s", string(msg))
select {
case fromMt <- msg:
default:
SendError(w, "no receiver available")
}
}
}
func LoginEndpoint(w http.ResponseWriter, req *http.Request) {
logger.Printf("Got request from %s", req.Host)
msg := json.RawMessage{}
err := json.NewDecoder(req.Body).Decode(&msg)
if err != nil {
SendError(w, err.Error())
return
}
select {
case toMt <- msg:
case <-time.After(5 * time.Second):
SendError(w, "timed out sending")
return
}
select {
case msg = <-fromMt:
w.WriteHeader(200)
err = json.NewEncoder(w).Encode(msg)
if err != nil {
SendError(w, err.Error())
}
case <-time.After(5 * time.Second):
SendError(w, "timed out receiving")
}
}

6
types/loginmessage.go Normal file
View File

@ -0,0 +1,6 @@
package types
type LoginMessage struct {
Username string `json:"username"`
Password string `json:"password"`
}

9
util.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "net/http"
func SendError(w http.ResponseWriter, msg string) {
logger.Printf("Error: %s", msg)
w.WriteHeader(500)
w.Write([]byte(msg))
}