Moved configuration to the separate file
This commit is contained in:
parent
10edeb6e2d
commit
b0bb5bd922
6 changed files with 68 additions and 44 deletions
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
func sendAssetForCache(sessionID uint64, baseURL string, relativeURL string) {
|
||||
if fullURL, cacheable := assets.GetFullCachableURL(baseURL, relativeURL); cacheable {
|
||||
producer.Produce(TOPIC_CACHE, sessionID, messages.Encode(&messages.AssetCache{
|
||||
producer.Produce(cfg.TopicCache, sessionID, messages.Encode(&messages.AssetCache{
|
||||
URL: fullURL,
|
||||
}))
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ func sendAssetsForCacheFromCSS(sessionID uint64, baseURL string, css string) {
|
|||
}
|
||||
|
||||
func handleURL(sessionID uint64, baseURL string, url string) string {
|
||||
if CACHE_ASSESTS {
|
||||
if cfg.CacheAssets {
|
||||
sendAssetForCache(sessionID, baseURL, url)
|
||||
return rewriter.RewriteURL(sessionID, baseURL, url)
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ func handleURL(sessionID uint64, baseURL string, url string) string {
|
|||
}
|
||||
|
||||
func handleCSS(sessionID uint64, baseURL string, css string) string {
|
||||
if CACHE_ASSESTS {
|
||||
if cfg.CacheAssets {
|
||||
sendAssetsForCacheFromCSS(sessionID, baseURL, css)
|
||||
return rewriter.RewriteCSS(sessionID, baseURL, css)
|
||||
}
|
||||
|
|
|
|||
39
backend/services/http/config.go
Normal file
39
backend/services/http/config.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import "openreplay/backend/pkg/env"
|
||||
|
||||
type config struct {
|
||||
HTTPPort string
|
||||
TopicRawWeb string
|
||||
TopicRawIOS string
|
||||
TopicCache string
|
||||
CacheAssets bool
|
||||
BeaconSizeLimit int64
|
||||
JsonSizeLimit int64
|
||||
AssetsOrigin string
|
||||
AWSRegion string
|
||||
S3BucketIOSImages string
|
||||
TokenSecret string
|
||||
UAParserFile string
|
||||
MaxMinDBFile string
|
||||
WorkerID uint16
|
||||
}
|
||||
|
||||
func NewConfig() *config {
|
||||
return &config{
|
||||
HTTPPort: env.String("HTTP_PORT"),
|
||||
TopicRawWeb: env.String("TOPIC_RAW_WEB"),
|
||||
TopicRawIOS: env.String("TOPIC_RAW_IOS"),
|
||||
TopicCache: env.String("TOPIC_CACHE"),
|
||||
CacheAssets: env.Bool("CACHE_ASSETS"),
|
||||
BeaconSizeLimit: int64(env.Uint64("BEACON_SIZE_LIMIT")),
|
||||
JsonSizeLimit: 1e3, // 1Kb
|
||||
AssetsOrigin: env.String("ASSETS_ORIGIN"),
|
||||
AWSRegion: env.String("AWS_REGION"),
|
||||
S3BucketIOSImages: env.String("S3_BUCKET_IOS_IMAGES"),
|
||||
TokenSecret: env.String("TOKEN_SECRET"),
|
||||
UAParserFile: env.String("UAPARSER_FILE"),
|
||||
MaxMinDBFile: env.String("MAXMINDDB_FILE"),
|
||||
WorkerID: env.WorkerID(),
|
||||
}
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ func startSessionHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
startTime := time.Now()
|
||||
req := &request{}
|
||||
body := http.MaxBytesReader(w, r.Body, JSON_SIZE_LIMIT)
|
||||
body := http.MaxBytesReader(w, r.Body, cfg.JsonSizeLimit)
|
||||
defer body.Close()
|
||||
if err := json.NewDecoder(body).Decode(req); err != nil {
|
||||
responseWithError(w, http.StatusBadRequest, err)
|
||||
|
|
@ -84,7 +84,7 @@ func startSessionHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
|||
country := geoIP.ExtractISOCodeFromHTTPRequest(r)
|
||||
|
||||
// The difference with web is mostly here:
|
||||
producer.Produce(TOPIC_RAW_IOS, tokenData.ID, Encode(&IOSSessionStart{
|
||||
producer.Produce(cfg.TopicRawIOS, tokenData.ID, Encode(&IOSSessionStart{
|
||||
Timestamp: req.Timestamp,
|
||||
ProjectID: uint64(p.ProjectID),
|
||||
TrackerVersion: req.TrackerVersion,
|
||||
|
|
@ -102,7 +102,7 @@ func startSessionHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
|||
Token: tokenizer.Compose(*tokenData),
|
||||
UserUUID: userUUID,
|
||||
SessionID: strconv.FormatUint(tokenData.ID, 10),
|
||||
BeaconSizeLimit: BEACON_SIZE_LIMIT,
|
||||
BeaconSizeLimit: cfg.BeaconSizeLimit,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -112,7 +112,7 @@ func pushMessagesHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
|||
responseWithError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
pushMessages(w, r, sessionData.ID, TOPIC_RAW_IOS)
|
||||
pushMessages(w, r, sessionData.ID, cfg.TopicRawIOS)
|
||||
}
|
||||
|
||||
func pushLateMessagesHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -122,7 +122,7 @@ func pushLateMessagesHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
// Check timestamps here?
|
||||
pushMessages(w, r, sessionData.ID, TOPIC_RAW_IOS)
|
||||
pushMessages(w, r, sessionData.ID, cfg.TopicRawIOS)
|
||||
}
|
||||
|
||||
func imagesUploadHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ func startSessionHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
if r.Body == nil {
|
||||
responseWithError(w, http.StatusBadRequest, errors.New("request body is empty"))
|
||||
}
|
||||
body := http.MaxBytesReader(w, r.Body, JSON_SIZE_LIMIT)
|
||||
body := http.MaxBytesReader(w, r.Body, cfg.JsonSizeLimit)
|
||||
defer body.Close()
|
||||
|
||||
// Parse request body
|
||||
|
|
@ -71,7 +71,7 @@ func startSessionHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
expTime := startTime.Add(time.Duration(p.MaxSessionDuration) * time.Millisecond)
|
||||
tokenData = &token.TokenData{ID: sessionID, ExpTime: expTime.UnixNano() / 1e6}
|
||||
|
||||
producer.Produce(TOPIC_RAW_WEB, tokenData.ID, Encode(&SessionStart{
|
||||
producer.Produce(cfg.TopicRawWeb, tokenData.ID, Encode(&SessionStart{
|
||||
Timestamp: req.Timestamp,
|
||||
ProjectID: uint64(p.ProjectID),
|
||||
TrackerVersion: req.TrackerVersion,
|
||||
|
|
@ -95,7 +95,7 @@ func startSessionHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
Token: tokenizer.Compose(*tokenData),
|
||||
UserUUID: userUUID,
|
||||
SessionID: strconv.FormatUint(tokenData.ID, 10),
|
||||
BeaconSizeLimit: BEACON_SIZE_LIMIT,
|
||||
BeaconSizeLimit: cfg.BeaconSizeLimit,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -111,7 +111,7 @@ func pushMessagesHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
if r.Body == nil {
|
||||
responseWithError(w, http.StatusBadRequest, errors.New("request body is empty"))
|
||||
}
|
||||
body := http.MaxBytesReader(w, r.Body, BEACON_SIZE_LIMIT)
|
||||
body := http.MaxBytesReader(w, r.Body, cfg.BeaconSizeLimit)
|
||||
defer body.Close()
|
||||
|
||||
var handledMessages bytes.Buffer
|
||||
|
|
@ -153,7 +153,7 @@ func pushMessagesHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// Send processed messages to queue as array of bytes
|
||||
err = producer.Produce(TOPIC_RAW_WEB, sessionData.ID, handledMessages.Bytes())
|
||||
err = producer.Produce(cfg.TopicRawWeb, sessionData.ID, handledMessages.Bytes())
|
||||
if err != nil {
|
||||
log.Printf("can't send processed messages to queue: %s", err)
|
||||
}
|
||||
|
|
@ -166,7 +166,7 @@ func notStartedHandlerWeb(w http.ResponseWriter, r *http.Request) {
|
|||
if r.Body == nil {
|
||||
responseWithError(w, http.StatusBadRequest, errors.New("request body is empty"))
|
||||
}
|
||||
body := http.MaxBytesReader(w, r.Body, JSON_SIZE_LIMIT)
|
||||
body := http.MaxBytesReader(w, r.Body, cfg.JsonSizeLimit)
|
||||
defer body.Close()
|
||||
|
||||
// Parse request body
|
||||
|
|
|
|||
|
|
@ -9,10 +9,8 @@ import (
|
|||
gzip "github.com/klauspost/pgzip"
|
||||
)
|
||||
|
||||
const JSON_SIZE_LIMIT int64 = 1e3 // 1Kb
|
||||
|
||||
func pushMessages(w http.ResponseWriter, r *http.Request, sessionID uint64, topicName string) {
|
||||
body := http.MaxBytesReader(w, r.Body, BEACON_SIZE_LIMIT)
|
||||
body := http.MaxBytesReader(w, r.Body, cfg.BeaconSizeLimit)
|
||||
defer body.Close()
|
||||
var reader io.ReadCloser
|
||||
var err error
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ import (
|
|||
"openreplay/backend/pkg/pprof"
|
||||
)
|
||||
|
||||
// Global variables
|
||||
var cfg *config
|
||||
var rewriter *assets.Rewriter
|
||||
var producer types.Producer
|
||||
var pgconn *cache.PGCache
|
||||
|
|
@ -34,19 +36,13 @@ var geoIP *geoip.GeoIP
|
|||
var tokenizer *token.Tokenizer
|
||||
var s3 *storage.S3
|
||||
|
||||
var TOPIC_RAW_WEB string
|
||||
var TOPIC_RAW_IOS string
|
||||
var TOPIC_CACHE string
|
||||
var TOPIC_TRIGGER string
|
||||
|
||||
//var TOPIC_ANALYTICS string
|
||||
var CACHE_ASSESTS bool
|
||||
var BEACON_SIZE_LIMIT int64
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.LstdFlags | log.LUTC | log.Llongfile)
|
||||
pprof.StartProfilingServer()
|
||||
|
||||
// Configs
|
||||
cfg = NewConfig()
|
||||
|
||||
// Queue
|
||||
producer = queue.NewProducer()
|
||||
defer producer.Close(15000)
|
||||
|
|
@ -55,26 +51,17 @@ func main() {
|
|||
pgconn = cache.NewPGCache(postgres.NewConn(env.String("POSTGRES_STRING")), 1000*60*20)
|
||||
defer pgconn.Close()
|
||||
|
||||
// Envs
|
||||
TOPIC_RAW_WEB = env.String("TOPIC_RAW_WEB")
|
||||
TOPIC_RAW_IOS = env.String("TOPIC_RAW_IOS")
|
||||
TOPIC_CACHE = env.String("TOPIC_CACHE")
|
||||
TOPIC_TRIGGER = env.String("TOPIC_TRIGGER")
|
||||
CACHE_ASSESTS = env.Bool("CACHE_ASSETS")
|
||||
BEACON_SIZE_LIMIT = int64(env.Uint64("BEACON_SIZE_LIMIT"))
|
||||
HTTP_PORT := env.String("HTTP_PORT")
|
||||
|
||||
// Modules
|
||||
rewriter = assets.NewRewriter(env.String("ASSETS_ORIGIN"))
|
||||
s3 = storage.NewS3(env.String("AWS_REGION"), env.String("S3_BUCKET_IOS_IMAGES"))
|
||||
tokenizer = token.NewTokenizer(env.String("TOKEN_SECRET"))
|
||||
uaParser = uaparser.NewUAParser(env.String("UAPARSER_FILE"))
|
||||
geoIP = geoip.NewGeoIP(env.String("MAXMINDDB_FILE"))
|
||||
flaker = flakeid.NewFlaker(env.WorkerID())
|
||||
// Init modules
|
||||
rewriter = assets.NewRewriter(cfg.AssetsOrigin)
|
||||
s3 = storage.NewS3(cfg.AWSRegion, cfg.S3BucketIOSImages)
|
||||
tokenizer = token.NewTokenizer(cfg.TokenSecret)
|
||||
uaParser = uaparser.NewUAParser(cfg.UAParserFile)
|
||||
geoIP = geoip.NewGeoIP(cfg.MaxMinDBFile)
|
||||
flaker = flakeid.NewFlaker(cfg.WorkerID)
|
||||
|
||||
// Server
|
||||
server := &http.Server{
|
||||
Addr: ":" + HTTP_PORT,
|
||||
Addr: ":" + cfg.HTTPPort,
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// TODO: agree with specification
|
||||
|
|
@ -154,7 +141,7 @@ func main() {
|
|||
log.Fatal("Server error")
|
||||
}
|
||||
}()
|
||||
log.Printf("Server successfully started on port %v\n", HTTP_PORT)
|
||||
log.Printf("Server successfully started on port %v\n", cfg.HTTPPort)
|
||||
|
||||
sigchan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue