renames
This commit is contained in:
parent
e7c21859ef
commit
be1f216eaa
|
@ -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})
|
||||
}
|
|
@ -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)))
|
||||
}
|
|
@ -1,11 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
import "net/http"
|
||||
|
||||
func apiWhoami(w http.ResponseWriter, r *http.Request, u *User) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(u)
|
||||
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})
|
||||
}
|
||||
|
|
|
@ -1,54 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/facebookgo/flagenv"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
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
|
||||
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() {
|
||||
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()
|
||||
|
||||
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)
|
||||
ctx := context.Background()
|
||||
os.Exit(int(subcommands.Execute(ctx)))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue