Skip to content

Commit d712e8e

Browse files
committed
Added api srver
1 parent 01cfcbb commit d712e8e

File tree

12 files changed

+904
-5
lines changed

12 files changed

+904
-5
lines changed

api/configs/env.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package configs
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/spf13/viper"
8+
)
9+
10+
var Env *env
11+
12+
func InitEnvConfigs() {
13+
Env = loadEnvVariables()
14+
fmt.Printf("I read %s\n\n", Env.Mongodb_URI)
15+
}
16+
17+
type env struct {
18+
Mongodb_URI string `mapstructure:"MONGODB_URI"`
19+
Mongodb_Hostname string `mapstructure:"MONGODB_HOSTNAME"`
20+
Port string `mapstructure:"PORT"`
21+
AES_key string `mapstructure:"AES_KEY"`
22+
AES_iv string `mapstructure:"AES_IV"`
23+
}
24+
25+
func loadEnvVariables() (config *env) {
26+
viper.AddConfigPath(".")
27+
28+
viper.SetConfigName("app")
29+
30+
viper.SetConfigType("env")
31+
32+
if err := viper.ReadInConfig(); err != nil {
33+
log.Fatal("Error reading env file", err)
34+
}
35+
36+
if err := viper.Unmarshal(&config); err != nil {
37+
log.Fatal(err)
38+
}
39+
40+
return
41+
}

api/go.mod

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module github.com/marsian83/one-panel/api
2+
3+
go 1.20
4+
5+
require (
6+
github.com/gin-gonic/gin v1.9.1
7+
github.com/spf13/viper v1.16.0
8+
go.mongodb.org/mongo-driver v1.12.1
9+
)
10+
11+
require (
12+
github.com/bytedance/sonic v1.9.1 // indirect
13+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
14+
github.com/fsnotify/fsnotify v1.6.0 // indirect
15+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
16+
github.com/gin-contrib/sse v0.1.0 // indirect
17+
github.com/go-playground/locales v0.14.1 // indirect
18+
github.com/go-playground/universal-translator v0.18.1 // indirect
19+
github.com/go-playground/validator/v10 v10.14.0 // indirect
20+
github.com/goccy/go-json v0.10.2 // indirect
21+
github.com/golang/snappy v0.0.1 // indirect
22+
github.com/hashicorp/hcl v1.0.0 // indirect
23+
github.com/json-iterator/go v1.1.12 // indirect
24+
github.com/klauspost/compress v1.13.6 // indirect
25+
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
26+
github.com/leodido/go-urn v1.2.4 // indirect
27+
github.com/magiconair/properties v1.8.7 // indirect
28+
github.com/mattn/go-isatty v0.0.19 // indirect
29+
github.com/mitchellh/mapstructure v1.5.0 // indirect
30+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
31+
github.com/modern-go/reflect2 v1.0.2 // indirect
32+
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
33+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
34+
github.com/spf13/afero v1.9.5 // indirect
35+
github.com/spf13/cast v1.5.1 // indirect
36+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
37+
github.com/spf13/pflag v1.0.5 // indirect
38+
github.com/subosito/gotenv v1.4.2 // indirect
39+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
40+
github.com/ugorji/go/codec v1.2.11 // indirect
41+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
42+
github.com/xdg-go/scram v1.1.2 // indirect
43+
github.com/xdg-go/stringprep v1.0.4 // indirect
44+
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
45+
golang.org/x/arch v0.3.0 // indirect
46+
golang.org/x/crypto v0.9.0 // indirect
47+
golang.org/x/net v0.10.0 // indirect
48+
golang.org/x/sync v0.1.0 // indirect
49+
golang.org/x/sys v0.8.0 // indirect
50+
golang.org/x/text v0.9.0 // indirect
51+
google.golang.org/protobuf v1.30.0 // indirect
52+
gopkg.in/ini.v1 v1.67.0 // indirect
53+
gopkg.in/yaml.v3 v3.0.1 // indirect
54+
)

api/go.sum

+569
Large diffs are not rendered by default.

