2016-08-21 22:31:37 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"s.mcquay.me/dm/chipmunk"
|
|
|
|
|
2016-08-25 11:26:49 -07:00
|
|
|
"github.com/gorilla/context"
|
2016-08-21 22:31:37 -07:00
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
CookieSecret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-08-22 13:03:23 -07:00
|
|
|
var host, clientId, clientSecret, cookieSecret string
|
2016-08-21 22:31:37 -07:00
|
|
|
var port int
|
|
|
|
|
|
|
|
var run = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "run command",
|
|
|
|
Long: `run chipd with given options`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := &Config{}
|
|
|
|
err := envconfig.Process("chipd", config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if host != "" {
|
|
|
|
config.Host = host
|
|
|
|
}
|
|
|
|
if port != -1 {
|
|
|
|
config.Port = port
|
|
|
|
} else {
|
|
|
|
if config.Port == 0 {
|
2016-08-23 09:15:27 -07:00
|
|
|
config.Port = 8080
|
2016-08-21 22:31:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if clientId != "" {
|
|
|
|
config.ClientID = clientId
|
|
|
|
}
|
|
|
|
if clientSecret != "" {
|
|
|
|
config.ClientSecret = clientSecret
|
|
|
|
}
|
|
|
|
if cookieSecret != "" {
|
|
|
|
config.CookieSecret = cookieSecret
|
|
|
|
} else {
|
|
|
|
if config.CookieSecret == "" {
|
|
|
|
config.CookieSecret = strconv.Itoa(rand.Int())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.ClientID == "" || config.ClientSecret == "" {
|
|
|
|
log.Println("Please provide both a clientId and a clientSecret")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
log.Printf("%+v", config)
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, os.Interrupt, os.Kill)
|
|
|
|
go func() {
|
|
|
|
s := <-sigs
|
|
|
|
log.Printf("signal: %+v", s)
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
|
|
|
_ = chipmunk.NewServer(
|
|
|
|
sm,
|
|
|
|
config.ClientID,
|
|
|
|
config.ClientSecret,
|
|
|
|
config.CookieSecret,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
|
|
|
|
hostname := "localhost"
|
|
|
|
if config.Host == "" {
|
|
|
|
hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("problem getting hostname:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("serving at: http://%s:%d/", hostname, config.Port)
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
|
2016-08-25 11:26:49 -07:00
|
|
|
err = http.ListenAndServe(addr, context.ClearHandler(sm))
|
2016-08-21 22:31:37 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
run.Flags().StringVarP(
|
|
|
|
&host,
|
|
|
|
"host",
|
|
|
|
"n",
|
|
|
|
"",
|
|
|
|
"hostname",
|
|
|
|
)
|
|
|
|
run.Flags().IntVarP(
|
|
|
|
&port,
|
|
|
|
"port",
|
|
|
|
"p",
|
|
|
|
-1,
|
|
|
|
"port",
|
|
|
|
)
|
|
|
|
run.Flags().StringVarP(
|
|
|
|
&clientId,
|
|
|
|
"clientId",
|
|
|
|
"i",
|
|
|
|
"",
|
|
|
|
"github oauth clientId",
|
|
|
|
)
|
|
|
|
run.Flags().StringVarP(
|
|
|
|
&clientSecret,
|
|
|
|
"clientSecret",
|
|
|
|
"s",
|
|
|
|
"",
|
|
|
|
"github oauth clientSecret",
|
|
|
|
)
|
|
|
|
run.Flags().StringVarP(
|
|
|
|
&cookieSecret,
|
|
|
|
"cookieSecret",
|
|
|
|
"c",
|
|
|
|
"",
|
|
|
|
"cookieSecret",
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{Use: "app"}
|
|
|
|
rootCmd.AddCommand(run)
|
|
|
|
rootCmd.Execute()
|
|
|
|
}
|