Compare commits

...

8 Commits

Author SHA1 Message Date
Chakib Benziane 57b7fd0889 wip postgres 5 years ago
Chakib Benziane 6adad435fa Receive LN invoice on upload 5 years ago
Chakib Benziane 60ea287ccd Working upload process + redis cache 5 years ago
Chakib Benziane 16da18eda3 wip convert upload_id to RawBytes 5 years ago
Chakib Benziane f880881b9b working mariadb access + redis 5 years ago
Chakib Benziane 0145558cc8 maraidb compose file 5 years ago
Chakib Benziane 9b18f725d1 fix compose 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 package main
import ( import (
"git.sp4ke.com/sp4ke/bit4sat-server.git/storage" "git.sp4ke.com/sp4ke/bit4sat/storage"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "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,68 +3,66 @@ package db
import ( import (
"fmt" "fmt"
"log" "log"
"net/url"
"os" "os"
"path/filepath"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils" _ "github.com/lib/pq"
//_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" "github.com/mediocregopher/radix/v3"
) )
const ( const (
DBName = "bit4sat.sqlite" //DBName = "bit4sat.sqlite"
DBPath = "db-storage" DBName = "bit4sat"
DBPathEnv = "BIT4SAT_DB_PATH" DBHostEnv = "DB_HOST"
DBPragma = ` PRAGMA foreign_keys = ON; ` DBUserEnv = "DB_USER"
DBPassEnv = "DB_PASS"
//DBPragma = ` PRAGMA foreign_keys = ON; `
) )
var ( var (
DBOptions = map[string]string{
"_journal_mode": "WAL",
}
DB *Database DB *Database
) )
type Database struct { type Database struct {
Handle *sqlx.DB Sql *sqlx.DB
Redis *radix.Pool
} }
func (d *Database) Open() error { func (d *Database) Open() error {
var err error
dsnOptions := &url.Values{} // Get env vars
for k, v := range DBOptions { host, set := os.LookupEnv(DBHostEnv)
dsnOptions.Set(k, v) if !set {
log.Fatal("undefined DB_HOST env")
} }
// Get db base path user, set := os.LookupEnv(DBUserEnv)
path, set := os.LookupEnv(DBPathEnv)
if !set { if !set {
path = DBPath log.Fatal("undefined DB_USER env")
} }
// Create path if not exists pass, set := os.LookupEnv(DBPassEnv)
err := utils.Mkdir(path) if !set {
if err != nil { log.Fatal("undefined DB_PASS env")
log.Fatal(err)
} }
path = filepath.Join(path, DBName) dsn := fmt.Sprintf("%s:%s@%s/%s", user, pass, host, DBName)
//path = fmt.Sprintf("%s/%s", path, DBName)
dsn := fmt.Sprintf("file:%s?%s", path, dsnOptions.Encode()) log.Printf("Opening SQL at %s\n", dsn)
log.Printf("Opening sqlite db %s\n", dsn) d.Sql = sqlx.MustConnect("postgres", dsn)
d.Handle, err = sqlx.Open("sqlite3", dsn) // Execute Pragmas
//d.Sql.MustExec(DBPragma)
// Opend redis
d.Redis, err = radix.NewPool("tcp", "redis:6379", 10)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Execute Pragmas
d.Handle.MustExec(DBPragma)
return nil return nil
} }
@ -83,4 +81,5 @@ func RollbackTx(tx *sqlx.Tx, callback func()) {
func init() { func init() {
DB = &Database{} DB = &Database{}
DB.Open() DB.Open()
} }

@ -1,28 +1,70 @@
version: "3.4" version: "3.4"
volumes: volumes:
redis: db:
name: bit4sat-data maria-conf:
postgresql:
file-storage:
#sqlite:
gocache:
services: services:
bit4sat: app:
image: sp4ke/bit4sat image: sp4ke/bit4sat
build: build: ./docker
context: .
environment: environment:
- GO111MODULE=on - GO111MODULE=on
- BIT4SAT_STORAGE_PATH=/storage
- GOPATH=/go
- DB_HOST=maria
- DB_USER=bit4sat
- DB_PASS=bit4sat
- LN_CHARGE_API=10.192.122.10:9112
- LN_CHARGE_TOKEN=3emU3Fy8VasHCzMaMXHSVJYpQSqH3yXQj8N5cQFBbq3botrudJuR7zQkBBmFSbAmgXs9GD4j4U3J4R2sMfgqPo8q
deploy: #deploy:
replicas: 1 #replicas: 1
#
ports:
- "8880:8880"
volumes: volumes:
- $PWD:/src - $PWD:/src
- gocache:/go
#- ./db-storage:/sqlite
- file-storage:/storage
working_dir: /src working_dir: /src
command: maria:
- go image: mariadb:latest
- "run *.go" environment:
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_DATABASE=bit4sat
- MYSQL_USER=bit4sat
- MYSQL_PASSWORD=bit4sat
volumes:
- db:/var/lib/mysql
- maria-conf:/etc/mysql
postgres:
image: postgres:11.2
environment:
- POSTGRES_PASSWORD=bit4sat
- POSTGRES_USER=bit4sat
- POSTGRES_DB=bit4sat
volumes:
- postgresql:/var/lib/postgresql/data
redis:
image: redis:alpine
volumes:
- db:/data
command:
- redis-server
- --appendonly yes

@ -0,0 +1,15 @@
FROM golang:alpine
RUN apk update && apk add --no-cache \
git \
bash \
gcc build-base make
RUN mkdir -p /src
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

@ -4,18 +4,20 @@ go 1.12
require ( require (
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a 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/gin-gonic/gin v1.3.0
github.com/go-sql-driver/mysql v1.4.1
github.com/golang/protobuf v1.3.1 // indirect github.com/golang/protobuf v1.3.1 // indirect
github.com/jmoiron/sqlx v1.2.0 github.com/jmoiron/sqlx v1.2.0
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0
github.com/mattn/go-isatty v0.0.7 // indirect github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mattn/go-sqlite3 v1.10.0 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/segmentio/ksuid v1.0.2
github.com/stretchr/testify v1.3.0 // indirect github.com/stretchr/testify v1.3.0 // indirect
github.com/ugorji/go/codec v0.0.0-20190315113641-a70535d8491c // indirect github.com/ugorji/go/codec v0.0.0-20190320090025-2dc34c0b8780 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect golang.org/x/net v0.0.0-20190322120337-addf6b3196f6 // indirect
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 // indirect golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc // indirect
google.golang.org/appengine v1.5.0 // indirect google.golang.org/appengine v1.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
) )

@ -3,13 +3,14 @@ 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a h1:zBycVvXa03SIX+jdMv8wGu9TMDMWdN8EhaR1FoeKHNo= 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/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-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 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= 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/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/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.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 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@ -24,12 +25,17 @@ 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/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 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 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.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 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 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.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 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed h1:3dQJqqDouawQgl3gBE1PNHKFkJYGEuFb1DbSlaxdosE=
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 h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 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 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
@ -44,21 +50,23 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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 h1:JON3E2/GPW2iDNGoSAusl1KDf5TRQ8k8q7Tp097pZGs=
github.com/ugorji/go v1.1.2/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= 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-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-20190320090025-2dc34c0b8780 h1:vG/gY/PxA3v3l04qxe3tDjXyu3bozii8ulSlIPOYKhI=
github.com/ugorji/go/codec v0.0.0-20190315113641-a70535d8491c/go.mod h1:iT03XoTwV7xq/+UGwKO3UbC1nNNlopQiY61beSdrtOA= github.com/ugorji/go/codec v0.0.0-20190320090025-2dc34c0b8780/go.mod h1:iT03XoTwV7xq/+UGwKO3UbC1nNNlopQiY61beSdrtOA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 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-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-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-20190322120337-addf6b3196f6 h1:78jEq2G3J16aXneH23HSnTQQTCwMHoyO8VEiUH+bpPM=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190322120337-addf6b3196f6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 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-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-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-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-20190322080309-f49334f85ddc h1:4gbWbmmPFp4ySWICouJl6emP0MyS31yy9SrTlAGFT+g=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

@ -0,0 +1,124 @@
package ln
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/gin-gonic/gin"
)
const (
CurMSat = iota
CurSat
CurUSD
)
const (
LNChargeAPIEnv = "LN_CHARGE_API"
LNChargeTokenEnv = "LN_CHARGE_TOKEN"
InfoEndpoint = "info"
InvoiceEndpoint = "invoice"
)
var (
LNCEndpoint string
LNChargeToken string
)
type Charge struct {
client *http.Client
}
func NewCharge() *Charge {
netTransport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
c := &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
return &Charge{
client: c,
}
}
func (c *Charge) Info() (gin.H, error) {
result := make(gin.H)
resp, err := c.client.Get(getUrl(InfoEndpoint))
if err != nil {
return nil, err
}
jsonDec := json.NewDecoder(resp.Body)
jsonDec.Decode(&result)
return result, nil
}
func (c *Charge) Invoice(amount int, id string) (gin.H, error) {
result := make(gin.H)
reqData := gin.H{
"amount": 10,
"currency": "USD",
"description": fmt.Sprintf("bit4sat upload: %s", id),
}
jsonEnc, err := json.Marshal(reqData)
if err != nil {
return nil, err
}
resp, err := c.client.Post(getUrl(InvoiceEndpoint),
"application/json",
bytes.NewReader(jsonEnc))
if err != nil {
return nil, err
}
result = make(gin.H)
jsonDec := json.NewDecoder(resp.Body)
jsonDec.Decode(&result)
return result, nil
}
func getUrl(endpoint string) string {
url, err := url.Parse(fmt.Sprintf("http://api-token:%s@%s/%s",
LNChargeToken,
LNCEndpoint,
endpoint))
if err != nil {
log.Fatal(err)
}
return url.String()
}
func init() {
var set bool
LNCEndpoint, set = os.LookupEnv(LNChargeAPIEnv)
if !set {
log.Fatalf("%s not set", LNChargeAPIEnv)
}
LNChargeToken, set = os.LookupEnv(LNChargeTokenEnv)
if !set {
log.Fatalf("%s not set", LNChargeAPIEnv)
}
}

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

@ -9,7 +9,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils" "git.sp4ke.com/sp4ke/bit4sat/utils"
) )
const ( const (

@ -9,8 +9,9 @@ import (
"net/http" "net/http"
"os" "os"
"git.sp4ke.com/sp4ke/bit4sat-server.git/db" "git.sp4ke.com/sp4ke/bit4sat/db"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils" "git.sp4ke.com/sp4ke/bit4sat/ln"
"git.sp4ke.com/sp4ke/bit4sat/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/segmentio/ksuid" "github.com/segmentio/ksuid"
) )
@ -29,7 +30,7 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
// Create unique id // Create unique id
id := ksuid.New() id := ksuid.New()
tx, err := db.DB.Handle.Beginx() tx, err := db.DB.Sql.Beginx()
if err != nil { if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err) utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return return
@ -37,9 +38,8 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
for _, file := range uploadForm.Files { for _, file := range uploadForm.Files {
up := &Upload{} up := &Upload{}
up.ID = id.String() up.ID = id
log.Println(file.Name)
up.FileName, up.FileExt = utils.CleanFileName(file.Name) up.FileName, up.FileExt = utils.CleanFileName(file.Name)
up.FileSize = file.Size up.FileSize = file.Size
@ -61,6 +61,13 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
return return
} }
err = CacheSetUploadStatus(id.String(), UpNew)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
log.Println("new upload created")
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK, "status": http.StatusOK,
"result": gin.H{ "result": gin.H{
@ -94,7 +101,7 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
files := form.File["upload[]"] files := form.File["upload[]"]
tx, err := db.DB.Handle.Beginx() tx, err := db.DB.Sql.Beginx()
if err != nil { if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err) utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return return
@ -180,9 +187,35 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
return return
} }
err = CacheSetUploadStatus(id, UpStored)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
// Get a new lightning invoice
charge := ln.NewCharge()
// New invoice for 10$
invoice, err := charge.Invoice(10, id)
//info, err := ln.Info()
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
//c.JSON(http.StatusOK, gin.H{
//"status": http.StatusOK,
//"result": fmt.Sprintf("%d files uploaded uploaded !", len(files)),
//})
result := gin.H{
"message": fmt.Sprintf("%d files uploaded uploaded !", len(files)),
"invoice": invoice,
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK, "status": http.StatusOK,
"result": fmt.Sprintf("%d files uploaded uploaded !", len(files)), "result": result,
}) })
} }

