-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1b4a04d
Showing
56 changed files
with
2,234 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Submodule go
added at
ae1e37
Submodule protobuf
added at
30cf7a
Submodule groupcache
added at
24b096
Submodule protobuf
added at
583188
Submodule gax-go
added at
254b60
Submodule mux
added at
cb4698
Submodule pq
added at
90697d
Submodule go.uuid
added at
36e9d2
Submodule go.opencensus.io
added at
ff7de9
Submodule net
added at
db08ff
Submodule oauth2
added at
1e0a3f
Submodule text
added at
5c1cf6
Submodule api
added at
2eea9b
Submodule genproto
added at
32ee49
Submodule grpc
added at
7e6dc3
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package base | ||
|
||
|
||
import ( | ||
"os" | ||
) | ||
|
||
|
||
|
||
func SetEnv() { | ||
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "/Users/apple/MyProjects/creds/hw-storage-75d060e8419a.json") | ||
os.Setenv("GOOGLE_STORAGE_BUCKET", "hyperview001") | ||
} | ||
|
||
|
||
func GetEnv(name string) string{ | ||
return os.Getenv(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
|
||
type HTTPError struct { | ||
Status int | ||
Message string | ||
} | ||
|
||
func (err *HTTPError) Error() string { | ||
return fmt.Sprintf("%d %s", err.Status, err.Message) | ||
} | ||
|
||
func HTTPErrorf(status int, format string, args ...interface{}) *HTTPError { | ||
return &HTTPError{status, fmt.Sprintf(format, args...)} | ||
} | ||
|
||
func ErrorToHTTPStatus(err error) (int, string) { | ||
if err == nil { | ||
return 200, "OK" | ||
} | ||
|
||
return http.StatusInternalServerError, err.Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package base | ||
|
||
import ( | ||
"sync" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type IntMax struct { | ||
i int64 | ||
mu sync.RWMutex | ||
} | ||
|
||
func (v *IntMax) String() string { | ||
v.mu.RLock() | ||
defer v.mu.RUnlock() | ||
return strconv.FormatInt(v.i, 10) | ||
} | ||
|
||
func (v *IntMax) SetIfMax(value int64) { | ||
v.mu.Lock() | ||
defer v.mu.Unlock() | ||
if value > v.i { | ||
v.i = value | ||
} | ||
} | ||
|
||
|
||
type IntMeanVar struct { | ||
count int64 // number of values seen | ||
mean int64 // average value | ||
mu sync.RWMutex | ||
} | ||
|
||
func (v *IntMeanVar) String() string { | ||
v.mu.RLock() | ||
defer v.mu.RUnlock() | ||
return strconv.FormatInt(v.mean, 10) | ||
} | ||
|
||
// Adds value. Calculates new mean as iterative mean (avoids int overflow) | ||
func (v *IntMeanVar) AddValue(value int64) { | ||
v.mu.Lock() | ||
defer v.mu.Unlock() | ||
v.count++ | ||
v.mean = v.mean + int64((value-v.mean)/v.count) | ||
} | ||
|
||
func (v *IntMeanVar) AddSince(start time.Time) { | ||
v.AddValue(time.Since(start).Nanoseconds()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package base | ||
|
||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
) | ||
|
||
type LogLevel uint32 | ||
|
||
const ( | ||
LevelZero LogLevel = iota | ||
LevelError | ||
LevelWarn | ||
LevelInfo | ||
LevelDebug | ||
LevelTrace | ||
|
||
levelCount | ||
) | ||
|
||
|
||
var ( | ||
levelNames = []string{"off", "error", "warn", "info", "debug", "trace"} | ||
levelShortNames = []string{"OFF", "ERR", "WRN", "INF", "DBG", "TRC"} | ||
) | ||
|
||
/* why: use atomic operation so current processes will not be affected by update */ | ||
func (l *LogLevel) set(newLevel LogLevel) { | ||
atomic.StoreUint32((*uint32) (l), uint32(newLevel)) | ||
} | ||
|
||
/* what: check if log level is enabled */ | ||
func (l *LogLevel) Enabled(logLevel LogLevel) bool { | ||
if l == nil { | ||
return false | ||
} | ||
return atomic.LoadUint32((*uint32)(l)) >= uint32(logLevel) | ||
} | ||
|
||
func (l LogLevel) String() string { | ||
if l >= levelCount { | ||
return fmt.Sprintf("LogLevel(%d)", l) | ||
} | ||
return levelNames[l] | ||
} | ||
|
||
func (l LogLevel) StringShort() string { | ||
if l >= levelCount { | ||
return fmt.Sprintf("LVL(%d)", l) | ||
} | ||
return levelShortNames[l] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type LoggerConfig struct { | ||
logLevel int | ||
} | ||
|
||
type LogKey uint64 | ||
|
||
const DefaultLogLevel = 0 | ||
|
||
const ( | ||
infoLevel = iota | ||
debugLevel | ||
errorLevel | ||
) | ||
|
||
func Log(format string, args ...interface{}) { | ||
logTo(DefaultLogLevel, 0, format, args) | ||
} | ||
|
||
|
||
// TODO: log levels and writing to file | ||
func LogInfo(format string, args ...interface{}) { | ||
logTo(infoLevel, 0, format, args) | ||
} | ||
|
||
func logTo(logLevel LogLevel, logKey LogKey, format string, args ...interface{}) { | ||
fmt.Println(format, args) | ||
} | ||
|
||
|
||
func Errorf(format string, args ...interface{}) { | ||
fmt.Errorf(format, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"encoding/json" | ||
"hyperview.in/server/base" | ||
) | ||
|
||
//TODO: Add entity checks | ||
|
||
type Body map[string]interface{} | ||
|
||
|
||
func (d *DatabaseContext) Insert(key string, value interface{}) error{ | ||
tx, err := d.conn.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stmt, err := tx.Prepare("INSERT INTO ws_collections(key, value) VALUES ($1, $2)") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//defer stmt.Close() | ||
json_value, err := json.Marshal(value) | ||
if (err != nil) { | ||
return err | ||
} | ||
|
||
if _, err = stmt.Exec(key, json_value); err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} | ||
|
||
func (d *DatabaseContext) Upsert(key string, value interface{}) error{ | ||
|
||
if d.KeyExists(key) { | ||
return d.Update(key, value) | ||
} else { | ||
return d.Insert(key, value) | ||
} | ||
} | ||
|
||
func (d *DatabaseContext) DeleteIfExists(key string) error{ | ||
tx, err := d.conn.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stmt, err := tx.Prepare("DELETE FROM ws_collections WHERE key=$1") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err = stmt.Exec(key); err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} | ||
|
||
// TODO: throw error if rec doesnt exist | ||
|
||
func (d *DatabaseContext) Delete(key string) error{ | ||
tx, err := d.conn.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stmt, err := tx.Prepare("DELETE FROM ws_collections WHERE key=$1") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err = stmt.Exec(key); err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} | ||
|
||
func (d *DatabaseContext) KeyExists(key string) (bool) { | ||
var err error | ||
var data []byte | ||
|
||
rec, err := d.conn.Query("SELECT 1 FROM ws_collections WHERE key=$1", key) | ||
|
||
if err != nil { | ||
base.Log("Something wrong with database connection. Key Exists Error: %s", err) | ||
return false | ||
} | ||
|
||
defer rec.Close() | ||
|
||
for rec.Next() { | ||
err = rec.Scan(&data) | ||
} | ||
|
||
if data != nil { | ||
base.Log("File key exists. Updating file info") | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (d *DatabaseContext) Get(key string) ([]byte, error) { | ||
var data []byte | ||
var err error | ||
|
||
rows, err := d.conn.Query("SELECT value FROM ws_collections WHERE key=$1", key) | ||
if err != nil { | ||
fmt.Println("failed to retrive rows", err) | ||
return nil, err | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
err = rows.Scan(&data) | ||
} | ||
|
||
return data, err | ||
} | ||
|
||
|
||
// TODO: raises error if key is missing | ||
// | ||
func (d *DatabaseContext) Update(key string, value interface{}) error { | ||
tx, err := d.conn.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stmt, err := tx.Prepare("UPDATE ws_collections SET value=$1 WHERE key=$2") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//defer stmt.Close() | ||
json_value, err := json.Marshal(value) | ||
if (err != nil) { | ||
return err | ||
} | ||
|
||
if _, err = stmt.Exec(json_value, key); err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} | ||
|
||
|
Oops, something went wrong.