Skip to content

Commit

Permalink
added initial version of commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mindhash committed Jul 1, 2018
0 parents commit 1b4a04d
Show file tree
Hide file tree
Showing 56 changed files with 2,234 additions and 0 deletions.
Binary file added pkg/darwin_amd64/cloud.google.com/go/storage.a
Binary file not shown.
Binary file not shown.
Binary file added pkg/darwin_amd64/github.com/golang/groupcache.a
Binary file not shown.
Binary file added pkg/darwin_amd64/github.com/gorilla/mux.a
Binary file not shown.
Binary file added pkg/darwin_amd64/github.com/lib/pq.a
Binary file not shown.
Binary file added pkg/darwin_amd64/github.com/satori/go.uuid.a
Binary file not shown.
Binary file added pkg/darwin_amd64/golang.org/x/net/context.a
Binary file not shown.
Binary file added pkg/darwin_amd64/golang.org/x/oauth2/google.a
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions src/cloud.google.com/go
Submodule go added at ae1e37
1 change: 1 addition & 0 deletions src/github.com/gogo/protobuf
Submodule protobuf added at 30cf7a
1 change: 1 addition & 0 deletions src/github.com/golang/groupcache
Submodule groupcache added at 24b096
1 change: 1 addition & 0 deletions src/github.com/golang/protobuf
Submodule protobuf added at 583188
1 change: 1 addition & 0 deletions src/github.com/googleapis/gax-go
Submodule gax-go added at 254b60
1 change: 1 addition & 0 deletions src/github.com/gorilla/mux
Submodule mux added at cb4698
1 change: 1 addition & 0 deletions src/github.com/lib/pq
Submodule pq added at 90697d
1 change: 1 addition & 0 deletions src/github.com/satori/go.uuid
Submodule go.uuid added at 36e9d2
1 change: 1 addition & 0 deletions src/go.opencensus.io
Submodule go.opencensus.io added at ff7de9
1 change: 1 addition & 0 deletions src/golang.org/x/net
Submodule net added at db08ff
1 change: 1 addition & 0 deletions src/golang.org/x/oauth2
Submodule oauth2 added at 1e0a3f
1 change: 1 addition & 0 deletions src/golang.org/x/text
Submodule text added at 5c1cf6
1 change: 1 addition & 0 deletions src/google.golang.org/api
Submodule api added at 2eea9b
1 change: 1 addition & 0 deletions src/google.golang.org/genproto
Submodule genproto added at 32ee49
1 change: 1 addition & 0 deletions src/google.golang.org/grpc
Submodule grpc added at 7e6dc3
Binary file added src/hyperview.in/server/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions src/hyperview.in/server/base/env.go
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)
}
28 changes: 28 additions & 0 deletions src/hyperview.in/server/base/errors.go
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()
}
51 changes: 51 additions & 0 deletions src/hyperview.in/server/base/expvar.go
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())
}
54 changes: 54 additions & 0 deletions src/hyperview.in/server/base/log_level.go
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]
}

38 changes: 38 additions & 0 deletions src/hyperview.in/server/base/logging.go
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)
}
160 changes: 160 additions & 0 deletions src/hyperview.in/server/core/db/crud.go
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()
}


Loading

0 comments on commit 1b4a04d

Please sign in to comment.