feat: Implement config parsing and start a server based on config

master
Luther Wen Xu 2020-11-30 22:15:27 +07:00
parent 7e55bad275
commit 2225bc34e4
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 92 additions and 3 deletions

1
.gitignore vendored

@ -0,0 +1 @@
config.json

@ -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
}

@ -2,4 +2,7 @@ module gitea.teamortix.com/Team-Ortix/blgo
go 1.15 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
)

@ -1,10 +1,17 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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.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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/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 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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/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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -1,9 +1,31 @@
package main package main
import ( import (
"fmt" "net/http"
"strconv"
"github.com/sirupsen/logrus"
) )
func main() { 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)
}
} }