Files
go-eve-pi/main.go

80 lines
2.0 KiB
Go

package main
import (
"context"
"flag"
"os"
"os/signal"
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func main() {
flag.Parse()
logger.InitFlag()
logger.Info("Starting Eve PI")
// Create SSO instance
sso, err := NewSSO(
options.ClientID,
options.RedirectURI,
options.Scopes,
)
if err != nil {
logger.Error("Failed to create SSO instance %v", err)
return
}
// Setup fasthttp router
r := router.New()
// Configure SSO to use existing fasthttp router
sso.SetRouter(r)
// Add your own routes
r.GET("/", func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.WriteString("EVE PI Server Running")
})
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 0.0.0.0:%s", options.Port)
go func() {
if err := fasthttp.ListenAndServe("0.0.0.0:"+options.Port, r.Handler); err != nil {
logger.Error("Server failed: %v", err)
}
}()
// 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...")
logger.Info("Web server shut down cleanly")
}