You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"net/http" |
|
"os" |
|
"os/signal" |
|
"stick/internal/handlers" |
|
"syscall" |
|
"time" |
|
|
|
"html/template" |
|
|
|
"github.com/joho/godotenv" |
|
"github.com/markbates/goth" |
|
"github.com/markbates/goth/providers/twitch" |
|
) |
|
|
|
func main() { |
|
err := godotenv.Load() |
|
if err != nil { |
|
fmt.Printf("error loading .env file: %v", err) |
|
} |
|
fs := http.FileServer(http.Dir("./static")) |
|
tmpl, err := template.ParseGlob("templates/*.html") |
|
mux := http.NewServeMux() |
|
h := handlers.Handler{Template: *tmpl} |
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs)) |
|
mux.HandleFunc("/", h.Home) |
|
goth.UseProviders( |
|
twitch.New( |
|
os.Getenv("TWITCH_CLIENT_ID"), |
|
os.Getenv("TWITCH_SECRET"), |
|
os.Getenv("TWITCH_CALLBACK"), |
|
"channel:read:redemptions", |
|
"channel:manage:redemptions", |
|
), |
|
) |
|
fmt.Printf("twitch callback: %s", os.Getenv("TWITCH_CALLBACK")) |
|
|
|
if err != nil { |
|
panic(fmt.Errorf("failed to parse templates: %w\n", err)) |
|
} |
|
|
|
go func() { |
|
hub := handlers.Hub |
|
hub.Run() |
|
}() |
|
|
|
mux.HandleFunc("/ws", h.WsHandler) |
|
mux.HandleFunc("/auth/twitch", h.Auth) |
|
mux.HandleFunc("/auth/twitch/callback", h.Callback) |
|
mux.HandleFunc("/logout", h.Logout) |
|
|
|
server := &http.Server{ |
|
Addr: ":8080", |
|
Handler: mux, |
|
} |
|
|
|
go func() { |
|
fmt.Println("Server started...") |
|
fmt.Println(os.Getenv("TWITCH_CALLBACK")) |
|
|
|
if err := server.ListenAndServe(); err != http.ErrServerClosed { |
|
fmt.Printf("Serve error: %v\n", err) |
|
} |
|
}() |
|
|
|
stop := make(chan os.Signal, 1) |
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) |
|
|
|
<-stop |
|
fmt.Println("Shutting down server") |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
|
defer cancel() |
|
|
|
if err := server.Shutdown(ctx); err != nil { |
|
fmt.Printf("Server force to shutdown: %v\n", err) |
|
} else { |
|
fmt.Println("Server exit gracefully") |
|
} |
|
}
|
|
|