73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/facebookgo/flagenv"
|
|
"github.com/gorilla/mux"
|
|
"github.com/jinzhu/gorm"
|
|
nats "github.com/nats-io/nats.go"
|
|
"within.website/ln/ex"
|
|
)
|
|
|
|
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")
|
|
ipfsURL = flag.String("ipfs-host", "127.0.0.1:5001", "URL of the IPFS API")
|
|
natsURL = flag.String("nats-url", nats.DefaultURL, "URL of the nats server")
|
|
|
|
db *gorm.DB
|
|
nc *nats.Conn
|
|
)
|
|
|
|
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{}, Handler{}, ExecutionLog{})
|
|
db = gormDB
|
|
|
|
natsClient, err := nats.Connect(*natsURL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
nc = natsClient
|
|
|
|
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"))
|
|
|
|
// API routes
|
|
rtr.HandleFunc("/api/handler", makeHandler(true, listHandlers)).Methods(http.MethodGet)
|
|
rtr.HandleFunc("/api/handler/create", makeHandler(true, createHandler)).Methods(http.MethodPost)
|
|
rtr.HandleFunc("/api/handler/logs", makeHandler(true, getLogs)).Methods(http.MethodGet)
|
|
rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami))
|
|
|
|
// invocation
|
|
rtr.HandleFunc("/invoke", invokeHandler).Methods(http.MethodPost)
|
|
rtr.HandleFunc("/invoke/sync", invokeHandlerSync).Methods(http.MethodPost)
|
|
|
|
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))
|
|
|
|
log.Printf("listening on http://wasmcloud.kahless.cetacean.club:%s", *port)
|
|
http.ListenAndServe(":"+*port, ex.HTTPLog(rtr))
|
|
}
|