From 2225bc34e49d23450e546cb3faec33db164f2a41 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Mon, 30 Nov 2020 22:15:27 +0800 Subject: [PATCH] feat: Implement config parsing and start a server based on config --- .gitignore | 1 + config.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++++- go.sum | 7 +++++++ main.go | 26 +++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 config.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/config.go b/config.go new file mode 100644 index 0000000..399d848 --- /dev/null +++ b/config.go @@ -0,0 +1,56 @@ +package main + +import ( + "encoding/json" + "errors" + "os" +) + +const configPath = "config.json" + +type config struct { + Port int `json:"port"` + ThemeName string `json:"theme"` +} + +// createDefaultConfig checks if a config file is present. If not, it creates the default config file. +// It returns true if a file is created. +func createDefaultConfig() (bool, error) { + info, err := os.Stat(configPath) + if err == nil { + if info.IsDir() { + return false, errors.New("config.json is a directory") + } + return false, nil + } + if !os.IsNotExist(err) { + return false, err + } + + // Create the file. + f, err := os.Create(configPath) + if err != nil { + return false, err + } + encoder := json.NewEncoder(f) + encoder.SetIndent("", "\t") + err = encoder.Encode(config{ + Port: 80, + ThemeName: "default", + }) + return true, err +} + +// parseConfig parses the config file into a struct +func parseConfig() (config, error) { + // Open the file. + f, err := os.Open(configPath) + if err != nil { + return config{}, err + } + defer f.Close() + // Decode it into the config struct. + var cfg config + err = json.NewDecoder(f).Decode(&cfg) + return cfg, err +} diff --git a/go.mod b/go.mod index 346a264..f73b071 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module gitea.teamortix.com/Team-Ortix/blgo go 1.15 -require github.com/stretchr/testify v1.6.1 +require ( + github.com/sirupsen/logrus v1.7.0 + github.com/stretchr/testify v1.6.1 +) diff --git a/go.sum b/go.sum index 56d62e7..df0cd57 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,17 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index c04328c..f003a4b 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,31 @@ package main import ( - "fmt" + "net/http" + "strconv" + + "github.com/sirupsen/logrus" ) func main() { - fmt.Print("zoop") + created, err := createDefaultConfig() + if err != nil { + logrus.Fatalf("error while reading config file: %v", err) + } + if created { + logrus.Info("The default config file has been created. Please edit it and re-run the program.") + return + } + + cfg, err := parseConfig() + if err != nil { + logrus.Fatalf("error while parsing the config: %v", err) + } + + portStr := strconv.Itoa(cfg.Port) + logrus.Info("Listening on port " + portStr) + err = http.ListenAndServe(":"+portStr, nil) + if err != nil { + logrus.Fatalf("error while listen and serving: %v", err) + } }