Skip to content
Snippets Groups Projects
Commit 447827fc authored by Lucendio's avatar Lucendio
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
.local
Makefile 0 → 100644
SHELL := /usr/bin/env bash -euo pipefail
.DEFAULT_GOAL := default
MKFILE_DIR = $(abspath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
LOCAL_DIR := $(abspath $(MKFILE_DIR)/.local)
BIN_DIR := $(LOCAL_DIR)/bin
TMP_DIR := $(LOCAL_DIR)/tmp
SRC_DIR := $(MKFILE_DIR)
export PATH := $(BIN_DIR):$(PATH)
export GOMODCACHE = $(LOCAL_DIR)/cache/go
export GOTMPDIR = $(TMP_DIR)/go
default: clean install build run
.PHONY: install
install:
mkdir -p $(GOTMPDIR)
cd $(SRC_DIR) \
&& go get -t
.PHONY: run
run:
cd $(SRC_DIR) \
&& go run .
.PHONY: build $(BIN_DIR)/artifact.bin
build: $(BIN_DIR)/artifact.bin
$(BIN_DIR)/artifact.bin:
cd $(SRC_DIR) \
&& go build \
-o $(@) \
$(SRC_DIR)/*.go
.PHONY: test
.SILENT: test
test:
cd $(SRC_DIR) \
&& go test \
-race \
-v \
$(SRC_DIR)/...
.PHONY: exec
exec:
chmod +x $(BIN_DIR)/artifact.bin
exec $(BIN_DIR)/artifact.bin
.PHONY: clean
clean:
go clean -modcache
rm -rf \
$(LOCAL_DIR)
package configuration
import (
"errors"
"fmt"
configParser "github.com/caarlos0/env/v9"
)
type Config struct {
Environment string `env:"ENV_NAME" envDefault:"development"`
Host string `env:"HOST" envDefault:"127.0.0.1"`
Port int16 `env:"PORT" envDefault:"3000"`
}
func New() ( *Config, error ){
cfg := Config{}
if err := configParser.Parse( &cfg ); err != nil {
return nil, err
}
possibleEnvValues := map[ string ] bool {
"development": true,
"testing": true,
"production": true,
}
if _, ok := possibleEnvValues[ cfg.Environment ]; !ok {
return nil, errors.New(
fmt.Sprintf( "Invalid environment value: %s", cfg.Environment ),
)
}
return &cfg, nil
}
go.mod 0 → 100644
module webservice
go 1.21
require (
github.com/caarlos0/env/v9 v9.0.0
github.com/gofiber/fiber/v2 v2.49.2
github.com/stretchr/testify v1.8.4
)
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.49.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
go.sum 0 → 100644
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs=
github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE=
github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
main.go 0 → 100644
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"webservice/configuration"
"webservice/routing"
"github.com/gofiber/fiber/v2"
)
var isHealthy = false
func main() {
config, err := configuration.New()
if err != nil {
log.Fatalf( "HTTP server failed to start: %v", err )
}
server := fiber.New( fiber.Config{
AppName: "webservice",
DisableStartupMessage: config.Environment != "development",
})
routing.SetRoutes( server )
go func(){
err := server.Listen( fmt.Sprintf( "%s:%d", config.Host, config.Port ) )
if err != nil {
log.Fatalf( "HTTP server failed to start: %v", err )
}
}()
osSignaling := make( chan os.Signal, 1 )
signal.Notify( osSignaling, syscall.SIGHUP )
signal.Notify( osSignaling, syscall.SIGINT )
signal.Notify( osSignaling, syscall.SIGTERM )
signal.Notify( osSignaling, syscall.SIGQUIT )
shuttingDown := context.TODO()
isHealthy = true
if config.Environment != "development" {
log.Println( "HTTP server started successfully" )
}
for {
select {
case <-osSignaling:
isHealthy = false
log.Println( "Gracefully shutting down HTTP server" )
var concludeShutdown context.CancelFunc
shuttingDown, concludeShutdown = context.WithTimeout(
context.Background(),
time.Second * 15,
)
err := server.ShutdownWithContext( shuttingDown )
if err != nil {
log.Printf( "HTTP server failed to shut down: %v", err )
}
concludeShutdown()
case <-shuttingDown.Done():
return
}
}
}
package routing
import (
f "github.com/gofiber/fiber/v2"
)
func SetRoutes( router *f.App ){
router.Get( "/", func( c *f.Ctx ) error {
return c.SendString( "Hello World!" )
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment