import sources from develop/0.1.0
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/tpeetz-kontor/kontor-go/pkg/infrastructure/config"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Application is our general purpose Application struct
|
||||
type Application struct {
|
||||
Server *http.Server
|
||||
Router *mux.Router
|
||||
Logger *zap.Logger
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
// Run will run the Application server
|
||||
func (app *Application) Run() {
|
||||
app.Logger.Info("App started...")
|
||||
err := app.Server.ListenAndServe()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// WaitForShutdown is a graceful way to handle server shutdown events
|
||||
func WaitForShutdown(application *Application) {
|
||||
// Create a channel to listen for OS signals
|
||||
interruptChan := make(chan os.Signal, 1)
|
||||
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Block until we receive a signal to our channel
|
||||
<-interruptChan
|
||||
|
||||
application.Logger.Info("Received shutdown signal, gracefully terminating")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
application.Server.Shutdown(ctx)
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
// AppConfig is the Application configuration struct
|
||||
type AppConfig struct {
|
||||
Name string
|
||||
Version string
|
||||
Token string
|
||||
}
|
||||
|
||||
// HTTPConfig is the Application HTTP configuration
|
||||
type HTTPConfig struct {
|
||||
Content string
|
||||
Problem string
|
||||
Port string
|
||||
}
|
||||
|
||||
// Config is the Configuration struct
|
||||
type Config struct {
|
||||
App AppConfig
|
||||
HTTP HTTPConfig
|
||||
}
|
||||
|
||||
// New returns a new Config Struct
|
||||
func New() *Config {
|
||||
return &Config{
|
||||
App: AppConfig{
|
||||
Name: env("APP_NAME", "Kontor"),
|
||||
Version: env("APP_VERSION", "v1.0"),
|
||||
Token: env("APP_TOKEN", ""),
|
||||
},
|
||||
HTTP: HTTPConfig{
|
||||
Content: env("HTTP_CONTENT_TYPE", "application/json"),
|
||||
Problem: env("HTTP_PROBLEM", "application/problem+json"),
|
||||
Port: env("HTTP_PORT", ":8086"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// env is a simple helper function to read an environment variable or return a default value
|
||||
func env(key string, defaultValue string) string {
|
||||
if value, exists := os.LookupEnv(key); exists {
|
||||
return value
|
||||
}
|
||||
|
||||
return defaultValue
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package kernel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
gohandlers "github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/tpeetz-kontor/kontor-go/pkg/infrastructure/app"
|
||||
"gitlab.com/tpeetz-kontor/kontor-go/pkg/infrastructure/config"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Boot the Application
|
||||
func Boot() *app.Application {
|
||||
// Configuration
|
||||
config := bootConfig()
|
||||
|
||||
// Router
|
||||
router := bootRouter()
|
||||
|
||||
// CORS
|
||||
corsHandler := gohandlers.CORS(gohandlers.AllowedOrigins([]string{"*"}))
|
||||
|
||||
// Logger
|
||||
logger := bootLogger()
|
||||
defer logger.Sync() // flushes buffer, if any
|
||||
|
||||
// Create and return and Application
|
||||
return &app.Application{
|
||||
Server: &http.Server{
|
||||
Addr: config.HTTP.Port,
|
||||
Handler: corsHandler(requestIDMiddleware(router)),
|
||||
IdleTimeout: 120 * time.Second,
|
||||
ReadTimeout: 1 * time.Second,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
},
|
||||
Router: router,
|
||||
Logger: logger,
|
||||
Config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func bootConfig() *config.Config {
|
||||
return config.New()
|
||||
}
|
||||
|
||||
func bootRouter() *mux.Router {
|
||||
return mux.NewRouter()
|
||||
}
|
||||
|
||||
func bootLogger() *zap.Logger {
|
||||
logger, logError := zap.NewProduction()
|
||||
if logError != nil {
|
||||
panic(logError)
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
// ContextKey is used for context.Context value. The value requires a key that is not primitive type.
|
||||
type ContextKey string
|
||||
|
||||
// ContextKeyRequestID is the ContextKey for RequestID
|
||||
const ContextKeyRequestID ContextKey = "requestID"
|
||||
|
||||
func requestIDMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
id := uuid.New()
|
||||
|
||||
ctx = context.WithValue(ctx, ContextKeyRequestID, id.String())
|
||||
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
bootLogger().Debug("Incoming request",
|
||||
zap.String("method", r.Method),
|
||||
zap.String("uri", r.RequestURI),
|
||||
zap.String("addr", r.RemoteAddr), zap.String("id", id.String()),
|
||||
)
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
bootLogger().Debug("Finished handling http req. %s",
|
||||
zap.String("id", id.String()))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response is a generic HTTP Response Struct
|
||||
type Response struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// Send a HTTP Response
|
||||
func Send(responseWriter http.ResponseWriter, code int, payload interface{}, contentType string) {
|
||||
response, _ := json.Marshal(payload)
|
||||
|
||||
responseWriter.Header().Set("Content-Type", contentType)
|
||||
responseWriter.WriteHeader(code)
|
||||
responseWriter.Write(response)
|
||||
}
|
||||
Reference in New Issue
Block a user