This commit is contained in:
Cadey Ratio 2019-12-08 14:27:42 +00:00
parent e7c21859ef
commit be1f216eaa
13 changed files with 95 additions and 95 deletions

View File

@ -1,17 +0,0 @@
package main
import "net/http"
var apiConfig *Config
func withAPI(req *http.Request) {
if apiConfig == nil {
var err error
apiConfig, err = loadConfig()
if err != nil {
panic(err)
}
}
req.AddCookie(&http.Cookie{Name: "wasmcloud-token", Value: apiConfig.Token})
}

View File

@ -1,30 +0,0 @@
package main
import (
"context"
"flag"
"os"
"path/filepath"
"github.com/google/subcommands"
)
var (
apiServer = flag.String("api-server", "http://wasmcloud.kahless.cetacean.club:3002", "default API server")
configLocation = flag.String("config", filepath.Join(os.Getenv("HOME"), ".wasmc.json"), "default config location")
)
func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&loginCmd{}, "")
subcommands.Register(&whoamiCmd{}, "")
subcommands.Register(namegenCmd{}, "")
subcommands.ImportantFlag("api-server")
subcommands.ImportantFlag("config")
flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
}

View File

@ -1,11 +1,17 @@
package main package main
import ( import "net/http"
"encoding/json"
"net/http"
)
func apiWhoami(w http.ResponseWriter, r *http.Request, u *User) { var apiConfig *Config
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(u) func withAPI(req *http.Request) {
if apiConfig == nil {
var err error
apiConfig, err = loadConfig()
if err != nil {
panic(err)
}
}
req.AddCookie(&http.Cookie{Name: "wasmcloud-token", Value: apiConfig.Token})
} }

View File

@ -1,54 +1,30 @@
package main package main
import ( import (
"context"
"flag" "flag"
"log" "os"
"net/http" "path/filepath"
"time"
"github.com/facebookgo/flagenv" "github.com/google/subcommands"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
) )
var ( var (
port = flag.String("port", "3002", "TCP port to listen on") apiServer = flag.String("api-server", "http://wasmcloud.kahless.cetacean.club:3002", "default API server")
databaseURL = flag.String("database-url", "./wasmcloud.db", "database URL") configLocation = flag.String("config", filepath.Join(os.Getenv("HOME"), ".wasmc.json"), "default config location")
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() { func main() {
flagenv.Parse() subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&loginCmd{}, "")
subcommands.Register(&whoamiCmd{}, "")
subcommands.Register(namegenCmd{}, "")
subcommands.ImportantFlag("api-server")
subcommands.ImportantFlag("config")
flag.Parse() flag.Parse()
ctx := context.Background()
gormDB, err := gorm.Open(*databaseKind, *databaseURL) os.Exit(int(subcommands.Execute(ctx)))
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)
} }

11
cmd/wasmcloudd/api.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"encoding/json"
"net/http"
)
func apiWhoami(w http.ResponseWriter, r *http.Request, u *User) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(u)
}

54
cmd/wasmcloudd/main.go Normal file
View File

@ -0,0 +1,54 @@
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)
}