go-podcast-proxy/main.go

58 lines
1.3 KiB
Go
Raw Normal View History

2024-08-03 20:05:08 +02:00
package main
import (
"log"
"strconv"
"os"
"time"
"os/signal"
"syscall"
"net/http"
)
func (ctx *ServerContext) Sleep(seconds uint) {
for i:=(uint)(0); i<seconds && ctx.IsActive; i++ {
time.Sleep(1 * time.Second)
}
}
func main(){
ctx := &ServerContext{}
if err := ctx.LoadConfig("config.toml"); err!=nil {
log.Fatal(err)
}
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func(){
<-sigs
log.Println("shutdown..")
ctx.IsActive = false
ctx.WG.Wait()
os.Exit(0)
log.Println("bye");
}()
ctx.WG.Add(1)
go func(){
defer ctx.WG.Done()
for ctx.IsActive {
ctx.WG.Add(1)
ctx.UpdateCache()
ctx.Sleep(60)
}
}()
ctx.IsActive = true
http.HandleFunc("/feed/", func (w http.ResponseWriter, req *http.Request) {
if err := ctx.HandleFeed(w, req); err!=nil {
log.Fatal(err)
}
});
http.HandleFunc("/file/", func (w http.ResponseWriter, req *http.Request) {
if err := ctx.HandleFile(w, req); err!=nil {
log.Fatal(err)
}
});
http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), nil);
}