diff --git a/cmd/main.go b/cmd/main.go index 0827c51..c003e46 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -24,10 +24,16 @@ const ( defaultServerAddr = "wss://jetstream.atproto.tools/subscribe" httpClientTimeoutDuration = time.Second * 5 transportIdleConnTimeoutDuration = time.Second * 90 + defaultPort = "8080" ) func main() { - err := godotenv.Load(".env") + envLocation := os.Getenv("ENV_LOCATION") + if envLocation == "" { + envLocation = ".env" + } + + err := godotenv.Load(envLocation) if err != nil { if !os.IsNotExist(err) { log.Fatal("Error loading .env file") @@ -62,11 +68,15 @@ func main() { } var config oauth.ClientConfig - bind := ":8080" + port := os.Getenv("PORT") + if port == "" { + port = defaultPort + } scopes := []string{"atproto", "transition:generic"} if host == "" { + host = fmt.Sprintf("http://127.0.0.1:%s", port) config = oauth.NewLocalhostConfig( - fmt.Sprintf("http://127.0.0.1%s/oauth-callback", bind), + fmt.Sprintf("%s/oauth-callback", host), scopes, ) slog.Info("configuring localhost OAuth client", "CallbackURL", config.CallbackURL) @@ -79,7 +89,7 @@ func main() { } oauthClient := oauth.NewClientApp(&config, db) - server, err := statusphere.NewServer(host, 8080, db, oauthClient, httpClient) + server, err := statusphere.NewServer(host, port, db, oauthClient, httpClient) if err != nil { slog.Error("create new server", "error", err) return diff --git a/database/oauth_sessions.go b/database/oauth_sessions.go index 41c9783..845a57e 100644 --- a/database/oauth_sessions.go +++ b/database/oauth_sessions.go @@ -48,8 +48,6 @@ func (d *DB) SaveSession(ctx context.Context, sess oauth.ClientSessionData) erro return fmt.Errorf("marshalling scopes: %w", err) } - slog.Info("session to save", "did", sess.AccountDID.String(), "session id", sess.SessionID) - sql := `INSERT INTO oauthsessions (accountDID, sessionID, hostURL, authServerURL, authServerTokenEndpoint, scopes, accessToken, refreshToken, dpopAuthServerNonce, dpopHostNonce, dpopPrivateKeyMultibase) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(accountDID) DO NOTHING;` // TODO: update on conflict _, err = d.db.Exec(sql, sess.AccountDID.String(), sess.SessionID, sess.HostURL, sess.AuthServerURL, sess.AuthServerTokenEndpoint, string(scopes), sess.AccessToken, sess.RefreshToken, sess.DPoPAuthServerNonce, sess.DPoPHostNonce, sess.DPoPPrivateKeyMultibase) if err != nil { diff --git a/home_handler.go b/home_handler.go index 5e43502..e230b99 100644 --- a/home_handler.go +++ b/home_handler.go @@ -114,8 +114,6 @@ func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) { return } - slog.Info("session", "did", did.String(), "session id", sessionID) - oauthSess, err := s.oauthClient.ResumeSession(r.Context(), *did, sessionID) if err != nil { http.Error(w, "not authenticated", http.StatusUnauthorized) diff --git a/server.go b/server.go index fa43f95..644d84e 100644 --- a/server.go +++ b/server.go @@ -2,7 +2,7 @@ package statusphere import ( "context" - _ "embed" + "embed" "encoding/json" "errors" "fmt" @@ -44,14 +44,18 @@ type Server struct { httpClient *http.Client } -func NewServer(host string, port int, store Store, oauthClient *oauth.ClientApp, httpClient *http.Client) (*Server, error) { +//go:embed html +var htmlFolder embed.FS + +func NewServer(host string, port string, store Store, oauthClient *oauth.ClientApp, httpClient *http.Client) (*Server, error) { sessionStore := sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY"))) - homeTemplate, err := template.ParseFiles("./html/home.html") + homeTemplate, err := template.ParseFS(htmlFolder, "html/home.html") if err != nil { - return nil, fmt.Errorf("parsing home template: %w", err) + return nil, fmt.Errorf("error parsing templates: %w", err) } - loginTemplate, err := template.ParseFiles("./html/login.html") + + loginTemplate, err := template.ParseFS(htmlFolder, "html/login.html") if err != nil { return nil, fmt.Errorf("parsing login template: %w", err) } @@ -83,7 +87,7 @@ func NewServer(host string, port int, store Store, oauthClient *oauth.ClientApp, mux.HandleFunc("/oauth-client-metadata.json", srv.serveClientMetadata) mux.HandleFunc("/oauth-callback", srv.handleOauthCallback) - addr := fmt.Sprintf("0.0.0.0:%d", port) + addr := fmt.Sprintf("0.0.0.0:%s", port) srv.httpserver = &http.Server{ Addr: addr, Handler: mux, @@ -140,7 +144,7 @@ func (s *Server) serveClientMetadata(w http.ResponseWriter, r *http.Request) { metadata.ClientName = &clientName metadata.ClientURI = &s.host if s.oauthClient.Config.IsConfidential() { - jwksURI := fmt.Sprintf("%s/jwks.json", r.Host) + jwksURI := fmt.Sprintf("%s/jwks.json", s.host) metadata.JWKSURI = &jwksURI }