Skip to content

Commit fec8b42

Browse files
committed
addicionado: banco de dados sqlitq
1 parent 3b5b599 commit fec8b42

11 files changed

Lines changed: 296 additions & 58 deletions

File tree

.air.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = ["clear"]
7+
bin = "./tmp/main"
8+
cmd = "go build -o ./tmp/main ./cmd/server/main.go"
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = false
22+
poll_interval = 0
23+
post_cmd = []
24+
pre_cmd = []
25+
rerun = false
26+
rerun_delay = 500
27+
send_interrupt = false
28+
stop_on_error = false
29+
30+
[color]
31+
app = ""
32+
build = "yellow"
33+
main = "magenta"
34+
runner = "green"
35+
watcher = "cyan"
36+
37+
[log]
38+
main_only = false
39+
time = false
40+
41+
[misc]
42+
clean_on_exit = false
43+
44+
[proxy]
45+
app_port = 0
46+
enabled = false
47+
proxy_port = 0
48+
49+
[screen]
50+
clear_on_rebuild = false
51+
keep_scroll = true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.env
22
go.sum
33
instances
4-
main
4+
main
5+
testdata
6+
tmp

Dockerfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
FROM golang:1.22.3-bullseye
22

3-
LABEL version="0.0.1" description="Worker HTTP para Gerenciamento de Sessões na API de WhatsApp"
4-
LABEL maintainer="jrCleber" git="https://github.com/jrCleber"
5-
LABEL contact="suporte@codechat.dev"
3+
LABEL codechat.worker.version="0.0.2"
4+
LABEL codechat.worker.description="Worker HTTP para Gerenciamento de Sessões na API de WhatsApp"
5+
LABEL codechat.worker.maintainer="jrCleber"
6+
LABEL codechat.worker.git="https://github.com/jrCleber"
7+
LABEL codechat.worker.contact="suporte@codechat.dev"
68

79
ENV DOCKER_ENV=true
10+
ENV CGO_ENABLED=1
11+
12+
RUN apt-get install gcc
13+
RUN apt-get install build-essential
814