api/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/marsian83/one-panel/api/configs"
8+
"github.com/marsian83/one-panel/api/src/handlers"
9+
"github.com/marsian83/one-panel/api/src/helpers"
10+
"github.com/marsian83/one-panel/api/src/mongodb"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
)
13+
14+
var dbClient *mongo.Client
15+
16+
func main() {
17+
configs.InitEnvConfigs()
18+
19+
port := configs.Env.Port
20+
21+
router := gin.Default()
22+
dbClient = mongodb.GetClient()
23+
24+
router.GET("/ping", handlers.Ping)
25+
router.GET("/d/:uri", handlers.GetEntries)
26+
27+
key := []byte(configs.Env.AES_key)
28+
iv := []byte(configs.Env.AES_iv)
29+
30+
fmt.Println(string(helpers.EncryptAES(key, iv, []byte("{\"db\":\"onelot1_sometimes\",\"artifact\":\"thikkhai\",\"collection\":\"patla\"}"))))
31+
32+
router.Run(fmt.Sprintf(":%s", port))
33+
}

api/src/handlers/index.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"net/http"
9+
"time"
10+
11+
"github.com/gin-gonic/gin"
12+
"github.com/marsian83/one-panel/api/configs"
13+
"github.com/marsian83/one-panel/api/src/helpers"
14+
"github.com/marsian83/one-panel/api/src/mongodb"
15+
"go.mongodb.org/mongo-driver/bson"
16+
)
17+
18+
func Ping(c *gin.Context) {
19+
c.JSON(http.StatusOK, gin.H{"message": "pong"})
20+
}
21+
22+
func GetEntries(c *gin.Context) {
23+
type Meta struct {
24+
DBName string `json:"db"`
25+
Artifact string `json:"artifact"`
26+
Collection string `json:"collection"`
27+
}
28+
29+
type Entry struct {
30+
Name string `bson:"name"`
31+
Data []struct {
32+
ID int `json:"id"`
33+
Data interface{} `json:"data"`
34+
} `bson:"data"`
35+
}
36+
37+
uri, _ := hex.DecodeString(c.Param("uri"))
38+
39+
key, _ := hex.DecodeString(configs.Env.AES_key)
40+
iv, _ := hex.DecodeString(configs.Env.AES_iv)
41+
42+
encrypted := []byte(uri)
43+
44+
decrypted, err := helpers.DecryptAES([]byte(key), []byte(iv), encrypted)
45+
46+
if err != nil {
47+
c.JSON(http.StatusInternalServerError, gin.H{"Error:": err})
48+
return
49+
}
50+
51+
var meta Meta
52+
if err := json.Unmarshal([]byte(string(decrypted)), &meta); err != nil {
53+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid URI"})
54+
return
55+
}
56+
57+
client := mongodb.GetClient()
58+
collection := client.Database(meta.DBName).Collection(meta.Artifact)
59+
60+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
61+
defer cancel()
62+
63+
entry := Entry{}
64+
65+
if err := collection.FindOne(ctx, bson.M{"name": meta.Collection}).Decode(&entry); err != nil {
66+
fmt.Println(err)
67+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
68+
return
69+
}
70+
71+
c.JSON(http.StatusOK, entry.Data)
72+
}

api/src/helpers/utils.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package helpers
2+
3+
import (
4+
"bytes"
5+
"crypto/aes"
6+
"crypto/cipher"
7+
"errors"
8+
"fmt"
9+
)
10+
11+
func EncryptAES(key, iv, plaintext []byte) ([]byte, error) {
12+
block, err := aes.NewCipher(key)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
paddedPlaintext := padPlaintext(plaintext, aes.BlockSize)
18+
19+
ciphertext := make([]byte, len(paddedPlaintext))
20+
21+
mode := cipher.NewCBCEncrypter(block, iv)
22+
mode.CryptBlocks(ciphertext, paddedPlaintext)
23+
24+
return ciphertext, nil
25+
}
26+
27+
func DecryptAES(key, iv, ciphertext []byte) ([]byte, error) {
28+
block, err := aes.NewCipher(key)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
if len(ciphertext) < aes.BlockSize {
34+
return nil, errors.New("ciphertext too short")
35+
}
36+
37+
mode := cipher.NewCBCDecrypter(block, iv)
38+
decrypted := make([]byte, len(ciphertext))
39+
mode.CryptBlocks(decrypted, ciphertext)
40+
41+
fmt.Println("sometimes fine", string(decrypted))
42+
43+
// Remove padding
44+
padSize := int(decrypted[len(decrypted)-1])
45+
if padSize > aes.BlockSize || padSize > len(decrypted) {
46+
return nil, errors.New("invalid padding size")
47+
}
48+
decrypted = decrypted[:len(decrypted)-padSize]
49+
50+
return decrypted, nil
51+
}
52+
53+
func padPlaintext(plaintext []byte, blockSize int) []byte {
54+
padding := blockSize - (len(plaintext) % blockSize)
55+
paddedPlaintext := append(plaintext, bytes.Repeat([]byte{byte(padding)}, padding)...)
56+
return paddedPlaintext
57+
}

