Skip to content

Commit 80a0fae

Browse files
committed
release 1.0.0-beta-1 version including dev server
1 parent 5ed8d87 commit 80a0fae

File tree

11 files changed

+1413
-0
lines changed

11 files changed

+1413
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.demo-backend.yaml
2+
dist/
3+
14
# Binaries for programs and plugins
25
*.exe
36
*.exe~

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
build:
2+
@go build -o backend
3+
4+
test:
5+
@go test -cover ./...
6+
7+
pkg: build
8+
@rm -rf dist/*
9+
@echo "building linux binaries"
10+
@GOARCH=amd64 GOOS=linux go build -o dist/binary-for-linux-64-bit
11+
@GOARCH=386 GOOS=linux go build -o dist/binary-for-linux-32-bit
12+
@echo "building mac binaries"
13+
@GOARCH=amd64 GOOS=darwin go build -o dist/binary-for-mac-64-bit
14+
@GOARCH=386 GOOS=darwin go build -o dist/binary-for-mac-32-bit
15+
@echo "building windows binaries"
16+
@GOARCH=amd64 GOOS=windows go build -o dist/binary-for-windows-64-bit.exe
17+
@GOARCH=386 GOOS=windows go build -o dist/binary-for-windows-32-bit.exe
18+
@echo "compressing binaries"
19+
@gzip dist/*

backend

12.7 MB
Binary file not shown.

cmd/proxy.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"net/http"
23+
"net/http/httputil"
24+
"net/url"
25+
"os"
26+
27+
"github.com/spf13/cobra"
28+
"github.com/spf13/viper"
29+
)
30+
31+
var proxyTarget string
32+
33+
// proxyCmd represents the proxy command
34+
var proxyCmd = &cobra.Command{
35+
Use: "proxy",
36+
Short: "Proxy requests to production instead of using the local dev server.",
37+
Long: fmt.Sprintf(`
38+
%s
39+
40+
This command starts a proxy server relaying your requests to the production
41+
backend.
42+
43+
It is useful if you'd like to test your application against the production
44+
backend without deploying your application changes yet.
45+
46+
All requests are proxy as-is, and the responses sent to you without any
47+
modifications.
48+
`, clbold(clsecondary("Proxy requests to production"))),
49+
Run: func(cmd *cobra.Command, args []string) {
50+
f := cmd.Flag("port")
51+
startProxy(f.Value.String())
52+
},
53+
}
54+
55+
func init() {
56+
rootCmd.AddCommand(proxyCmd)
57+
58+
// Here you will define your flags and configuration settings.
59+
60+
// Cobra supports Persistent Flags which will work for this command
61+
// and all subcommands, e.g.:
62+
// proxyCmd.PersistentFlags().String("foo", "", "A help for foo")
63+
64+
// Cobra supports local flags which will only run when this command
65+
// is called directly, e.g.:
66+
proxyCmd.Flags().Int32P("port", "p", 8099, "port for the proxy server")
67+
}
68+
69+
func startProxy(port string) {
70+
region := viper.GetString("region")
71+
if len(region) == 0 {
72+
fmt.Printf("%s %s %s\n", cldanger("Missing a"), clerror("region"), cldanger("config entry in your config file"))
73+
os.Exit(1)
74+
}
75+
76+
proxyTarget = fmt.Sprintf("https://%s.staticbackend.com", region)
77+
78+
http.HandleFunc("/", proxy)
79+
80+
fmt.Printf("%s http://localhost:%s\n", clsecondary("Proxy server started at:"), port)
81+
log.Fatal(http.ListenAndServe(":"+port, nil))
82+
}
83+
84+
func proxy(w http.ResponseWriter, r *http.Request) {
85+
t, err := url.Parse(proxyTarget)
86+
if err != nil {
87+
log.Fatal("invalid proxy target: ", err)
88+
}
89+
90+
proxy := httputil.NewSingleHostReverseProxy(t)
91+
92+
// Update the headers to allow for SSL redirection
93+
r.URL.Host = "na1.staticbackend.com"
94+
r.URL.Scheme = "https"
95+
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
96+
r.Host = "na1.staticbackend.com"
97+
98+
// Note that ServeHttp is non blocking and uses a go routine under the hood
99+
proxy.ServeHTTP(w, r)
100+
}

cmd/root.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/gookit/color"
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
)
27+
28+
const (
29+
VERSION = "v1.0.0-beta1"
30+
)
31+
32+
var (
33+
clgreen = color.FgGreen.Render
34+
clinfo = color.Info.Render
35+
clnote = color.Note.Render
36+
cllight = color.Light.Render
37+
clerror = color.Error.Render
38+
cldanger = color.Danger.Render
39+
cldebug = color.Debug.Render
40+
clnotice = color.Notice.Render
41+
clsuccess = color.Success.Render
42+
clcomment = color.Comment.Render
43+
clprimary = color.Primary.Render
44+
clwarning = color.Warn.Render
45+
clquestion = color.Question.Render
46+
clsecondary = color.Secondary.Render
47+
clbold = color.Bold.Render
48+
)
49+
50+
var cfgFile string
51+
52+
// rootCmd represents the base command when called without any subcommands
53+
var rootCmd = &cobra.Command{
54+
Use: "backend",
55+
Short: "StaticBackend CLI for local development, managing resources, and your account.",
56+
Long: fmt.Sprintf(`
57+
%s
58+
59+
This CLI gives you the following functionalities:
60+
61+
- A local development server: %s
62+
- Managing your backend resources: %s
63+
- Managing your account: %s
64+
`,
65+
clbold(clsecondary("StaticBackend CLI "+VERSION)),
66+
clbold("backend server"),
67+
clsecondary("soon"),
68+
clsecondary("soon"),
69+
),
70+
// Uncomment the following line if your bare application
71+
// has an action associated with it:
72+
Run: func(cmd *cobra.Command, args []string) {
73+
if cmd.Flag("version").Value.String() == "true" {
74+
fmt.Println(VERSION)
75+
} else {
76+
fmt.Println(cmd.Long)
77+
fmt.Println("")
78+
cmd.Usage()
79+
}
80+
},
81+
}
82+
83+
// Execute adds all child commands to the root command and sets flags appropriately.
84+
// This is called by main.main(). It only needs to happen once to the rootCmd.
85+
func Execute() {
86+
if err := rootCmd.Execute(); err != nil {
87+
fmt.Println(err)
88+
os.Exit(1)
89+
}
90+
}
91+
92+
func init() {
93+
cobra.OnInitialize(initConfig)
94+
95+
// Here you will define your flags and configuration settings.
96+
// Cobra supports persistent flags, which, if defined here,
97+
// will be global for your application.
98+
99+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $PWD/.backend.yaml)")
100+
rootCmd.PersistentFlags().Bool("no-color", false, "turns color off")
101+
102+
// Cobra also supports local flags, which will only run
103+
// when this action is called directly.
104+
rootCmd.Flags().BoolP("version", "v", false, "display current version")
105+
}
106+
107+
// initConfig reads in config file and ENV variables if set.
108+
func initConfig() {
109+
if cfgFile != "" {
110+
// Use config file from the flag.
111+
viper.SetConfigFile(cfgFile)
112+
} else {
113+
// Find home directory.
114+
pwd, err := os.Getwd()
115+
if err != nil {
116+
fmt.Println(err)
117+
os.Exit(1)
118+
}
119+
120+
// Search config in home directory with name ".cli" (without extension).
121+
viper.AddConfigPath(pwd)
122+
viper.SetConfigName(".backend")
123+
}
124+
125+
viper.AutomaticEnv() // read in environment variables that match
126+
127+
// If a config file is found, read it in.
128+
if err := viper.ReadInConfig(); err == nil {
129+
fmt.Println("Using config file:", viper.ConfigFileUsed())
130+
}
131+
}

0 commit comments

Comments
 (0)