Skip to content

Commit 767feb7

Browse files
committed
feat(server): add server and read config
1 parent 503bc88 commit 767feb7

File tree

11 files changed

+523
-5
lines changed

11 files changed

+523
-5
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APP_NAME=go-project
2+
APP_HOST=0.0.0.0
3+
APP_PORT=8080

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
go-project
33
dist
44

5+
# Config file
6+
.env
7+
58
# Test binary, built with `go test -c`
69
*.test
710

.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ nfpms:
9191
dockers:
9292
- image_templates:
9393
- "zackijack/{{ .ProjectName }}:{{ .Tag }}"
94-
- "zackijack/{{ .ProjectName }}:v{{ .Major }}"
9594
- "zackijack/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}"
95+
- "zackijack/{{ .ProjectName }}:v{{ .Major }}"
9696
- "zackijack/{{ .ProjectName }}:latest"
9797
dockerfile: Dockerfile
9898
use_buildx: true

cmd/completion.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PowerShell:
5656
And source this file from your PowerShell profile.
5757
`
5858

59-
var errMsg = "There is something wrong with the completion generator"
59+
var errMsg = helpers.ErrMsg("completion generator")
6060

6161
var completionCmd = &cobra.Command{
6262
Use: "completion [bash|zsh|fish|powershell]",

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var rootCmd = &cobra.Command{
2020
// Execute adds all child commands to the root command and sets flags appropriately.
2121
// This is called by main.main(). It only needs to happen once to the rootCmd.
2222
func Execute() {
23-
helpers.CheckErr(rootCmd.Execute(), "There is something wrong with the application", true)
23+
helpers.CheckErr(rootCmd.Execute(), helpers.ErrMsg("application"), true)
2424
}
2525

2626
func init() {

cmd/start.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/gofiber/fiber/v2"
6+
zlog "github.com/rs/zerolog/log"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
"github.com/zackijack/go-project/internal/config"
10+
"github.com/zackijack/go-project/internal/helpers"
11+
)
12+
13+
const startDesc = `
14+
Start application on the HTTP-server.
15+
By default the HTTP-server will listen to host 0.0.0.0 & port 8080.
16+
However if you want to specify what host & port to use, just set via:
17+
18+
- argument:
19+
20+
$ go-project start --host localhost --port 80
21+
22+
- env variable:
23+
24+
$ export APP_HOST=localhost
25+
$ export APP_PORT=80
26+
`
27+
28+
var startCmd = &cobra.Command{
29+
Use: "start",
30+
Aliases: []string{"run", "serve", "server"},
31+
Short: "Start the application",
32+
Long: startDesc,
33+
Run: func(cmd *cobra.Command, args []string) {
34+
cfg, err := config.Load()
35+
if err != nil {
36+
zlog.Error().Err(err).Msg(helpers.ErrMsg("config"))
37+
}
38+
39+
start(cfg)
40+
},
41+
}
42+
43+
func init() {
44+
startCmd.Flags().StringP("host", "H", "0.0.0.0", "host address to serve the application on")
45+
startCmd.Flags().IntP("port", "P", 8080, "port to serve the application on")
46+
47+
// Bind to config.
48+
helpers.CheckErr(viper.BindPFlag("APP_HOST", startCmd.Flags().Lookup("host")), helpers.ErrMsg("config flag: host"), false)
49+
helpers.CheckErr(viper.BindPFlag("APP_PORT", startCmd.Flags().Lookup("port")), helpers.ErrMsg("config flag: port"), false)
50+
51+
rootCmd.AddCommand(startCmd)
52+
}
53+
54+
55+
func start(cfg *config.Config) {
56+
addr := fmt.Sprintf("%s:%d", cfg.AppHost, cfg.AppPort)
57+
58+
app := fiber.New()
59+
60+
// Get /johnny.
61+
app.Get("/:name", func(c *fiber.Ctx) error {
62+
msg := fmt.Sprintf("Hello, %s 👋! from %s with ❤️", c.Params("name"), cfg.AppName)
63+
return c.SendString(msg) // => Hello, johnny 👋! from go-project with ❤️
64+
})
65+
66+
zlog.Fatal().Err(app.Listen(addr)).Msg(helpers.ErrMsg("server"))
67+
}

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ module github.com/zackijack/go-project
33
go 1.16
44

55
require (
6+
github.com/andybalholm/brotli v1.0.3 // indirect
7+
github.com/gofiber/fiber/v2 v2.14.0
8+
github.com/klauspost/compress v1.13.1 // indirect
9+
github.com/mitchellh/go-homedir v1.1.0
610
github.com/rs/zerolog v1.23.0
711
github.com/spf13/cobra v1.1.3
12+
github.com/spf13/viper v1.8.1
13+
github.com/valyala/fasthttp v1.28.0 // indirect
14+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
815
)

go.sum

+393
Large diffs are not rendered by default.

internal/config/load.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package config
2+
3+
import (
4+
zlog "github.com/rs/zerolog/log"
5+
"github.com/spf13/viper"
6+
)
7+
8+
func Load() (c *Config, err error) {
9+
// Search env file in working directory with name ".env" if it exists.
10+
viper.AddConfigPath(".")
11+
viper.SetConfigName(".env")
12+
viper.SetConfigType("env")
13+
14+
viper.AutomaticEnv() // read in environment variables that match
15+
16+
// If a config file is found, read it in.
17+
if err := viper.ReadInConfig(); err == nil {
18+
zlog.Debug().Msg("Using config file: " + viper.ConfigFileUsed())
19+
}
20+
21+
err = viper.Unmarshal(&c)
22+
23+
return
24+
}

internal/config/model.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package config
2+
3+
import "github.com/spf13/viper"
4+
5+
type Config struct {
6+
AppName string `mapstructure:"APP_NAME"`
7+
AppHost string `mapstructure:"APP_HOST"`
8+
AppPort int `mapstructure:"APP_PORT"`
9+
}
10+
11+
func init() {
12+
// Set config default value.
13+
viper.SetDefault("APP_NAME", "go-project")
14+
viper.SetDefault("APP_HOST", "0.0.0.0")
15+
viper.SetDefault("APP_PORT", 8080)
16+
}

internal/helpers/error.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package helpers
22

33
import (
4-
"github.com/rs/zerolog/log"
4+
"fmt"
5+
zlog "github.com/rs/zerolog/log"
56
"os"
67
)
78

89
func CheckErr(err error, msg string, exit bool) {
910

1011
if err != nil {
11-
log.Error().Err(err).Msg(msg)
12+
zlog.Error().Err(err).Msg(msg)
1213

1314
if exit {
1415
os.Exit(1)
1516
}
1617
}
1718
}
19+
20+
func ErrMsg(s string) string {
21+
return fmt.Sprintf("There is something wrong with the %s", s)
22+
}

0 commit comments

Comments
 (0)