58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
|
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);
|
||
|
}
|