Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 593b962

Browse files
committed
refactored lots of code to sub-packages
1 parent 6754e9a commit 593b962

27 files changed

+430
-424
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
.setenv.sh
21
.env
32
mongodata/
4-
staticbackend
3+
cmd/staticbackend
54

65
# Binaries for programs and plugins
76
*.exe

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include .env
22
export $(shell sed 's/=.*//' .env)
33

44
build:
5-
@rm -rf staticbackend && go build
5+
@cd cmd && rm -rf staticbackend && go build -o staticbackend
66

77
start: build
88
@./staticbackend -host localhost

account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package staticbackend
22

33
import (
44
"context"
@@ -112,7 +112,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
112112
break
113113
}
114114

115-
base := middleware.BaseConfig{
115+
base := internal.BaseConfig{
116116
ID: primitive.NewObjectID(),
117117
SBID: acctID,
118118
Name: dbName,

cache.go renamed to cache/cache.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package main
1+
package cache
22

33
import (
44
"context"
55
"encoding/json"
66
"fmt"
77
"log"
8+
"os"
9+
"staticbackend/internal"
810
"time"
911

1012
"github.com/go-redis/redis/v8"
@@ -18,9 +20,9 @@ type Cache struct {
1820
// NewCache returns an initiated Redis client
1921
func NewCache() *Cache {
2022
rdb := redis.NewClient(&redis.Options{
21-
Addr: "localhost:6379",
22-
Password: "", // no password set
23-
DB: 0, // use default DB
23+
Addr: os.Getenv("REDIS_HOST"),
24+
Password: os.Getenv("REDIS_PASSWORD"),
25+
DB: 0, // use default DB
2426
})
2527

2628
return &Cache{
@@ -40,7 +42,7 @@ func (c *Cache) Set(key string, value string) error {
4042
return nil
4143
}
4244

43-
func (c *Cache) Subscribe(send chan Command, token, channel string, close chan bool) {
45+
func (c *Cache) Subscribe(send chan internal.Command, token, channel string, close chan bool) {
4446
pubsub := c.Rdb.Subscribe(c.Ctx, channel)
4547

4648
if _, err := pubsub.Receive(c.Ctx); err != nil {
@@ -53,7 +55,7 @@ func (c *Cache) Subscribe(send chan Command, token, channel string, close chan b
5355
for {
5456
select {
5557
case m := <-ch:
56-
var msg Command
58+
var msg internal.Command
5759
if err := json.Unmarshal([]byte(m.Payload), &msg); err != nil {
5860
log.Println("error parsing JSON message", err)
5961
_ = pubsub.Close()
@@ -62,8 +64,8 @@ func (c *Cache) Subscribe(send chan Command, token, channel string, close chan b
6264

6365
// for non DB events we change the type to MsgTypeChanOut
6466
if !msg.IsDBEvent() {
65-
msg.Type = MsgTypeChanOut
66-
} else if c.hasPermission(token, channel, msg.Data) == false {
67+
msg.Type = internal.MsgTypeChanOut
68+
} else if c.HasPermission(token, channel, msg.Data) == false {
6769
continue
6870
}
6971
send <- msg
@@ -74,7 +76,7 @@ func (c *Cache) Subscribe(send chan Command, token, channel string, close chan b
7476
}
7577
}
7678

77-
func (c *Cache) Publish(msg Command) error {
79+
func (c *Cache) Publish(msg internal.Command) error {
7880
b, err := json.Marshal(msg)
7981
if err != nil {
8082
return err
@@ -86,7 +88,7 @@ func (c *Cache) Publish(msg Command) error {
8688
return c.Rdb.Publish(ctx, msg.Channel, string(b)).Err()
8789
}
8890

89-
func (c *Cache) publishDocument(channel, typ string, v interface{}) {
91+
func (c *Cache) PublishDocument(channel, typ string, v interface{}) {
9092
subs, err := c.Rdb.PubSubNumSub(c.Ctx, channel).Result()
9193
if err != nil {
9294
fmt.Println("error getting db subscribers for ", channel)
@@ -107,7 +109,7 @@ func (c *Cache) publishDocument(channel, typ string, v interface{}) {
107109
return
108110
}
109111

110-
msg := Command{
112+
msg := internal.Command{
111113
Channel: channel,
112114
Data: string(b),
113115
Type: typ,
@@ -118,8 +120,8 @@ func (c *Cache) publishDocument(channel, typ string, v interface{}) {
118120
}
119121
}
120122

121-
func (c *Cache) hasPermission(token, repo, payload string) bool {
122-
me, ok := tokens[token]
123+
func (c *Cache) HasPermission(token, repo, payload string) bool {
124+
me, ok := internal.Tokens[token]
123125
if !ok {
124126
return false
125127
}
@@ -130,16 +132,16 @@ func (c *Cache) hasPermission(token, repo, payload string) bool {
130132
return false
131133
}
132134

133-
switch readPermission(repo) {
134-
case permGroup:
135-
acctID, ok := docs[fieldAccountID]
135+
switch internal.ReadPermission(repo) {
136+
case internal.PermGroup:
137+
acctID, ok := docs[internal.FieldAccountID]
136138
if !ok {
137139
return false
138140
}
139141

140142
return fmt.Sprintf("%v", acctID) == me.AccountID.Hex()
141-
case permOwner:
142-
owner, ok := docs[fieldOwnerID]
143+
case internal.PermOwner:
144+
owner, ok := docs[internal.FieldOwnerID]
143145
if !ok {
144146
return false
145147
}

cmd/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
backend "staticbackend"
5+
)
6+
7+
func main() {
8+
backend.Start()
9+
}

db.go

Lines changed: 22 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
package main
1+
package staticbackend
22

33
import (
44
"context"
55
"encoding/json"
66
"fmt"
77
"net/http"
88
"net/url"
9-
"regexp"
109
"staticbackend/internal"
1110
"staticbackend/middleware"
1211
"strconv"
1312
"strings"
1413
"time"
1514

15+
"staticbackend/cache"
16+
1617
"go.mongodb.org/mongo-driver/bson"
1718
"go.mongodb.org/mongo-driver/bson/primitive"
1819
"go.mongodb.org/mongo-driver/mongo"
@@ -21,7 +22,7 @@ import (
2122

2223
type Database struct {
2324
client *mongo.Client
24-
cache *Cache
25+
cache *cache.Cache
2526
}
2627

2728
func (database *Database) dbreq(w http.ResponseWriter, r *http.Request) {
@@ -91,7 +92,7 @@ func (database *Database) add(w http.ResponseWriter, r *http.Request) {
9192
doc["id"] = doc[internal.FieldID]
9293
delete(doc, internal.FieldID)
9394

94-
database.cache.publishDocument("db-"+col, MsgTypeDBCreated, doc)
95+
database.cache.PublishDocument("db-"+col, internal.MsgTypeDBCreated, doc)
9596

9697
respond(w, http.StatusCreated, doc)
9798
}
@@ -131,10 +132,10 @@ func (database *Database) list(w http.ResponseWriter, r *http.Request) {
131132

132133
// if they're not root
133134
if !strings.HasPrefix(col, "pub_") && auth.Role < 100 {
134-
switch readPermission(col) {
135-
case permGroup:
135+
switch internal.ReadPermission(col) {
136+
case internal.PermGroup:
136137
filter = bson.M{"accountId": auth.AccountID}
137-
case permOwner:
138+
case internal.PermOwner:
138139
filter = bson.M{internal.FieldAccountID: auth.AccountID, internal.FieldOwnerID: auth.UserID}
139140
}
140141
}
@@ -217,10 +218,10 @@ func (database *Database) get(w http.ResponseWriter, r *http.Request) {
217218

218219
// if they're not root and repo is not public
219220
if !strings.HasPrefix(col, "pub_") && auth.Role < 100 {
220-
switch readPermission(col) {
221-
case permGroup:
221+
switch internal.ReadPermission(col) {
222+
case internal.PermGroup:
222223
filter[internal.FieldAccountID] = auth.AccountID
223-
case permOwner:
224+
case internal.PermOwner:
224225
filter[internal.FieldAccountID] = auth.AccountID
225226
filter[internal.FieldOwnerID] = auth.UserID
226227
}
@@ -321,10 +322,10 @@ func (database *Database) query(w http.ResponseWriter, r *http.Request) {
321322

322323
// either not a public repo or not root
323324
if strings.HasPrefix(col, "pub_") == false && auth.Role < 100 {
324-
switch readPermission(col) {
325-
case permGroup:
325+
switch internal.ReadPermission(col) {
326+
case internal.PermGroup:
326327
filter[internal.FieldAccountID] = auth.AccountID
327-
case permOwner:
328+
case internal.PermOwner:
328329
filter[internal.FieldAccountID] = auth.AccountID
329330
filter[internal.FieldOwnerID] = auth.UserID
330331
}
@@ -439,10 +440,10 @@ func (database *Database) update(w http.ResponseWriter, r *http.Request) {
439440

440441
// if they are not "root", we use permission
441442
if auth.Role < 100 {
442-
switch writePermission(col) {
443-
case permGroup:
443+
switch internal.WritePermission(col) {
444+
case internal.PermGroup:
444445
filter[internal.FieldAccountID] = auth.AccountID
445-
case permOwner:
446+
case internal.PermOwner:
446447
filter[internal.FieldAccountID] = auth.AccountID
447448
filter[internal.FieldOwnerID] = auth.UserID
448449
}
@@ -475,7 +476,7 @@ func (database *Database) update(w http.ResponseWriter, r *http.Request) {
475476
result["id"] = result["_id"]
476477
delete(result, internal.FieldID)
477478

478-
database.cache.publishDocument("db-"+col, MsgTypeDBUpdated, result)
479+
database.cache.PublishDocument("db-"+col, internal.MsgTypeDBUpdated, result)
479480

480481
respond(w, http.StatusOK, result)
481482
}
@@ -505,10 +506,10 @@ func (database *Database) del(w http.ResponseWriter, r *http.Request) {
505506

506507
// if they're not root
507508
if auth.Role < 100 {
508-
switch writePermission(col) {
509-
case permGroup:
509+
switch internal.WritePermission(col) {
510+
case internal.PermGroup:
510511
filter[internal.FieldAccountID] = auth.AccountID
511-
case permOwner:
512+
case internal.PermOwner:
512513
filter[internal.FieldAccountID] = auth.AccountID
513514
filter[internal.FieldOwnerID] = auth.UserID
514515

@@ -521,7 +522,7 @@ func (database *Database) del(w http.ResponseWriter, r *http.Request) {
521522
return
522523
}
523524

524-
database.cache.publishDocument("db-"+col, MsgTypeDBDeleted, id)
525+
database.cache.PublishDocument("db-"+col, internal.MsgTypeDBDeleted, id)
525526
respond(w, http.StatusOK, res.DeletedCount)
526527
}
527528

@@ -578,77 +579,3 @@ func getPagination(u *url.URL) (page int64, size int64) {
578579

579580
return
580581
}
581-
582-
type permissionLevel int
583-
584-
const (
585-
permOwner permissionLevel = iota
586-
permGroup
587-
permEveryone
588-
)
589-
590-
func getPermission(col string) (owner string, group string, everyone string) {
591-
// default permission
592-
owner, group, everyone = "7", "4", "0"
593-
594-
re := regexp.MustCompile(`_\d\d\d_$`)
595-
if re.MatchString(col) == false {
596-
return
597-
}
598-
599-
results := re.FindAllString(col, -1)
600-
if len(results) != 1 {
601-
return
602-
}
603-
604-
perm := strings.Replace(results[0], "_", "", -1)
605-
606-
if len(perm) != 3 {
607-
return
608-
}
609-
610-
owner = string(perm[0])
611-
group = string(perm[1])
612-
everyone = string(perm[2])
613-
return
614-
}
615-
616-
func writePermission(col string) permissionLevel {
617-
_, g, e := getPermission(col)
618-
619-
if canWrite(e) {
620-
return permEveryone
621-
}
622-
if canWrite(g) {
623-
return permGroup
624-
}
625-
return permOwner
626-
}
627-
628-
func readPermission(col string) permissionLevel {
629-
_, g, e := getPermission(col)
630-
631-
if canRead(e) {
632-
return permEveryone
633-
}
634-
if canRead(g) {
635-
return permGroup
636-
}
637-
return permOwner
638-
}
639-
640-
func canWrite(s string) bool {
641-
i, err := strconv.Atoi(s)
642-
if err != nil {
643-
return false
644-
}
645-
return uint8(i)&uint8(2) != 0
646-
}
647-
648-
func canRead(s string) bool {
649-
i, err := strconv.Atoi(s)
650-
if err != nil {
651-
fmt.Println(err)
652-
}
653-
return uint8(i)&uint8(4) != 0
654-
}

db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package staticbackend
22

33
import (
44
"bytes"

form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package staticbackend
22

33
import (
44
"context"

0 commit comments

Comments
 (0)