915
WORKDIR /worker
1016

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,18 @@ Além disso, um token global deve ser definido nas variáveis de ambiente. O cli
4949
│ └── app.go
5050
5151
├── /pkg
52-
│ └── /config
53-
│ └── config.go
52+
│ ├── /config
53+
│ │ └── config.go
54+
│ └── /db
55+
│ ├── /models
56+
│ │ └── creds_model.go
57+
│ └── conn.go
5458
59+
├── /tmp
60+
│ ├── build-errors.log
61+
│ └── main
62+
63+
├── .air.toml
5564
├── .env
5665
├── .gitignore
5766
├── build.sh
@@ -63,6 +72,20 @@ Além disso, um token global deve ser definido nas variáveis de ambiente. O cli
6372
└── README.md
6473
```
6574

75+
---
76+
## Ajuste de compatibilidade com SQLite se necessário
77+
78+
- `build-essential`
79+
```sh
80+
sudo apt-get install build-essential
81+
```
82+
83+
- `gcc`
84+
```sh
85+
sudo apt-get install gcc
86+
```
87+
---
88+
6689
## Instalação
6790

6891
### Instalando o GO

api/handlers/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
type Response struct {
9-
Code int `json:"statusCode"`
10-
Data any `json:"message,omitempty"`
9+
Code int `json:"statusCode"`
10+
Data any `json:"message,omitempty"`
1111
Err string `json:"error"`
1212
}
1313

@@ -58,14 +58,14 @@ func UnmarshalDescriptionError(e error) *Response {
5858
return &Response{
5959
Code: http.StatusBadRequest,
6060
Data: []string{"body is empty"},
61-
Err: "Bad Request",
61+
Err: "Bad Request",
6262
}
6363
}
6464
fieldName, dataType := extractErrorDetails(e.Error())
6565
return &Response{
6666
Code: http.StatusBadRequest,
6767
Data: []string{fieldName + " must be of type " + dataType + "."},
68-
Err: "Bad Request",
68+
Err: "Bad Request",
6969
}
7070
}
7171

api/handlers/session_handler.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
67
"worker-session/internal/session"
78

@@ -25,14 +26,14 @@ func getParams(r *http.Request) (string, string, string) {
2526
return group, instance, key
2627
}
2728

28-
func (h *Session) POST_GroupFolder(r *http.Request) *Response {
29+
func (h *Session) POST_Group(r *http.Request) *Response {
2930
var body map[string]string
3031
e := render.DecodeJSON(r.Body, &body)
3132
if err := UnmarshalDescriptionError(e); err != nil {
3233
return err
3334
}
3435

35-
status, err := h.service.CreateFolder(body["group"], "")
36+
status, err := h.service.CreateGroup(body["group"])
3637

3738
response := NewResponse(status)
3839

@@ -44,7 +45,24 @@ func (h *Session) POST_GroupFolder(r *http.Request) *Response {
4445
return response
4546
}
4647

47-
func (h *Session) POST_InstanceFolder(r *http.Request) *Response {
48+
func (h *Session) DELETE_Group(r *http.Request) *Response {
49+
params_group, _, _ := getParams(r)
50+
51+
status, err := h.service.RemoveGroup(params_group)
52+
53+
response := NewResponse(status)
54+
55+
if err != nil {
56+
response.SetError(err)
57+
return response
58+
}
59+
60+
response.SetCode(status)
61+
62+
return response
63+
}
64+
65+
func (h *Session) POST_InstanceDB(r *http.Request) *Response {
4866
var body map[string]string
4967
e := render.DecodeJSON(r.Body, &body)
5068
if err := UnmarshalDescriptionError(e); err != nil {
@@ -53,7 +71,7 @@ func (h *Session) POST_InstanceFolder(r *http.Request) *Response {
5371

5472
params_group, _, _ := getParams(r)
5573

56-
status, err := h.service.CreateFolder(params_group, body["instance"])
74+
status, err := h.service.CreateInstanceDb(params_group, body["instance"])
5775

5876
response := NewResponse(status)
5977

@@ -65,10 +83,10 @@ func (h *Session) POST_InstanceFolder(r *http.Request) *Response {
6583
return response
6684
}
6785

68-
func (h *Session) DELETE_InstanceFolder(r *http.Request) *Response {
86+
func (h *Session) DELETE_InstanceDB(r *http.Request) *Response {
6987
params_group, params_instance, _ := getParams(r)
7088

71-
status, err := h.service.RemoveFolder(params_group, params_instance)
89+
status, err := h.service.RemoveInstanceDb(params_group, params_instance)
7290

7391
response := NewResponse(status)
7492

@@ -83,7 +101,7 @@ func (h *Session) DELETE_InstanceFolder(r *http.Request) *Response {
83101
}
84102

85103
func (h *Session) POST_Credentials(r *http.Request) *Response {
86-
var body map[string]string
104+
var body map[string]any
87105
e := render.DecodeJSON(r.Body, &body)
88106
if err := UnmarshalDescriptionError(e); err != nil {
89107
return err
@@ -119,9 +137,15 @@ func (h *Session) GET_Credentials(r *http.Request) *Response {
119137
return response
120138
}
121139

140+
json_str := string(binary)
141+
var unescaped string
142+
json.Unmarshal([]byte(json_str), &unescaped)
143+
122144
var data map[string]any
123-
err = json.Unmarshal(binary, &data)
145+
err = json.Unmarshal([]byte(unescaped), &data)
146+
fmt.Println("2")
124147
if err != nil {
148+
response.SetCode(http.StatusBadRequest)
125149
response.SetError(err)
126150
return response
127151
}

api/routers/session_routes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ func (x *Session) Routes() *chi.Mux {
2626
r.Use(middlewares.AuthGuard(x.env.GlobalToken))
2727

2828
// Criando a pasta que conterá o grupo de instâncias
29-
r.Post("/", ResponseRequest(x.handler.POST_GroupFolder))
29+
r.Post("/", ResponseRequest(x.handler.POST_Group))
3030
// Removendo o grupo com todas as instâncias
31-
r.Delete("/", ResponseRequest(x.handler.DELETE_InstanceFolder))
31+
r.Delete("/", ResponseRequest(x.handler.DELETE_Group))
3232
})
3333

3434
instance_router := chi.NewRouter()
3535
instance_router.Route("/", func(r chi.Router) {
3636
r.Use(middlewares.LimitBodySize(x.env.LimitBody))
3737

38-
r.Post("/", ResponseRequest(x.handler.POST_InstanceFolder))
39-
r.Delete("/{instance}", ResponseRequest(x.handler.DELETE_InstanceFolder))
38+
r.Post("/", ResponseRequest(x.handler.POST_InstanceDB))
39+
r.Delete("/{instance}", ResponseRequest(x.handler.DELETE_InstanceDB))
4040

4141
r.Post("/{instance}/{key}", ResponseRequest(x.handler.POST_Credentials))
4242
r.Get("/{instance}/{key}", ResponseRequest(x.handler.GET_Credentials))

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ go 1.22.3
44

55
require github.com/go-chi/render v1.0.3
66

7+
require (
8+
github.com/jinzhu/inflection v1.0.0 // indirect
9+
github.com/jinzhu/now v1.1.5 // indirect
10+
github.com/mattn/go-sqlite3 v1.14.17 // indirect
11+
gorm.io/driver/sqlite v1.5.5 // indirect
12+
gorm.io/gorm v1.25.10 // indirect
13+
)
14+
715
require (
816
github.com/ajg/form v1.5.1 // indirect
917
github.com/go-chi/chi/v5 v5.0.12 // indirect

0 commit comments

Comments
 (0)