add go modules
This commit is contained in:
34
util/config.go
Normal file
34
util/config.go
Normal file
@ -0,0 +1,34 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Title string `json:"title"`
|
||||
FpsLimit int `json:"fpsLimit, omitempty" validate:"required"`
|
||||
Version string `json:"version"`
|
||||
MainWindowSizeX int `json:"sizeX"`
|
||||
MainWindowSizeY int `json:"sizeY"`
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
var configArg string = "./config.json"
|
||||
flag.StringVar(&configArg, "cfg", configArg, "config file location")
|
||||
flag.Parse()
|
||||
|
||||
// get config
|
||||
data, err := ioutil.ReadFile(configArg)
|
||||
if err != nil {
|
||||
log.Fatalf("Application couldn't read config at %s", configArg)
|
||||
}
|
||||
config := new(Config)
|
||||
err = json.Unmarshal(data, config)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
return config
|
||||
}
|
44
util/context.go
Normal file
44
util/context.go
Normal file
@ -0,0 +1,44 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
const (
|
||||
configKey = "config"
|
||||
loggerKey = "logger"
|
||||
)
|
||||
|
||||
type ClientCtx struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewClientContext(config *Config, logger *zerolog.Logger) ClientCtx {
|
||||
ctx := context.Context(context.TODO())
|
||||
ctx = context.WithValue(ctx, configKey, config)
|
||||
ctx = context.WithValue(ctx, loggerKey, logger)
|
||||
return ClientCtx{ctx: ctx}
|
||||
}
|
||||
|
||||
func (c *ClientCtx) Config() *Config {
|
||||
cfg, ok := c.ctx.Value(configKey).(*Config)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("no access to config from context"))
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (c *ClientCtx) Logger() *zerolog.Logger {
|
||||
logger, ok := c.ctx.Value(loggerKey).(*zerolog.Logger)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("no access to logger from context"))
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
//func FromContext(ctx context.Context) (*User, bool) {
|
||||
// u, ok := ctx.Value(userKey).(*User)
|
||||
// return u, ok
|
||||
//}
|
24
util/util.go
Normal file
24
util/util.go
Normal file
@ -0,0 +1,24 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func InArray(val interface{}, array interface{}) (exists bool, index int) {
|
||||
exists = false
|
||||
index = -1
|
||||
|
||||
switch reflect.TypeOf(array).Kind() {
|
||||
case reflect.Slice:
|
||||
s := reflect.ValueOf(array)
|
||||
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
|
||||
index = i
|
||||
exists = true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user