Skip to content

Commit

Permalink
Merge pull request #91 from filecoin-project/feature/powervoting-w3
Browse files Browse the repository at this point in the history
support web3storage
  • Loading branch information
ianconsolata authored Jun 19, 2024
2 parents 4435bff + 8f67927 commit 363d821
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
17 changes: 12 additions & 5 deletions powervoting-backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:alpine
FROM golang:alpine AS backend-builder

# env
ENV GO111MODULE=on \
Expand All @@ -16,14 +16,21 @@ COPY . .
# go build
RUN go build -o app .


#requires Node 18 or higher
FROM node:22

#
RUN npm install -g @web3-storage/w3cli

# move to /dist
WORKDIR /dist

# copy file to /dist
RUN cp /build/app .
RUN cp /build/configuration.yaml .
RUN cp /build/abi/power-voting.json .
RUN cp /build/abi/oracle.json .
RUN cp --from=backend-builder /build/app .
RUN cp --from=backend-builder /build/configuration.yaml .
RUN cp --from=backend-builder /build/abi/power-voting.json .
RUN cp --from=backend-builder /build/abi/oracle.json .

# expose server port
EXPOSE 9999
Expand Down
61 changes: 61 additions & 0 deletions powervoting-backend/api/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"powervoting-server/db"
"powervoting-server/model"
"powervoting-server/response"
"time"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
Expand Down Expand Up @@ -60,3 +68,56 @@ func VoteHistory(c *gin.Context) {
}
response.SuccessWithData(history, c)
}

func W3Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
zap.L().Error("upload file error: ", zap.Error(err))
response.SystemError(c)
return
}

//rand file name
randSource := rand.NewSource(time.Now().UnixNano())
r := rand.New(randSource)
randomNumber := r.Intn(1000000)
timeStamp := time.Now().Unix()

filePath := fmt.Sprintf("./uploads/%d_%d", timeStamp, randomNumber)
if err := c.SaveUploadedFile(file, filePath); err != nil {
zap.L().Error("save file error: ", zap.Error(err))
response.SystemError(c)
return
}

absolutePath, err := filepath.Abs(filePath)
if err != nil {
zap.L().Error("get file path error: ", zap.Error(err))
response.SystemError(c)
return
}
zap.L().Info("upload with w3")
cmd := exec.Command("w3", "upload", absolutePath, "--json", "--no-wrap")

//execut w3 upload xxxx
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
if err != nil {
os.Remove(absolutePath)
zap.L().Error("upload file error: ", zap.Error(err))
response.SystemError(c)
return
}
var jsonData map[string]interface{}
err = json.Unmarshal([]byte(outBuf.Bytes()), &jsonData)
if err != nil {
os.Remove(absolutePath)
zap.L().Error("upload file error: ", zap.Error(err))
response.SystemError(c)
return
}
os.Remove(absolutePath)
response.SuccessWithData(jsonData, c)
}
4 changes: 3 additions & 1 deletion powervoting-backend/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package routers

import (
"github.com/gin-gonic/gin"
"powervoting-server/api"
"powervoting-server/response"

"github.com/gin-gonic/gin"
)

// InitRouters initializes the routers for the power voting API endpoints.
Expand All @@ -34,4 +35,5 @@ func InitRouters(r *gin.Engine) {

powerVotingRouter.GET("/proposal/result", api.VoteResult)
powerVotingRouter.GET("/proposal/history", api.VoteHistory)
powerVotingRouter.POST("/w3storage/upload", api.W3Upload)
}
14 changes: 14 additions & 0 deletions powervoting-backend/w3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#after the container has started

#login
w3 login [email]

# if no space
w3 space create [Name]

# alread create , find did
w3 space ls

#
w3 space use <did>

0 comments on commit 363d821

Please sign in to comment.