wasmcloud/cmd/wasmcloudd/main.go

75 lines
2.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"
2019-12-08 19:13:36 +00:00
nats "github.com/nats-io/nats.go"
2019-12-08 21:44:12 +00:00
"within.website/ln/ex"
2019-12-08 14:27:42 +00:00
)
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")
2019-12-08 16:07:31 +00:00
ipfsURL = flag.String("ipfs-host", "127.0.0.1:5001", "URL of the IPFS API")
2019-12-08 19:13:36 +00:00
natsURL = flag.String("nats-url", nats.DefaultURL, "URL of the nats server")
2019-12-08 14:27:42 +00:00
db *gorm.DB
2019-12-08 19:13:36 +00:00
nc *nats.Conn
2019-12-08 14:27:42 +00:00
)
func main() {
flagenv.Parse()
flag.Parse()
gormDB, err := gorm.Open(*databaseKind, *databaseURL)
if err != nil {
log.Fatal(err)
}
log.Println("migrating")
2019-12-08 19:13:36 +00:00
gormDB.AutoMigrate(User{}, Token{}, Handler{}, ExecutionLog{})
2019-12-08 14:27:42 +00:00
db = gormDB
2019-12-08 19:13:36 +00:00
natsClient, err := nats.Connect(*natsURL)
if err != nil {
log.Fatal(err)
}
nc = natsClient
2019-12-08 14:27:42 +00:00
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"))
2019-12-08 19:13:36 +00:00
// API routes
rtr.HandleFunc("/api/handler", makeHandler(true, listHandlers)).Methods(http.MethodGet)
2019-12-13 04:55:20 +00:00
rtr.HandleFunc("/api/handler/delete", makeHandler(true, deleteHandler)).Methods(http.MethodDelete)
2019-12-08 16:07:31 +00:00
rtr.HandleFunc("/api/handler/create", makeHandler(true, createHandler)).Methods(http.MethodPost)
2019-12-13 11:48:03 +00:00
rtr.HandleFunc("/api/handler/update", makeHandler(true, updateHandler)).Methods(http.MethodPost)
2019-12-08 19:13:36 +00:00
rtr.HandleFunc("/api/handler/logs", makeHandler(true, getLogs)).Methods(http.MethodGet)
rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami))
// invocation
2019-12-09 23:01:42 +00:00
rtr.HandleFunc("/invoke", invokeHandler).Methods(http.MethodPost)
rtr.HandleFunc("/invoke/sync", invokeHandlerSync).Methods(http.MethodPost)
2019-12-08 14:27:42 +00:00
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))
log.Printf("listening on http://wasmcloud.kahless.cetacean.club:%s", *port)
2019-12-08 21:44:12 +00:00
http.ListenAndServe(":"+*port, ex.HTTPLog(rtr))
2019-12-08 14:27:42 +00:00
}