-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ghanithan/2-create-http-server
feat: half way through with db and server
- Loading branch information
Showing
15 changed files
with
460 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -25,4 +25,4 @@ jobs: | |
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... | ||
run: go test -short -v ./... |
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 @@ | ||
/settings/default.yaml |
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,25 @@ | ||
package config | ||
|
||
type ConifgService interface { | ||
GetConfig(...string) *Config | ||
} | ||
|
||
type ( | ||
Config struct { | ||
Server ServerConfig `yaml: server` | ||
Db DbConfig `yaml: db` | ||
} | ||
|
||
ServerConfig struct { | ||
Host string `yaml: host` | ||
Port string `yaml: port` | ||
} | ||
|
||
DbConfig struct { | ||
Host string `yaml: host` | ||
Port string `yaml: port` | ||
Reponame string `yaml: reponame ` | ||
Username string `yaml: username` | ||
Password string `yaml: password` | ||
} | ||
) |
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,34 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func GetConfig(args ...string) (*Config, error) { | ||
//init config struct | ||
config := &Config{} | ||
|
||
// set default file path | ||
filePath := "../settings/sample.yaml" | ||
// collect the filepath from varidac arguments if provided | ||
if len(args) > 0 { | ||
filePath = args[0] | ||
} | ||
|
||
// read the file | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
// unmarshal the yaml file into conifg struct | ||
decoder := yaml.NewDecoder(file) | ||
if err := decoder.Decode(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} |
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,35 @@ | ||
package config | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetConfig(t *testing.T) { | ||
t.Run( | ||
"Testing getConfig", | ||
func(t *testing.T) { | ||
want := &Config{ | ||
Server: ServerConfig{ | ||
Host: "http://localhost", | ||
Port: "3000", | ||
}, | ||
Db: DbConfig{ | ||
Host: "localhost", | ||
Port: "8091", | ||
Reponame: "vizha", | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
} | ||
got, err := GetConfig() | ||
if err != nil { | ||
t.Fatalf("Error in fetching config: %s", err) | ||
} | ||
if !reflect.DeepEqual(want, got) { | ||
t.Fatalf("expected %q got %q", want, got) | ||
|
||
} | ||
}, | ||
) | ||
} |
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,66 @@ | ||
package database | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/couchbase/gocb/v2" | ||
"github.com/ghanithan/goBilling/config" | ||
) | ||
|
||
// Module for couchbase db wrapper | ||
|
||
type couchbaseDatabase struct { | ||
Db *gocb.Cluster | ||
} | ||
|
||
var ( | ||
once sync.Once | ||
dbInstance couchbaseDatabase | ||
) | ||
|
||
func InitCouchbaseDb(config *config.Config) couchbaseDatabase { | ||
once.Do(func() { | ||
options := gocb.ClusterOptions{ | ||
Authenticator: gocb.PasswordAuthenticator{ | ||
Username: config.Db.Username, | ||
Password: config.Db.Password, | ||
}, | ||
} | ||
|
||
// Sets a pre-configured profile called "wan-development" to help avoid latency issues | ||
// when accessing Capella from a different Wide Area Network | ||
// or Availability Zone (e.g. your laptop). | ||
if err := options.ApplyProfile(gocb.ClusterConfigProfileWanDevelopment); err != nil { | ||
log.Fatal(err, "Error in profile") | ||
} | ||
|
||
// Initialize the Connection | ||
cluster, err := gocb.Connect("couchbase://"+config.Db.Host, options) | ||
if err != nil { | ||
log.Fatal(err, " connecting cluster") | ||
} | ||
|
||
// bucket := cluster.Bucket(config.Db.Reponame) | ||
|
||
err = cluster.WaitUntilReady(5*time.Second, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
dbInstance = couchbaseDatabase{ | ||
Db: cluster, | ||
} | ||
}) | ||
return dbInstance | ||
} | ||
|
||
func (cb *couchbaseDatabase) GetDb() *gocb.Cluster { | ||
return cb.Db | ||
} | ||
|
||
func (cb *couchbaseDatabase) Close() { | ||
closeOpt := gocb.ClusterCloseOptions{} | ||
cb.Db.Close(&closeOpt) | ||
} |
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,72 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/couchbase/gocb/v2" | ||
"github.com/ghanithan/goBilling/config" | ||
) | ||
|
||
func TestInitCouchbaseDb(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode.") | ||
} | ||
config, err := config.GetConfig("../settings/default.yaml") | ||
if err != nil { | ||
t.Fatalf("Error in reading config: %s", err) | ||
} | ||
|
||
db := InitCouchbaseDb(config) | ||
|
||
cluster := db.GetDb() | ||
|
||
// col := bucket.Scope("_default").Collection("customers") | ||
|
||
// // Get the document back | ||
// getResult, err := col.Get("custid:C1", nil) | ||
// if err != nil { | ||
// t.Errorf("Error fetching: %s", err) | ||
// } | ||
|
||
qOpts := gocb.QueryOptions{} | ||
|
||
// create query | ||
queryStr := "select * from default:vizha._default.customers" | ||
|
||
fmt.Printf("query: %v\n", queryStr) | ||
|
||
rows, err := cluster.Query(queryStr, &qOpts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("rows: %v\n", rows) | ||
|
||
for rows.Next() { | ||
var intfc interface{} | ||
err = rows.Row(&intfc) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("interface result: %v\n", intfc) | ||
} | ||
|
||
// type UserType struct { | ||
// Id string `json:custid` | ||
// Name string `json: name` | ||
// Address struct { | ||
// Street string `json: street` | ||
// City string `json: city` | ||
// Zipcode string `json: zipcode` | ||
// } `json: address` | ||
// } | ||
|
||
// user := &UserType{} | ||
// err = getResult.Content(&user) | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// fmt.Printf("User: %v\n", user) | ||
|
||
} |
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,10 @@ | ||
package database | ||
|
||
type DbService interface { | ||
GetDb() any | ||
CloseDb() | ||
} | ||
|
||
type DbStub interface { | ||
FetchById(...string) any | ||
} |
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
Oops, something went wrong.