api/src/mongodb/mongodb.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package mongodb
2+
3+
import (
4+
"context"
5+
"reflect"
6+
"sync"
7+
8+
"github.com/marsian83/one-panel/api/configs"
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/bson/bsontype"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/mongo/options"
13+
)
14+
15+
var (
16+
once sync.Once
17+
dbClient *mongo.Client
18+
)
19+
20+
func GetClient() *mongo.Client {
21+
once.Do(func() {
22+
db_uri := configs.Env.Mongodb_URI
23+
24+
tM := reflect.TypeOf(bson.M{})
25+
reg := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build()
26+
clientOptions := options.Client().ApplyURI(db_uri).SetRegistry(reg)
27+
28+
client, err := mongo.Connect(context.TODO(), clientOptions)
29+
30+
if err != nil {
31+
return
32+
}
33+
34+
dbClient = client
35+
})
36+
37+
return dbClient
38+
}

client/src/App.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ function AuthLoader({ children }: { children: ReactNode }) {
131131
}, []);
132132

133133
return loading ? (
134-
<>
135-
<Loader />
136-
</>
134+
<article className="flex justify-center items-center">
135+
<div className="w-28 h-28">
136+
<Loader />
137+
</div>
138+
</article>
137139
) : (
138140
<>{children}</>
139141
);

go.work

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ go 1.20
33
use (
44
./services/auth
55
./services/db_access
6+
./api
67
)

go.work.sum

+12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
cloud.google.com/go v0.110.0/go.mod h1:SJnCLqQ0FCFGSZMUNUf84MV3Aia54kn7pi8st7tMzaY=
2+
cloud.google.com/go/compute v1.19.0/go.mod h1:rikpw2y+UMidAe9tISo04EHNOIf42RLYF/q8Bs93scU=
3+
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
4+
cloud.google.com/go/firestore v1.9.0/go.mod h1:HMkjKHNTtRyZNiMzu7YAsLr9K3X2udY2AMwDaMEQiiE=
5+
cloud.google.com/go/longrunning v0.4.1/go.mod h1:4iWDqhBZ70CvZ6BfETbvam3T8FMvLK+eFj0E6AaRQTo=
6+
github.com/armon/go-metrics v0.4.0/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
7+
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
8+
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
19
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
210
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
311
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
412
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
513
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
14+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
15+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16+
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
17+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
618
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
719
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
820
google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8=

services/db_access/Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM golang:alpine as BUILD_IMAGE
2+
3+
WORKDIR /app
4+
5+
COPY go.mod .
6+
COPY go.sum .
7+
8+
RUN go mod download
9+
10+
COPY main.go .
11+
COPY src src
12+
COPY configs configs
13+
14+
RUN go build -o app
15+
16+
17+
FROM scratch
18+
19+
COPY --from=BUILD_IMAGE /app/app /
20+
21+
CMD [ "/app" ]

services/db_access/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ func main() {
2121
dbClient = mongodb.GetClient()
2222

2323
router.GET("/ping", handlers.Ping)
24-
router.PUT("/entries", handlers.GetEntries)
24+
router.GET("/entries", handlers.GetEntries)
2525
router.POST("/allocate", handlers.AllotDatabase)
2626
router.POST("/entry", handlers.NewEntry)
2727

28-
fmt.Printf("ok see here %s\n", configs.Env.Mongodb_Hostname)
2928
router.Run(fmt.Sprintf(":%s", port))
3029
}

0 commit comments

Comments
 (0)