Skip to content

Commit

Permalink
add insecure option to server module
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 15, 2020
1 parent b9fe406 commit 57134eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import (
"github.com/cfi2017/bl3-save/internal/server"
"github.com/cfi2017/bl3-save/internal/shared"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string

var (
insecure bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bl3-save",
Expand All @@ -43,7 +48,9 @@ to quickly create a Cobra application.`,
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
shared.OpenBrowser("https://bl3.swiss.dev/")
if err := server.Start(); err != nil {
opts := server.Options{}
opts.Insecure = insecure
if err := server.Start(opts); err != nil {
panic(err)
}
},
Expand All @@ -60,7 +67,6 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
Expand All @@ -69,7 +75,7 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolVar(&insecure, "insecure", false, "Run server without cors protection")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
5 changes: 5 additions & 0 deletions internal/server/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package server

type Options struct {
Insecure bool
}
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ func init() {
pwd, _ = os.Getwd()
}

func Start() error {
func Start(opts Options) error {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.LoggerWithWriter(os.Stderr, "/stats"), gin.Recovery())

r.Use(cors.New(cors.Config{
AllowAllOrigins: opts.Insecure,
AllowOrigins: []string{"https://bl3.swiss.dev", "http://localhost:4200"},
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"Origin", "Content-Type"},
Expand Down

0 comments on commit 57134eb

Please sign in to comment.