party/cmd/api/handlers.go
2026-04-08 07:51:15 +02:00

125 lines
2.8 KiB
Go

package main
import (
// "encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"time"
// "github.com/julienschmidt/httprouter"
// "strconv"
// "errors"
// "party.at/party/internal/data"
// "party.at/party/internal/validator"
)
func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Println(r.URL.Path)
ts, err := template.ParseFiles("ui/html/home.page.tmpl", "ui/html/base.layout.tmpl")
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
return
}
err = ts.Execute(w, nil)
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
}
// data := struct {
// Name string
// }{
// Name: "Vicente",
// }
// page_template.Execute(w, data)
}
func ws(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Upgrade error:", err)
return
}
defer conn.Close()
done := make(chan struct{})
go func() {
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-done:
return
case t := <-ticker.C:
msg := map[string]interface{}{
"type": "server_tick",
"timestamp": t.Format(time.RFC3339),
}
if err := conn.WriteJSON(msg); err != nil {
fmt.Println("Write error:", err)
return
}
}
}
}()
for {
var msg Message
err := conn.ReadJSON(&msg)
if err != nil {
fmt.Println("Read error:", err)
break
}
fmt.Println(msg)
// Send a response
// response := fmt.Sprintf("Server time: %s", time.Now())
// if err := conn.WriteMessage(websocket.TextMessage, []byte(response)); err != nil {
// fmt.Println("Write error:", err)
// break
// }
}
}
// func handleMobileLogin(w http.ResponseWriter, r *http.Request) {
// // 1. Get the token from the request header
// rawIDToken := r.Header.Get("Authorization")
// // 2. Initialize the verifier (pointing to ID Austria's keys)
// verifier := provider.Verifier(&oidc.Config{ClientID: "YOUR_APP_ID"})
// // 3. Verify the signature and expiration
// idToken, err := verifier.Verify(ctx, rawIDToken)
// if err != nil {
// http.Error(w, "Invalid Token", http.StatusUnauthorized)
// return
// }
// // 4. Extract User Data
// var claims struct {
// Subject string `json:"sub"` // This is the unique ID
// Name string `json:"name"`
// }
// if err := idToken.Claims(&claims); err != nil {
// // Handle error
// }
// // 5. Create your own application session for the mobile app
// issueLocalSession(w, claims.Subject)
// }
func redirectToHTTPS(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://localhost:8443"+r.URL.RequestURI(), http.StatusMovedPermanently)
}