Compare commits

...

3 Commits

Author SHA1 Message Date
Chakib Benziane 8808bc69f3 use json marshalling 5 years ago
Chakib Benziane aa892eca4c Working redis with encoding 5 years ago
Chakib Benziane 8dce666783 use docker, adding redis 5 years ago

@ -1,11 +0,0 @@
FROM golang:alpine
RUN mkdir -p /src
WORKDIR /src
ENV GO111MODULE=on
COPY . .
RUN go build
CMD ["bit4sat"]

@ -1,7 +1,7 @@
package main
import (
"git.sp4ke.com/sp4ke/bit4sat-server.git/storage"
"git.sp4ke.com/sp4ke/bit4sat/storage"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/home/spike/projects/bit4sat/server/db-storage/bit4sat.sqlite" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="1"/><attached/><window><main_tabs open="structure browser query pragmas" current="1"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="2105"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="upload"/><default_encoding codec=""/><browse_table_settings><table schema="" name="" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><conditional_formats/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths><column index="1" value="275"/><column index="2" value="176"/><column index="3" value="256"/></column_widths><filter_values/><conditional_formats/><display_formats><column index="7" value=""/></display_formats><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload_status" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><conditional_formats/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload_with_status" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths><column index="7" value="0"/><column index="8" value="0"/></column_widths><filter_values/><conditional_formats/><display_formats/><hidden_columns><column index="7" value="1"/><column index="8" value="1"/></hidden_columns><plot_y_axes/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1">SELECT EXISTS (SELECT upload_id FROM upload WHERE upload_id = '1Ijbco3pYIrl2VzTslERxszEHG1');</sql><sql name="SQL 2">SELECT * FROM upload JOIN upload_status ON upload.status = upload_status.type;</sql><current_tab id="1"/></tab_sql></sqlb_project>
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/home/spike/projects/bit4sat/db-storage/bit4sat.sqlite" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="1"/><attached/><window><main_tabs open="structure browser query pragmas" current="1"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="2105"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="upload"/><default_encoding codec=""/><browse_table_settings><table schema="" name="" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><conditional_formats/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths><column index="1" value="275"/><column index="2" value="176"/><column index="3" value="256"/></column_widths><filter_values/><conditional_formats/><display_formats><column index="7" value=""/></display_formats><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload_status" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><conditional_formats/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="upload_with_status" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths><column index="7" value="0"/><column index="8" value="0"/></column_widths><filter_values/><conditional_formats/><display_formats/><hidden_columns><column index="7" value="1"/><column index="8" value="1"/></hidden_columns><plot_y_axes/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1">SELECT EXISTS (SELECT upload_id FROM upload WHERE upload_id = '1Ijbco3pYIrl2VzTslERxszEHG1');</sql><sql name="SQL 2">SELECT * FROM upload JOIN upload_status ON upload.status = upload_status.type;</sql><current_tab id="1"/></tab_sql></sqlb_project>

@ -3,84 +3,50 @@ package db
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/mediocregopher/radix/v3"
"github.com/mediocregopher/radix/v3/resp/resp2"
)
const (
DBName = "bit4sat.sqlite"
DBPath = "db-storage"
DBPathEnv = "BIT4SAT_DB_PATH"
DBPragma = ` PRAGMA foreign_keys = ON; `
RedisHost = "redis"
RedisHostEnv = "REDIS_HOST"
RedisPort = 6379
)
var (
DBOptions = map[string]string{
"_journal_mode": "WAL",
}
DB *Database
RuntimeRedisHost string
DB *radix.Pool
)
type Database struct {
Handle *sqlx.DB
}
func (d *Database) Open() error {
dsnOptions := &url.Values{}
for k, v := range DBOptions {
dsnOptions.Set(k, v)
}
// Get db base path
path, set := os.LookupEnv(DBPathEnv)
if !set {
path = DBPath
}
func RedisMarshal(v interface{}) string {
// Create path if not exists
err := utils.Mkdir(path)
if err != nil {
log.Fatal(err)
data := resp2.Any{
I: v,
MarshalBulkString: true,
MarshalNoArrayHeaders: true,
}
marshalled := &strings.Builder{}
data.MarshalRESP(marshalled)
path = filepath.Join(path, DBName)
//path = fmt.Sprintf("%s/%s", path, DBName)
dsn := fmt.Sprintf("file:%s?%s", path, dsnOptions.Encode())
return marshalled.String()
}
log.Printf("Opening sqlite db %s\n", dsn)
func init() {
var err error
d.Handle, err = sqlx.Open("sqlite3", dsn)
if err != nil {
log.Fatal(err)
RuntimeRedisHost, set := os.LookupEnv(RedisHostEnv)
if !set {
RuntimeRedisHost = RedisHost
}
// Execute Pragmas
d.Handle.MustExec(DBPragma)
return nil
}
func RollbackTx(tx *sqlx.Tx, callback func()) {
log.Println("Rolling back transactions !")
err := tx.Rollback()
redisLoc := fmt.Sprintf("%s:%s", RuntimeRedisHost, strconv.Itoa(RedisPort))
DB, err = radix.NewPool("tcp", redisLoc, 10)
if err != nil {
log.Fatal(err)
}
if callback != nil {
callback()
}
}
func init() {
DB = &Database{}
DB.Open()
}

@ -2,27 +2,40 @@ version: "3.4"
volumes:
redis:
name: bit4sat-data
file-storage:
#sqlite:
gocache:
services:
bit4sat:
image: sp4ke/bit4sat
build:
context: .
build: ./docker
environment:
- GO111MODULE=on
#- BIT4SAT_DB_PATH=/sqlite
- BIT4SAT_STORAGE_PATH=/storage
- GOPATH=/go
deploy:
replicas: 1
#deploy:
#replicas: 1
#
ports:
- "8880:8880"
volumes:
- $PWD:/src
- gocache:/go
- ./db-storage:/sqlite
- file-storage:/storage
working_dir: /src
command:
- go
- "run *.go"
redis:
image: redis:alpine
volumes:
- redis:/data
command:
- redis-server
- --appendonly yes

@ -0,0 +1,16 @@
FROM golang:alpine
RUN apk update && apk add --no-cache \
git \
bash \
gcc build-base make
RUN mkdir -p /src
RUN mkdir -p /sqlite
WORKDIR /src
ENV GO111MODULE=on
ADD entrypoint.sh /entry
RUN chmod +x /entry
CMD ["/entry"]

@ -0,0 +1,5 @@
#!/bin/bash
set -e
go build -v
exec ./bit4sat

@ -0,0 +1,23 @@
package encoding
import (
"bytes"
"encoding/gob"
)
func ToBytes(v interface{}) ([]byte, error) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func FromBytes(v []byte, dst interface{}) error {
buffer := bytes.NewBuffer(v)
dec := gob.NewDecoder(buffer)
return dec.Decode(dst)
}

@ -0,0 +1,5 @@
package encoding
func ToJson(v interface) ([]byte, error) {
enc := json.NewEncoder()
}

@ -3,19 +3,12 @@ module git.sp4ke.com/sp4ke/bit4sat
go 1.12
require (
github.com/fatih/structs v1.1.0
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect
github.com/gin-gonic/gin v1.3.0
github.com/golang/protobuf v1.3.1 // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jmoiron/sqlx v1.2.0
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mattn/go-sqlite3 v1.10.0
github.com/mediocregopher/radix/v3 v3.2.3
github.com/segmentio/ksuid v1.0.2
github.com/stretchr/testify v1.3.0 // indirect
github.com/ugorji/go/codec v0.0.0-20190315113641-a70535d8491c // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 // indirect
google.golang.org/appengine v1.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

@ -1,71 +1,41 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a h1:zBycVvXa03SIX+jdMv8wGu9TMDMWdN8EhaR1FoeKHNo=
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a/go.mod h1:pL2kNE+DgDU+eQ+dary5bX0Z6LPP8nR6Mqs1iejILw4=
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY=
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg=
github.com/mediocregopher/radix/v3 v3.2.3 h1:TbcGCZdo9zfPYPgevsqRn+OjvCyfOK6TzuXhqzWdCt0=
github.com/mediocregopher/radix/v3 v3.2.3/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/ksuid v1.0.2 h1:9yBfKyw4ECGTdALaF09Snw3sLJmYIX6AbPJrAy6MrDc=
github.com/segmentio/ksuid v1.0.2/go.mod h1:BXuJDr2byAiHuQaQtSKoXh1J0YmUDurywOXgB2w+OSU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go v1.1.2 h1:JON3E2/GPW2iDNGoSAusl1KDf5TRQ8k8q7Tp097pZGs=
github.com/ugorji/go v1.1.2/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4=
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v0.0.0-20190315113641-a70535d8491c h1:20Yyqg0mvFOyG3m7ejVqHEa03CBa3hTttx9jiYLkjYU=
github.com/ugorji/go/codec v0.0.0-20190315113641-a70535d8491c/go.mod h1:iT03XoTwV7xq/+UGwKO3UbC1nNNlopQiY61beSdrtOA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 h1:kcXqo9vE6fsZY5X5Rd7R1l7fTgnWaDCVmln65REefiE=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 h1:xe1/2UUJRmA9iDglQSlkx8c5n3twv58+K0mPpC2zmhA=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=

@ -1,11 +1,10 @@
package main
import "git.sp4ke.com/sp4ke/bit4sat-server.git/db"
import "git.sp4ke.com/sp4ke/bit4sat/db"
func main() {
defer db.DB.Handle.Close()
defer db.DB.Close()
api := NewAPI()
api.Run()
}

@ -9,7 +9,7 @@ import (
"os"
"path/filepath"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils"
"git.sp4ke.com/sp4ke/bit4sat/utils"
)
const (
@ -25,7 +25,7 @@ func GetStoreDestination(filename string) string {
return filepath.Join(RuntimeStoragePath, filename)
}
func StoreFormFile(src multipart.File, filename string) error {
func storeFormFile(src multipart.File, filename string) error {
filePath := GetStoreDestination(filename)
// If file exists just return
exists, err := CheckFileExists(filePath)
@ -67,6 +67,15 @@ func CheckFileExists(file string) (bool, error) {
return false, err
}
func removeFiles(paths []string) {
for _, path := range paths {
err := os.Remove(path)
if err != nil {
log.Fatal(err)
}
}
}
func init() {
path, set := os.LookupEnv(StoragePathEnv)
if !set {

@ -7,10 +7,8 @@ import (
"io"
"log"
"net/http"
"os"
"git.sp4ke.com/sp4ke/bit4sat-server.git/db"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils"
"git.sp4ke.com/sp4ke/bit4sat/utils"
"github.com/gin-gonic/gin"
"github.com/segmentio/ksuid"
)
@ -28,39 +26,27 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
// Create unique id
id := ksuid.New()
tx, err := db.DB.Handle.Beginx()
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
up := NewUpload()
up.ID = id.String()
up.Status = UpNew
for _, file := range uploadForm.Files {
up := &Upload{}
up.ID = id.String()
upfile := &UpFile{}
log.Println(file.Name)
up.FileName, up.FileExt = utils.CleanFileName(file.Name)
upfile.FileName, upfile.FileExt = utils.CleanFileName(file.Name)
upfile.FileSize = file.Size
upfile.FileType = file.Type
up.FileSize = file.Size
up.FileType = file.Type
up.SHA256 = file.SHA256
up.Status = UpNew
err := up.TxWrite(tx)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
db.RollbackTx(tx, nil)
return
}
up.Files[SHA256(file.SHA256)] = upfile
}
err = tx.Commit()
err := up.Write()
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
log.Println("new upload created")
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"result": gin.H{
@ -92,18 +78,28 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
return
}
files := form.File["upload[]"]
tx, err := db.DB.Handle.Beginx()
// Get the file's metadata from upload collection
up, err := GetByID(id)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
utils.JSONErrPriv(c, http.StatusNotFound, err)
return
}
log.Printf("Found matching upload %s", up.ID)
if up.Status == UpStored {
utils.JSONErr(c, http.StatusConflict, "already uploaded")
return
}
files := form.File["upload[]"]
var storedFiles []string
for _, file := range files {
log.Printf("Handling %s", file.Filename)
// Get the file's sha256
// Get the form's file sha256
hasher := sha256.New()
formFd, err := file.Open()
@ -114,69 +110,65 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
c, http.StatusInternalServerError,
fmt.Sprintf("Could not read file %s", file.Filename),
)
db.RollbackTx(tx, nil)
return
}
_, err = io.Copy(hasher, formFd)
if err != nil {
utils.JSONErr(c, http.StatusInternalServerError,
"")
db.RollbackTx(tx, nil)
utils.JSONErr(c, http.StatusInternalServerError, "")
return
}
sum256 := hex.EncodeToString(hasher.Sum(nil))
// Get the file's metadata from upload table
up, err := GetByHashID(sum256, id)
if err != nil {
utils.JSONErrPriv(c, http.StatusNotFound, err)
db.RollbackTx(tx, nil)
// Check sha256 same as present metadata
fileMeta, same := up.Files[SHA256(sum256)]
if !same {
utils.JSONErr(c, http.StatusConflict,
fmt.Sprintf("wrong hash for %s", file.Filename),
)
return
}
log.Printf("Found received file's %s metadata in local database\n", up.FileName)
//log.Println(up)
// Store file
if up.Status == UpStored {
utils.JSONErr(c, http.StatusConflict,
fmt.Sprintf("%s already uploaded", up.FileName))
return
}
//
//
log.Println("Storing file")
err = StoreFormFile(formFd, up.SHA256+up.FileExt)
log.Printf("Storing file %s", file.Filename)
err = storeFormFile(formFd, sum256+fileMeta.FileExt)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
db.RollbackTx(tx, nil)
removeFiles(storedFiles)
return
}
log.Printf("%s stored at %s", up.FileName,
GetStoreDestination(up.SHA256+up.FileExt))
storedFiles = append(storedFiles, GetStoreDestination(sum256+fileMeta.FileExt))
log.Printf("%s stored at %s", fileMeta.FileName,
GetStoreDestination(sum256+fileMeta.FileExt))
// Setting status to stored
log.Println("Updating upload status to stored")
up.Status = UpStored
err = up.TxSetState(tx, UpStored)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
db.RollbackTx(tx, func() {
err := os.Remove(GetStoreDestination(up.SHA256 + up.FileExt))
if err != nil {
log.Println(err)
}
})
return
}
//log.Println("Updating upload status to stored")
//up.Status = UpStored
//err = up.TxSetState(tx, UpStored)
//if err != nil {
//utils.JSONErrPriv(c, http.StatusInternalServerError, err)
//db.RollbackTx(tx, func() {
//err := os.Remove(GetStoreDestination(up.SHA256 + up.FileExt))
//if err != nil {
//log.Println(err)
//}
//})
//return
//}
}
err = tx.Commit()
up.Status = UpStored
// Commit
err = up.Write()
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
removeFiles(storedFiles)
return
}

@ -1,58 +1,15 @@
package storage
import (
"database/sql"
"errors"
"log"
"encoding/json"
"fmt"
"git.sp4ke.com/sp4ke/bit4sat-server.git/db"
"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
"git.sp4ke.com/sp4ke/bit4sat/db"
"github.com/mediocregopher/radix/v3"
)
var DB = db.DB
const (
DBUploadSchema = `
CREATE TABLE IF NOT EXISTS "upload" (
upload_id TEXT NOT NULL,
sha256 TEXT NOT NULL,
file_name TEXT NOT NULL,
file_type TEXT DEFAULT '',
file_size INTEGER NOT NULL,
file_ext TEXT DEFAULT '',
status INTEGER DEFAULT 0,
FOREIGN KEY (status) REFERENCES upload_status(type),
UNIQUE (upload_id, sha256)
);
`
DBUploadView = `
CREATE VIEW IF NOT EXISTS "upload_with_status" AS SELECT * FROM upload JOIN upload_status ON
upload.status = upload_status.type
`
DBUploadStatusSchema = `CREATE TABLE IF NOT EXISTS upload_status
(
type INTEGER PRIMARY KEY,
status TEXT NOT NULL UNIQUE
)
`
QNewUpload = `INSERT INTO upload
(upload_id, sha256, file_name, file_type, file_size, file_ext, status)
VALUES
(:upload_id, :sha256, :file_name, :file_type, :file_size, :file_ext, :status)`
QSetStatus = `UPDATE upload SET status = :status WHERE upload_id = :upload_id `
QGetByHashID = `SELECT * FROM upload
WHERE
sha256 = ?
AND
upload_id = ?`
)
const (
UpNew = iota
UpStored
@ -61,127 +18,71 @@ const (
)
var UploadStatus = map[int]string{
UpNew: "new upload",
UpNew: "new",
UpStored: "stored",
UpWaitingPayment: "waiting payment",
UpWaitingPayment: "waiting-payment",
UpReady: "ready",
}
var (
ErrDoesNotExist = errors.New("does not exist")
ErrAlreadyExists = errors.New("already exists")
)
type SHA256 string
type Upload struct {
ID string `db:"upload_id"`
SHA256 string `db:"sha256"`
FileName string `db:"file_name"`
FileType string `db:"file_type"`
FileSize int64 `db:"file_size"`
FileExt string `db:"file_ext"`
Status int `db:"status"`
func (sha SHA256) MarshalText() ([]byte, error) {
return []byte(sha), nil
}
// Returns true if id exists in DB
func IdExists(id string) (exists bool, err error) {
qUploadExists := `
SELECT EXISTS (SELECT upload_id FROM upload where upload_id = ?)
`
err = DB.Handle.Get(&exists, qUploadExists, id)
// No result found is also no result
if err == sql.ErrNoRows {
err = nil
}
return
type Upload struct {
ID string
Status uint8
Files map[SHA256]*UpFile
}
// Get a file by upload id and hash
func GetByHashID(sha256 string, id string) (*Upload, error) {
var up Upload
err := DB.Handle.Get(&up, QGetByHashID, sha256, id)
if err == sql.ErrNoRows {
return nil, ErrDoesNotExist
}
type UpFile struct {
FileName string
FileType string
FileSize int64
FileExt string
}
if err != nil {
return nil, err
func NewUpload() *Upload {
files := make(map[SHA256]*UpFile)
return &Upload{
Files: files,
}
return &up, nil
}
func (u *Upload) TxSetState(tx *sqlx.Tx, status int) error {
_, err := tx.NamedExec(QSetStatus, u)
// Returns true if id exists in DB
func IdExists(id string) (exists bool, err error) {
key := fmt.Sprintf("upload:%s", id)
if err != nil {
return err
}
err = DB.Do(radix.Cmd(&exists, "EXISTS", key))
return nil
return
}
func (u *Upload) TxWrite(tx *sqlx.Tx) error {
_, err := tx.NamedExec(QNewUpload, u)
sqlErr, isSqlErr := err.(sqlite3.Error)
if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint {
return ErrAlreadyExists
}
// Get a file by upload id and hash
func GetByID(id string) (*Upload, error) {
var buf []byte
key := fmt.Sprintf("upload:%s", id)
err := DB.Do(radix.Cmd(&buf, "GET", key))
if err != nil {
return err
return nil, err
}
return nil
var up Upload
err = json.Unmarshal(buf, &up)
return &up, err
}
func (u *Upload) Write() error {
_, err := DB.Handle.NamedExec(QNewUpload, u)
sqlErr, isSqlErr := err.(sqlite3.Error)
if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint {
return ErrAlreadyExists
}
// Create a new upload hash upload:id --> [Files]
key := fmt.Sprintf("upload:%s", u.ID)
enc, err := json.Marshal(u)
if err != nil {
return err
}
return nil
}
func init() {
_, err := DB.Handle.Exec(DBUploadSchema)
if err != nil {
log.Fatal(err)
}
_, err = DB.Handle.Exec(DBUploadView)
if err != nil {
log.Fatal(err)
}
_, err = DB.Handle.Exec(DBUploadStatusSchema)
if err != nil {
log.Fatal(err)
}
// Populate status types
query := `INSERT INTO upload_status (type, status) VALUES(?,?)`
for k, v := range UploadStatus {
_, err := DB.Handle.Exec(query, k, v)
if err != nil {
sqlErr, ok := err.(sqlite3.Error)
if ok && sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique {
log.Panic(err)
}
if !ok {
log.Panic(err)
}
}
}
return DB.Do(radix.FlatCmd(nil, "SET", key, enc))
}

Loading…
Cancel
Save