-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (56 loc) · 1.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"github.com/depado/platypus/cmd"
"github.com/depado/platypus/infra"
"github.com/depado/platypus/mocker"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Main command that will be run when no other command is provided on the
// command-line
var rootCmd = &cobra.Command{
Use: "platypus",
Short: "Platypus is a very simple mock server",
Run: func(cmd *cobra.Command, args []string) { run() }, // nolint: unparam
}
func run() {
var err error
fmt.Println(cmd.Logo)
gin.SetMode(viper.GetString("server.mode"))
r := gin.Default()
corsc := infra.NewCorsConfig(
viper.GetBool("server.cors.enable"),
viper.GetBool("server.cors.all"),
viper.GetStringSlice("server.cors.origins"),
viper.GetStringSlice("server.cors.methods"),
viper.GetStringSlice("server.cors.headers"),
viper.GetStringSlice("server.cors.expose"),
)
if corsc != nil {
logrus.Info("CORS Enabled")
r.Use(cors.New(*corsc))
}
logrus.Info("Generating Routes")
if err = mocker.GenerateRoutes(viper.GetString("mock"), r); err != nil {
logrus.WithError(err).Fatal("Couldn't generate routes")
}
fmt.Println()
logrus.Infof("Running Mock Server on %s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
if err = r.Run(fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))); err != nil {
logrus.WithError(err).Fatal("Couldn't start router")
}
}
func main() {
// Initialize Cobra and Viper
cobra.OnInitialize(cmd.Initialize)
cmd.AddAllFlags(rootCmd)
rootCmd.AddCommand(cmd.VersionCmd)
// Run the command
if err := rootCmd.Execute(); err != nil {
logrus.WithError(err).Fatal("Couldn't start")
}
}