Skip to content

Commit

Permalink
Refactor package structure for web functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Oct 26, 2024
1 parent fcfa22b commit 1c69097
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/gorace.go → internal/web/entry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gorace
package web

import (
"fmt"
Expand Down
126 changes: 126 additions & 0 deletions internal/web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package web

import (
"fmt"
"net/http"
)

func StartWebServer() {
go func() {
request := http.NewServeMux()

// Log all inbound requests
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Received request: ", r.URL.Path)
request.ServeHTTP(w, r)
})


request.HandleFunc("/AuthLogin", func(w http.ResponseWriter, r *http.Request) {
handleAuthentication(r, w)
})

request.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
})
http.ListenAndServe(":3000", nil)
}()
}

type AuthLoginResponse struct {
Valid bool
Ticket string
ReasonCode string
ReasonText string
ReasonUrl string
}

func (r *AuthLoginResponse) IsValid() bool {
return r.Valid
}

func (r *AuthLoginResponse) GetTicket() string {
return r.Ticket
}

func (r *AuthLoginResponse) GetReasonCode() string {
return r.ReasonCode
}

func (r *AuthLoginResponse) GetReasonText() string {
return r.ReasonText
}

func (r *AuthLoginResponse) GetReasonUrl() string {
return r.ReasonUrl
}

func NewAuthLoginResponse() *AuthLoginResponse {
return &AuthLoginResponse{}
}

func (r *AuthLoginResponse) SetValid(ticket string) {
r.Valid = true
r.Ticket = ticket
}

func (r *AuthLoginResponse) SetInvalid(reasonCode string, reasonText string, reasonUrl string) {
r.Valid = false
r.ReasonCode = reasonCode
r.ReasonText = reasonText
r.ReasonUrl = reasonUrl
}

func (r *AuthLoginResponse) formatValidResponse() string {
return fmt.Sprintf("Valid=TRUE\nTicket=%s", r.Ticket)
}

func (r *AuthLoginResponse) formatInvalidResponse() string {
return fmt.Sprintf("reasoncode=%s\nreasontext=%s\nreasonurl=%s", r.ReasonCode, r.ReasonText, r.ReasonUrl)
}

func (r *AuthLoginResponse) formatResponse() string {
if r.Valid {
return r.formatValidResponse()
} else {
return r.formatInvalidResponse()
}
}

func writeResponse(w http.ResponseWriter, response string) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, response)
}

func handleAuthentication(r *http.Request, w http.ResponseWriter) {
username := r.URL.Query().Get("username")
password := r.URL.Query().Get("password")

fmt.Println("Authenticating user: ", username)

var userId = AuthenticateUser(username, password)

authResponse := NewAuthLoginResponse()

if userId > 0 {
fmt.Println("User #", userId, " authenticated")
authResponse.SetValid("d316cd2dd6bf870893dfbaaf17f965884e")
writeResponse(w, authResponse.formatResponse())
} else {
fmt.Println("User not authenticated")
authResponse.SetInvalid("INV-200", "Opps~", "https://www.winehq.com")
writeResponse(w, authResponse.formatResponse())
}

}

func AuthenticateUser(username string, password string) (userId int) {
userId = 0

if username == "admin" && password == "admin" {
fmt.Println("User authenticated")
userId = 1
}

return userId
}
3 changes: 1 addition & 2 deletions src/web_test.go → internal/web/web_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gorace_test
package web

import (
"fmt"
"testing"
. "github.com/rustymotors/gorace/src"
)

func TestAuthenticateUser(t *testing.T) {
Expand Down
15 changes: 4 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ package main

import (
"fmt"
"github.com/rustymotors/gorace/src"


"github.com/rustymotors/gorace/internal/web"
)







// ========================
// Main function
// ========================
Expand All @@ -24,7 +17,7 @@ func main() {
fmt.Println("Server started")

// Start a web server on port 3000
gorace.StartWebServer()
web.StartWebServer()

// List of ports to listen to
ports := []string{"8226", "8227", "8228", "7003"}
Expand All @@ -34,10 +27,10 @@ func main() {
*/
for _, port := range ports {
// Listen for an incoming connection. Break the loop when a signal is received.
gorace.StartListeningOnPort(port)
web.StartListeningOnPort(port)
}

go gorace.ListenForKeyboardEvents(ShutdownFlag)
go web.ListenForKeyboardEvents(ShutdownFlag)

// Wait for the shutdown signal
<-ShutdownFlag
Expand Down
61 changes: 0 additions & 61 deletions src/web.go

This file was deleted.

0 comments on commit 1c69097

Please sign in to comment.