Skip to content

Commit

Permalink
clean: Cleanup code for Serving the http server
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Feb 14, 2022
1 parent 819363a commit 5eee30f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 17 deletions.
7 changes: 6 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ var (
Use: "serve",
Short: "Start the proxy server",
Run: func(cmd *cobra.Command, args []string) {
log.Fatal().Err(server.Serve(flagPort)).Msg("Error during server start")
if err := server.LoadConfig(); err != nil {
log.Error().Err(err).Msg("error loading config")
return
}

log.Fatal().Err(server.Serve(*flagPort)).Msg("Error during server start")
},
}
)
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ type ConfigProxy struct {

var config *Config

func loadConfig() error {
// LoadConfig loads the configuration from the config file
// It returns an error if the config file is not found or if the config is invalid
func LoadConfig() error {
var err error
if err = viper.Unmarshal(&config); err != nil {
return fmt.Errorf("error unmarshalling config")
Expand Down
6 changes: 1 addition & 5 deletions internal/pkg/server/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"os"
"reflect"
"testing"

Expand All @@ -10,9 +9,6 @@ import (
)

func init() {
if err := os.Chdir("../../.."); err != nil {
panic(err)
}
viper.SetConfigFile("tests/config.test.yaml")
if err := viper.ReadInConfig(); err != nil {
panic(err)
Expand Down Expand Up @@ -73,7 +69,7 @@ func Test_loadV1Config(t *testing.T) {
}

func TestGetConfig(t *testing.T) {
loadConfig() //nolint
LoadConfig() //nolint

tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func init() {
}

func TestProxyHandler(t *testing.T) {
loadConfig() // nolint
cachedTemplate = template.Must(template.ParseFiles("internal/pkg/server/templates/index.html"))
LoadConfig() // nolint
cachedTemplate = template.Must(template.ParseFiles("templates/index.html"))

// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
Expand Down
22 changes: 14 additions & 8 deletions internal/pkg/server/serve.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !skip

package server

import (
Expand All @@ -15,20 +17,24 @@ import (
var embededFiles embed.FS

// Serve the proxy server
func Serve(port *int) error {
if err := loadConfig(); err != nil {
return err
}

func Serve(port int) error {
fsys, err := fs.Sub(embededFiles, "templates/index.html")
if err != nil {
panic(err)
}

cachedTemplate = template.Must(template.ParseFS(fsys))
if !validPort(port) {
return fmt.Errorf("invalid port")
}

cachedTemplate = template.Must(template.ParseFS(fsys))
http.HandleFunc("/", proxyHandler)
log.Info().Msgf("Listening on port %d", port)
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}

log.Info().Msgf("Listening on port %d", *port)
return http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
// validPort returns true if the port is valid
// following the RFC https://datatracker.ietf.org/doc/html/rfc6056#section-2.1
func validPort(port int) bool {
return port > 0 && port < 65535
}
29 changes: 29 additions & 0 deletions internal/pkg/server/serve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !skip

package server

import (
_ "embed"
"testing"
)

func TestServe(t *testing.T) {
type args struct {
port int
}
tests := []struct {
name string
args args
wantErr bool
}{
{"invalid port", args{port: -1}, true},
{"invalid port", args{port: 1_000_000}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Serve(tt.args.port); (err != nil) != tt.wantErr {
t.Errorf("Serve() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
23 changes: 23 additions & 0 deletions internal/pkg/server/tests/config.test.valid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# API Version also used to protect against API or Schema changes
# Actually, the available API versions are: 1
apiVersion: 1
# List of your proxies
# You can add as many proxies as you want with logic:
# 1 proxy per final domain
proxies:
- # namespace is the domain name used for following entries
# This can be a subdomain, a domain or a full domain name
# subdomain.example.org, example.org or example.org/subdomain
namespace: atomys.lab
# All entries of this namespace will be proxied to the following address
# Key are the name and the entrypoint/path of your proxied packages
# Value is the current URL of your package. The Destination URL must
# end with a valid protocol.
# Allowed protocol are: "bzr", "fossil", "git", "hg", "svn".
entries:
# Will responds to the following URL: atomys.codes/go-proxy
go-proxy: https://github.com/42Atomys/go-proxy.git
# Will responds to the following URL: atomys.codes/dns-updater
dns-updater: https://github.com/42Atomys/dns-updater.git
# Will responds to the following URL: atomys.codes/subpath/gw2api-go
subpath/gw2api-go: https://gitlab.com/Atomys/gw2api-go.git
File renamed without changes.

0 comments on commit 5eee30f

Please sign in to comment.