wasmcloud/cmd/wasmcloudd/main.go

55 lines
1.3 KiB
Go
Raw Normal View History

2019-12-08 14:27:42 +00:00
package main
import (
"flag"
"log"
"net/http"
"time"
"github.com/facebookgo/flagenv"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
var (
port = flag.String("port", "3002", "TCP port to listen on")
databaseURL = flag.String("database-url", "./wasmcloud.db", "database URL")
databaseKind = flag.String("database-kind", "sqlite3", "database kind")
defaultTokenLifetime = flag.Duration("default-token-lifetime", 500*24*time.Hour, "default token lifetime")
db *gorm.DB
)
func main() {
flagenv.Parse()
flag.Parse()
gormDB, err := gorm.Open(*databaseKind, *databaseURL)
if err != nil {
log.Fatal(err)
}
log.Println("migrating")
gormDB.AutoMigrate(User{}, Token{}, Stream{}, Handler{})
db = gormDB
rtr := mux.NewRouter()
// auth
rtr.HandleFunc("/register", registerUser)
rtr.HandleFunc("/login", loginUser)
rtr.HandleFunc("/logout", logoutUser)
// pages
rtr.HandleFunc("/", unauthenticatedShowAPage("index"))
rtr.HandleFunc("/control/", authenticatedShowAPage("controlindex"))
rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami))
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))
log.Printf("listening on http://wasmcloud.kahless.cetacean.club:%s", *port)
http.ListenAndServe(":"+*port, rtr)
}