Skip to content
Snippets Groups Projects
Verified Commit d1787b8e authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

chore: commit save point

parent f4e7c0f9
Branches
No related tags found
No related merge requests found
package server
import (
"github.com/google/uuid"
)
type RequestUUID uuid.UUID
func NewRequestUUID() RequestUUID {
return RequestUUID(uuid.New())
}
func (this RequestUUID) UUID() uuid.UUID {
return uuid.UUID(this)
}
func (this RequestUUID) String() string {
return this.UUID().String()
}
package server
import (
"context"
"crypto/tls"
"embed"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"gitlab.schukai.com/oss/minerva/config"
"net/http"
"sync"
)
var (
server http.Server
//go:embed web/*
embeddedFiles embed.FS
)
func lookUp(path string) (content []byte) {
// Zuerst im Verzeichnis /web/asserts schauen (wird beim build inkludiert)
c, _ := embeddedFiles.ReadFile("web" + path)
if len(c) > 0 {
return c
}
return nil
}
func serveStaticFiles(next http.Handler) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
content := lookUp(r.URL.Path)
if len(content) > 0 {
w.Write(content)
return
}
next.ServeHTTP(w, r)
}
}
const waitGroupContextKey = "wait.group"
func waitGroupMiddleware() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var wg sync.WaitGroup
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), waitGroupContextKey, wg)))
wg.Wait()
})
}
}
func initRouting() *chi.Mux {
/** x509: certificate signed by unknown authority */
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
mux := chi.NewRouter()
mux.Use(waitGroupMiddleware())
mux.Use(RequestIDMiddleware)
mux.Use(middleware.RealIP)
mux.Use(middleware.Heartbeat("/ping"))
mux.Use(middleware.Compress(5))
mux.Use(middleware.AllowContentType("application/json", "text/html"))
mux.Use(loggerMiddleware)
// mux.Mount("/api/v1", v1.V1Api(cfg))
//mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
// if negotiator.New(r.Header).Type("text/html") == "text/html" {
// http.Redirect(w, r, "https://www.alvine.cloud/en/products/media-services/juno/", http.StatusPermanentRedirect)
// } else {
// error2.StandardErrorResponse(w, r,
// error2.ErrorResponse{
// Status: http.StatusNotFound,
// Hint: "did you want to call the juno-api? have a look at https://www.alvine.cloud/en/products/media-services/juno/documentation/",
// })
// }
//
//})
//
//p := cfg.Paths.Web
//zap.S().Info("use webpath " + p + "for static-file-server")
//
//if cfg.Server.Port == "" {
// zap.S().Error("missing server address")
// return nil
//}
//d := http.Dir(p)
//mux.Get("/*",
// serveStaticFiles(fileserver.FileServerWith404(d, func(w http.ResponseWriter, r *http.Request) (doDefaultFileServe bool) {
// error2.StandardErrorResponse(w, r, error2.ErrorResponse{
// Status: 404,
// })
// return false
//
// })))
return mux
}
func Serve() {
initLogger()
mux := initRouting()
c := config.GetConfiguration()
address := ":" + c.Server.Port
logInfo("server listen on address %s", address)
server := http.Server{
Handler: mux,
Addr: address,
}
if err := server.ListenAndServe(); err != nil {
logError("server error %s", err.Error())
}
logInfo("server quit")
}
package utils
import (
"github.com/gookit/color"
"os"
)
func Exit(code int) {
os.Exit(code)
}
var lastBlock string
func PrintErrorAndExit(message string, a ...interface{}) {
PrintError(message, a...)
os.Exit(1)
}
func PrintError(message string, a ...interface{}) {
if lastBlock == "error" {
color.Error.Println("\t"+message, a)
return
}
lastBlock = "error"
color.Error.Block(message, a...)
}
func PrintWarning(message string, a ...interface{}) {
if lastBlock == "warning" {
color.Warn.Println("\t"+message, a)
return
}
lastBlock = "warning"
color.Warn.Block(message, a...)
}
package utils
import "os"
func FileExists(name string) (found bool) {
if f, err := os.Stat(name); err == nil {
return f.Mode().IsRegular()
}
return false
}
func DirectoryExists(name string) (found bool) {
if f, err := os.Stat(name); err == nil {
return f.Mode().IsDir()
}
return false
}
package utils
import (
"github.com/go-playground/locales/de"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"os"
"os/exec"
"runtime"
"strings"
)
// only one instance as translators within are shared + goroutine safe
var (
universalTraslator *ut.UniversalTranslator
Translator ut.Translator
)
func setupI18n() {
e := en.New()
universalTraslator = ut.New(e, e, de.New())
lang, _ := initLocale()
var found bool
Translator, found = universalTraslator.GetTranslator(lang)
if !found {
}
}
func initLocale() (string, string) {
osHost := runtime.GOOS
defaultLang := "en"
defaultLoc := "US"
switch osHost {
case "windows":
// Exec powershell Get-Culture on Windows.
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
output, err := cmd.Output()
if err == nil {
langLocRaw := strings.TrimSpace(string(output))
langLoc := strings.Split(langLocRaw, "-")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
case "darwin":
// Exec powershell Get-Culture on MacOS.
cmd := exec.Command("sh", "osascript -e 'user locale of (get system info)'")
output, err := cmd.Output()
if err == nil {
langLocRaw := strings.TrimSpace(string(output))
langLoc := strings.Split(langLocRaw, "_")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
case "linux":
envlang, ok := os.LookupEnv("LANG")
if ok {
langLocRaw := strings.TrimSpace(envlang)
langLocRaw = strings.Split(envlang, ".")[0]
langLoc := strings.Split(langLocRaw, "_")
lang := langLoc[0]
loc := langLoc[1]
return lang, loc
}
}
return defaultLang, defaultLoc
}
package utils
func Setup() {
setupI18n()
}
package utils
import "fmt"
// -ldflags "-X main.version=$app_version -X main.build=$(due --iso-8601 | tr -d "-" )"
var (
version string
build string
)
func printVersion() {
s := ""
if version != "" {
s += version
}
if build != "" {
s += "(" + build + ")"
}
if s == "" {
s = "snapshot"
}
fmt.Println(s)
}
func IsSnapshot() bool {
if version == "" && build == "" {
return true
}
return false
}
func GetVersion() string {
return version
}
func GetBuild() string {
return build
}
func GetVersionString() string {
s := ""
if version != "" {
s += version
}
if build != "" {
s += "(" + build + ")"
}
if s == "" {
s = "snapshot"
}
return s
}
File added
File added
Server:
Port: 1212
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment