Rewrite to use fasthttp

This commit is contained in:
2025-10-10 20:19:50 +02:00
parent 2c727632b8
commit a1f568cbe6
4 changed files with 108 additions and 125 deletions

72
main.go
View File

@@ -4,14 +4,15 @@ import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/fasthttp/router"
"github.com/joho/godotenv"
"github.com/valyala/fasthttp"
)
type Options struct {
@@ -46,61 +47,54 @@ func main() {
return
}
// Setup HTTP server
mux := http.NewServeMux()
// Setup fasthttp router
r := router.New()
// Configure SSO to use existing muxer
sso.SetMuxer(mux)
// Configure SSO to use existing fasthttp router
sso.SetRouter(r)
// Add your own routes
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("EVE PI Server Running"))
r.GET("/", func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.WriteString("EVE PI Server Running")
})
server := &http.Server{
Addr: ":3000",
Handler: mux,
}
r.GET("/login/{character}", func(ctx *fasthttp.RequestCtx) {
charName := ctx.UserValue("character")
charNameStr, ok := charName.(string)
if !ok || charNameStr == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.WriteString("Missing character parameter")
return
}
logger.Info("Login requested for character %s", charNameStr)
// Trigger the auth flow (will register callback if needed)
token, err := sso.GetToken(context.Background(), charNameStr)
if err != nil {
logger.Error("Failed to authenticate character %s: %v", charNameStr, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.WriteString("Authentication failed")
return
}
logger.Info("Successfully authenticated character %s", charNameStr)
ctx.SetContentType("text/plain")
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.WriteString("Authenticated! Access token: " + token)
})
logger.Info("Starting web server on :3000")
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := fasthttp.ListenAndServe(":3000", r.Handler); err != nil {
logger.Error("Server failed: %v", err)
}
}()
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
charName := r.URL.Query().Get("character")
if charName == "" {
http.Error(w, "Missing character parameter", http.StatusBadRequest)
return
}
logger.Info("Login requested for character %s", charName)
// Trigger the auth flow (will register callback if needed)
token, err := sso.GetToken(r.Context(), charName)
if err != nil {
logger.Error("Failed to authenticate character %s: %v", charName, err)
http.Error(w, "Authentication failed", http.StatusInternalServerError)
return
}
logger.Info("Successfully authenticated character %s", charName)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Authenticated! Access token: " + token))
})
// Listen for SIGINT and gracefully shut down the server
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
<-sigCh
logger.Info("SIGINT received, shutting down web server gracefully...")
if err := server.Shutdown(context.Background()); err != nil {
logger.Error("Error shutting down server: %v", err)
} else {
logger.Info("Web server shut down cleanly")
}
logger.Info("Web server shut down cleanly")
}
func LoadOptions() (Options, error) {