@ -3,39 +3,53 @@ package storage
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"fmt"
"log" "log"
"git.sp4ke.com/sp4ke/bit4sat-server.git/db" "git.sp4ke.com/sp4ke/bit4sat/db"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3" "github.com/mattn/go-sqlite3"
"github.com/mediocregopher/radix/v3"
"github.com/segmentio/ksuid"
) )
var DB = db.DB var DB = db.DB
const ( const (
DBUploadSchema = ` DBUploadSchema = `
CREATE TABLE IF NOT EXISTS "upload" ( CREATE TABLE IF NOT EXISTS upload (
upload_id TEXT NOT NULL, id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
sha256 TEXT NOT NULL, upload_id CHAR(27) NOT NULL,
file_name TEXT NOT NULL, sha256 CHAR(64) NOT NULL,
file_type TEXT DEFAULT '', file_name CHAR(255) NOT NULL,
file_size INTEGER NOT NULL, file_type CHAR(255) DEFAULT '',
file_ext TEXT DEFAULT '', file_size INT NOT NULL,
status INTEGER DEFAULT 0, file_ext CHAR(255) DEFAULT '',
status INT DEFAULT 0,
FOREIGN KEY (status) REFERENCES upload_status(type), FOREIGN KEY (status) REFERENCES upload_status(type),
UNIQUE (upload_id, sha256) UNIQUE (upload_id, sha256)
); );
` `
DBUploadView = ` DBUploadView = `
CREATE VIEW IF NOT EXISTS "upload_with_status" AS SELECT * FROM upload JOIN upload_status ON CREATE VIEW IF NOT EXISTS upload_with_status
AS SELECT
upload.upload_id,
upload.sha256,
upload_status.status,
upload.file_name,
upload.file_type,
upload.file_size,
upload.file_ext
FROM upload
JOIN upload_status ON
upload.status = upload_status.type upload.status = upload_status.type
` `
DBUploadStatusSchema = `CREATE TABLE IF NOT EXISTS upload_status DBUploadStatusSchema = `CREATE TABLE IF NOT EXISTS upload_status
( (
type INTEGER PRIMARY KEY, type INT NOT NULL PRIMARY KEY,
status TEXT NOT NULL UNIQUE status CHAR(255) NOT NULL UNIQUE
) )
` `
@ -46,11 +60,14 @@ upload.status = upload_status.type
QSetStatus = `UPDATE upload SET status = :status WHERE upload_id = :upload_id ` QSetStatus = `UPDATE upload SET status = :status WHERE upload_id = :upload_id `
QGetByHashID = `SELECT * FROM upload QGetByHashID = `SELECT upload_id,
WHERE sha256,
sha256 = ? file_name,
AND file_type,
upload_id = ?` file_size,
file_ext,
status
FROM upload WHERE sha256 = ? AND upload_id = ?`
) )
const ( const (
@ -73,26 +90,26 @@ var (
) )
type Upload struct { type Upload struct {
ID string `db:"upload_id"` ID ksuid.KSUID `db:"upload_id"`
SHA256 string `db:"sha256"` SHA256 string `db:"sha256"`
FileName string `db:"file_name"` FileName string `db:"file_name"`
FileType string `db:"file_type"` FileType string `db:"file_type"`
FileSize int64 `db:"file_size"` FileSize int64 `db:"file_size"`
FileExt string `db:"file_ext"` FileExt string `db:"file_ext"`
Status int `db:"status"` Status int `db:"status"`
}
func CacheSetUploadStatus(id string, status int) error {
key := fmt.Sprintf("upload_status_%s", id)
return DB.Redis.Do(radix.FlatCmd(nil, "SET", key, status))
} }
// Returns true if id exists in DB // Returns true if id exists in DB
func IdExists(id string) (exists bool, err error) { func IdExists(id string) (exists bool, err error) {
qUploadExists := ` key := fmt.Sprintf("upload_status_%s", id)
SELECT EXISTS (SELECT upload_id FROM upload where upload_id = ?)
`
err = DB.Handle.Get(&exists, qUploadExists, id)
// No result found is also no result err = DB.Redis.Do(radix.Cmd(&exists, "EXISTS", key))
if err == sql.ErrNoRows {
err = nil
}
return return
} }
@ -100,7 +117,8 @@ func IdExists(id string) (exists bool, err error) {
// Get a file by upload id and hash // Get a file by upload id and hash
func GetByHashID(sha256 string, id string) (*Upload, error) { func GetByHashID(sha256 string, id string) (*Upload, error) {
var up Upload var up Upload
err := DB.Handle.Get(&up, QGetByHashID, sha256, id)
err := DB.Sql.Get(&up, QGetByHashID, sha256, id)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, ErrDoesNotExist return nil, ErrDoesNotExist
@ -126,7 +144,9 @@ func (u *Upload) TxSetState(tx *sqlx.Tx, status int) error {
func (u *Upload) TxWrite(tx *sqlx.Tx) error { func (u *Upload) TxWrite(tx *sqlx.Tx) error {
_, err := tx.NamedExec(QNewUpload, u) _, err := tx.NamedExec(QNewUpload, u)
sqlErr, isSqlErr := err.(sqlite3.Error) sqlErr, isSqlErr := err.(sqlite3.Error)
if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint { if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint {
return ErrAlreadyExists return ErrAlreadyExists
} }
@ -139,7 +159,7 @@ func (u *Upload) TxWrite(tx *sqlx.Tx) error {
} }
func (u *Upload) Write() error { func (u *Upload) Write() error {
_, err := DB.Handle.NamedExec(QNewUpload, u) _, err := DB.Sql.NamedExec(QNewUpload, u)
sqlErr, isSqlErr := err.(sqlite3.Error) sqlErr, isSqlErr := err.(sqlite3.Error)
if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint { if isSqlErr && sqlErr.Code == sqlite3.ErrConstraint {
return ErrAlreadyExists return ErrAlreadyExists
@ -153,25 +173,27 @@ func (u *Upload) Write() error {
} }
func init() { func init() {
_, err := DB.Handle.Exec(DBUploadSchema) _, err := DB.Sql.Exec(DBUploadStatusSchema)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
_, err = DB.Handle.Exec(DBUploadView) _, err = DB.Sql.Exec(DBUploadSchema)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
_, err = DB.Handle.Exec(DBUploadStatusSchema) _, err = DB.Sql.Exec(DBUploadView)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Populate status types // Populate status types
query := `INSERT INTO upload_status (type, status) VALUES(?,?)` query := `INSERT INTO upload_status
(type, status) VALUES(?,?)
ON DUPLICATE KEY UPDATE status = ?`
for k, v := range UploadStatus { for k, v := range UploadStatus {
_, err := DB.Handle.Exec(query, k, v) _, err := DB.Sql.Exec(query, k, v, v)
if err != nil { if err != nil {
sqlErr, ok := err.(sqlite3.Error) sqlErr, ok := err.(sqlite3.Error)
if ok && sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique { if ok && sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique {

Loading…
Cancel
Save