websub-hub/cmd/hubserver/main.go

50 lines
1008 B
Go

package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"p83.nl/go/websub-hub/cmd/hubserver/storage"
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
type Stat struct {
Updates int
LastUpdate time.Time
}
func main() {
var hostPort,baseURL string
flag.StringVar(&hostPort, "http", ":80", "host and port to listen on")
flag.StringVar(&baseURL, "baseurl", "", "baseurl that the server should response with")
flag.Parse()
dsn := fmt.Sprintf("postgres://%v:%v@localhost:9999/hub?sslmode=disable", "postgres", "simple")
store, err := storage.New(dsn)
if err != nil {
log.Fatal(err)
}
defer store.Close()
handler := &subscriptionHandler{store, baseURL}
http.Handle("/", handler)
log.Fatal(http.ListenAndServe(hostPort, nil))
}