├── .gitignore
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
├── api
├── handler
│ ├── company.go
│ └── user.go
└── main.go
├── bin
└── api
├── cli
└── go.test.sh
├── cmd
└── main.go
├── config
├── dbconfig.yml
├── dev.env
├── production.env
└── testing.env
├── coverage.txt
├── doc
├── README.md
├── api.apib
└── index.html
├── docker-compose.yml
├── migrations
├── 20171103095219-user.sql
└── 20171103095223-company.sql
└── pkg
├── company
├── company.go
├── company_test.go
└── mysql.go
├── middleware
├── Cors.go
└── isAuthenticated.go
├── mysql
└── mysql.go
└── user
├── mysql.go
├── user.go
└── user_test.go
/.gitignore:
--------------------------------------------------------------------------------
1 | bin/
2 | vendor/
--------------------------------------------------------------------------------
/Gopkg.lock:
--------------------------------------------------------------------------------
1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
2 |
3 |
4 | [[projects]]
5 | name = "github.com/asaskevich/govalidator"
6 | packages = ["."]
7 | revision = "73945b6115bfbbcc57d89b7316e28109364124e1"
8 | version = "v7"
9 |
10 | [[projects]]
11 | name = "github.com/codegangsta/negroni"
12 | packages = ["."]
13 | revision = "fde5e16d32adc7ad637e9cd9ad21d4ebc6192535"
14 | version = "v0.2.0"
15 |
16 | [[projects]]
17 | name = "github.com/go-sql-driver/mysql"
18 | packages = ["."]
19 | revision = "a0583e0143b1624142adab07e0e97fe106d99561"
20 | version = "v1.3"
21 |
22 | [[projects]]
23 | name = "github.com/gorilla/context"
24 | packages = ["."]
25 | revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a"
26 | version = "v1.1"
27 |
28 | [[projects]]
29 | name = "github.com/gorilla/mux"
30 | packages = ["."]
31 | revision = "24fca303ac6da784b9e8269f724ddeb0b2eea5e7"
32 | version = "v1.5.0"
33 |
34 | [[projects]]
35 | name = "github.com/joho/godotenv"
36 | packages = ["."]
37 | revision = "a79fa1e548e2c689c241d10173efd51e5d689d5b"
38 | version = "v1.2.0"
39 |
40 | [[projects]]
41 | branch = "master"
42 | name = "github.com/rubenv/sql-migrate"
43 | packages = [".","sqlparse"]
44 | revision = "79fe99e24311fa42469fb2ca23eb3f8f065e6155"
45 |
46 | [[projects]]
47 | name = "gopkg.in/gorp.v1"
48 | packages = ["."]
49 | revision = "c87af80f3cc5036b55b83d77171e156791085e2e"
50 | version = "v1.7.1"
51 |
52 | [solve-meta]
53 | analyzer-name = "dep"
54 | analyzer-version = 1
55 | inputs-digest = "4be7cdb387d47d8146e2d16c70ff3cdd01e9a2fd93225e6a4837b715931b5403"
56 | solver-name = "gps-cdcl"
57 | solver-version = 1
58 |
--------------------------------------------------------------------------------
/Gopkg.toml:
--------------------------------------------------------------------------------
1 |
2 | # Gopkg.toml example
3 | #
4 | # Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5 | # for detailed Gopkg.toml documentation.
6 | #
7 | # required = ["github.com/user/thing/cmd/thing"]
8 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9 | #
10 | # [[constraint]]
11 | # name = "github.com/user/project"
12 | # version = "1.0.0"
13 | #
14 | # [[constraint]]
15 | # name = "github.com/user/project2"
16 | # branch = "dev"
17 | # source = "github.com/myfork/project2"
18 | #
19 | # [[override]]
20 | # name = "github.com/x/y"
21 | # version = "2.4.0"
22 |
23 |
24 | [[constraint]]
25 | name = "github.com/asaskevich/govalidator"
26 | version = "7.0.0"
27 |
28 | [[constraint]]
29 | name = "github.com/codegangsta/negroni"
30 | version = "0.2.0"
31 |
32 | [[constraint]]
33 | name = "github.com/go-sql-driver/mysql"
34 | version = "1.3.0"
35 |
36 | [[constraint]]
37 | name = "github.com/gorilla/context"
38 | version = "1.1.0"
39 |
40 | [[constraint]]
41 | name = "github.com/gorilla/mux"
42 | version = "1.5.0"
43 |
44 | [[constraint]]
45 | name = "github.com/joho/godotenv"
46 | version = "1.2.0"
47 |
48 | [[constraint]]
49 | branch = "master"
50 | name = "github.com/thecodenation/app"
51 |
52 | [[constraint]]
53 | name = "gopkg.in/gorp.v1"
54 | version = "1.7.1"
55 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: all
2 | all: format test build
3 |
4 |
5 | SHELL := env SKEL_ENV=$(SKEL_ENV) $(SHELL)
6 | SKEL_ENV ?= dev
7 |
8 | include config/$(SKEL_ENV).env
9 | export $(shell sed 's/=.*//' config/$(SKEL_ENV).env)
10 |
11 | .PHONY: build migrations
12 | doc-build:
13 | cd doc; aglio -i api.apib --theme-full-width --no-theme-condense -o index.html
14 |
15 | doc-install:
16 | npm install -g aglio drakov dredd
17 |
18 | doc-serve:
19 | cd doc; aglio -i api.apib --theme-full-width --no-theme-condense -s
20 |
21 | doc-mock:
22 | cd doc; drakov -f api.apib -p 4000
23 |
24 | doc-test:
25 | cd doc; dredd api.apib http://localhost:4000
26 |
27 | build:
28 | dep ensure
29 | go build -o ./bin/cli cmd/main.go
30 | go build -o ./bin/api api/main.go
31 |
32 | db:
33 | $(DATABASE_DRIVER) -u$(DATABASE_USER) -p$(DATABASE_PASSWORD) -h $(DATABASE_HOST) -e "create database if not exists $(DATABASE_NAME)"
34 |
35 | new-migration: ## create a new migration, use make new-migration m=message to set the message
36 | sql-migrate new -config=./config/dbconfig.yml -env=production "$(m)"
37 |
38 | migrations:
39 | sql-migrate up -config=config/dbconfig.yml -env=production
40 |
41 | test:
42 | ./cli/go.test.sh
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # go-web-skel
2 | Golang web application skeleton
3 |
--------------------------------------------------------------------------------
/api/handler/company.go:
--------------------------------------------------------------------------------
1 | package handler
2 |
3 | import (
4 | "net/http"
5 |
6 | "github.com/codegangsta/negroni"
7 | "github.com/eminetto/go-web-skel/pkg/company"
8 | "github.com/eminetto/go-web-skel/pkg/middleware"
9 | "github.com/gorilla/mux"
10 | )
11 |
12 | func companyFindAll(service company.Service) http.Handler {
13 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14 | return
15 | })
16 | }
17 |
18 | //MakeCompanyHandlers make url handlers
19 | func MakeCompanyHandlers(r *mux.Router, service company.Service) {
20 | r.Handle("/v1/company", negroni.New(
21 | negroni.HandlerFunc(middleware.Cors),
22 | negroni.HandlerFunc(middleware.IsAuthenticated),
23 | negroni.Wrap(companyFindAll(service)),
24 | )).Methods("GET", "OPTIONS")
25 | }
26 |
--------------------------------------------------------------------------------
/api/handler/user.go:
--------------------------------------------------------------------------------
1 | package handler
2 |
3 | import (
4 | "encoding/json"
5 | "net/http"
6 | "strconv"
7 |
8 | "github.com/codegangsta/negroni"
9 | "github.com/eminetto/go-web-skel/pkg/middleware"
10 | "github.com/eminetto/go-web-skel/pkg/user"
11 | "github.com/gorilla/mux"
12 | )
13 |
14 | func userFindAll(service user.Service) http.Handler {
15 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16 | var data []*user.User
17 | var err error
18 | var dataToJSON []user.ToJSON
19 | data, err = service.FindAll()
20 | if err != nil {
21 | http.Error(w, err.Error(), http.StatusInternalServerError)
22 | return
23 | }
24 | if len(data) == 0 {
25 | http.Error(w, "Not found", http.StatusNotFound)
26 | return
27 | }
28 | for _, j := range data {
29 | d, err := service.ToJSON(j)
30 | if err != nil {
31 | http.Error(w, err.Error(), http.StatusInternalServerError)
32 | return
33 | }
34 | dataToJSON = append(dataToJSON, d)
35 | }
36 | if err := json.NewEncoder(w).Encode(dataToJSON); err != nil {
37 | http.Error(w, err.Error(), http.StatusInternalServerError)
38 | return
39 | }
40 | })
41 | }
42 |
43 | func userFind(service user.Service) http.Handler {
44 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45 | vars := mux.Vars(r)
46 | id, err := strconv.ParseInt(vars["id"], 10, 64)
47 | if err != nil {
48 | http.Error(w, err.Error(), http.StatusInternalServerError)
49 | return
50 | }
51 |
52 | u, err := service.Find(id)
53 | if err != nil || u == nil {
54 | http.Error(w, "User not found", http.StatusNotFound)
55 | return
56 | }
57 | d, err := service.ToJSON(u)
58 | if err != nil {
59 | http.Error(w, err.Error(), http.StatusInternalServerError)
60 | return
61 | }
62 | if err := json.NewEncoder(w).Encode(d); err != nil {
63 | http.Error(w, err.Error(), http.StatusInternalServerError)
64 | return
65 | }
66 | })
67 | }
68 |
69 | func userRemove(service user.Service) http.Handler {
70 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
71 | vars := mux.Vars(r)
72 | id, err := strconv.ParseInt(vars["id"], 10, 64)
73 | if err != nil {
74 | http.Error(w, err.Error(), http.StatusInternalServerError)
75 | return
76 | }
77 |
78 | err = service.Remove(id)
79 | if err != nil {
80 | http.Error(w, "Error removing user", http.StatusInternalServerError)
81 | return
82 | }
83 | w.WriteHeader(http.StatusNoContent)
84 | })
85 | }
86 |
87 | func userAdd(service user.Service) http.Handler {
88 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89 | type parameters struct {
90 | user.User
91 | }
92 | var param parameters
93 | err := json.NewDecoder(r.Body).Decode(¶m)
94 | if err != nil {
95 | http.Error(w, err.Error(), http.StatusInternalServerError)
96 | return
97 | }
98 | userID, err := service.Store(¶m.User)
99 | if err != nil {
100 | http.Error(w, err.Error(), http.StatusInternalServerError)
101 | return
102 | }
103 |
104 | type data struct {
105 | ID int64 `json:"id"`
106 | }
107 | w.WriteHeader(http.StatusCreated)
108 | if err := json.NewEncoder(w).Encode(data{ID: userID}); err != nil {
109 | http.Error(w, err.Error(), http.StatusInternalServerError)
110 | return
111 | }
112 | })
113 | }
114 |
115 | //MakeUserHandlers make url handlers
116 | func MakeUserHandlers(r *mux.Router, service user.Service) {
117 | r.Handle("/v1/user", negroni.New(
118 | negroni.HandlerFunc(middleware.Cors),
119 | negroni.HandlerFunc(middleware.IsAuthenticated),
120 | negroni.Wrap(userFindAll(service)),
121 | )).Methods("GET", "OPTIONS")
122 |
123 | r.Handle("/v1/user", negroni.New(
124 | negroni.HandlerFunc(middleware.Cors),
125 | negroni.Wrap(userAdd(service)),
126 | )).Methods("POST", "OPTIONS")
127 |
128 | r.Handle("/v1/user/{id}", negroni.New(
129 | negroni.HandlerFunc(middleware.Cors),
130 | negroni.HandlerFunc(middleware.IsAuthenticated),
131 | negroni.Wrap(userFind(service)),
132 | )).Methods("GET", "OPTIONS")
133 |
134 | r.Handle("/v1/user/{id}", negroni.New(
135 | negroni.HandlerFunc(middleware.Cors),
136 | negroni.HandlerFunc(middleware.IsAuthenticated),
137 | negroni.Wrap(userRemove(service)),
138 | )).Methods("DELETE", "OPTIONS")
139 | }
140 |
--------------------------------------------------------------------------------
/api/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "database/sql"
5 | "fmt"
6 | "log"
7 | "net/http"
8 |
9 | "os"
10 |
11 | "github.com/eminetto/go-web-skel/api/handler"
12 | "github.com/eminetto/go-web-skel/pkg/company"
13 | "github.com/eminetto/go-web-skel/pkg/mysql"
14 | "github.com/eminetto/go-web-skel/pkg/user"
15 | "github.com/gorilla/context"
16 | "github.com/gorilla/mux"
17 | "github.com/joho/godotenv"
18 | migrate "github.com/rubenv/sql-migrate"
19 | )
20 |
21 | func main() {
22 |
23 | env := os.Getenv("SKEL_ENV")
24 | err := godotenv.Load("config/" + env + ".env")
25 | if err != nil {
26 | log.Fatal("Error loading .env file")
27 | }
28 | dbConf := mysql.DBConfig{
29 | os.Getenv("DATABASE_USER"),
30 | os.Getenv("DATABASE_PASSWORD"),
31 | os.Getenv("DATABASE_NAME"),
32 | os.Getenv("DATABASE_HOST"),
33 | os.Getenv("DATABASE_PORT"),
34 | }
35 | dbConn, err := mysql.InitDb(dbConf)
36 | if err != nil {
37 | log.Printf("Error initializing database: %v\n", err)
38 | }
39 | migrateDatabase(dbConn)
40 | StartServer(dbConn)
41 | }
42 |
43 | func migrateDatabase(dbConn *sql.DB) {
44 | migrations := &migrate.FileMigrationSource{
45 | Dir: "migrations",
46 | }
47 | n, err := migrate.Exec(dbConn, "mysql", migrations, migrate.Up)
48 | if err != nil {
49 | log.Printf("Error applying migrations: %v\n", err)
50 | }
51 | fmt.Printf("Applied %d migrations!\n", n)
52 | }
53 |
54 | //StartServer rotas e handlers
55 | func StartServer(dbConn *sql.DB) {
56 |
57 | r := mux.NewRouter()
58 | //company
59 | cService := company.NewService(dbConn)
60 | handler.MakeCompanyHandlers(r, cService)
61 |
62 | //user
63 | uService := user.NewService(dbConn)
64 | handler.MakeUserHandlers(r, uService)
65 |
66 | http.Handle("/", r)
67 | http.ListenAndServe(":"+os.Getenv("API_PORT"), context.ClearHandler(http.DefaultServeMux))
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/bin/api:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eminetto/go-web-skel/7510acbf8621c7d4b75cc311c905b3a0d24e7191/bin/api
--------------------------------------------------------------------------------
/cli/go.test.sh:
--------------------------------------------------------------------------------
1 | set -e
2 | echo "" > coverage.txt
3 |
4 | for d in $(go list ./... | grep -v vendor); do
5 | go test -coverprofile=profile.out -covermode=atomic $d
6 | if [ -f profile.out ]; then
7 | cat profile.out >> coverage.txt
8 | rm profile.out
9 | fi
10 | done
11 |
--------------------------------------------------------------------------------
/cmd/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import "fmt"
4 |
5 | func main() {
6 | fmt.Println("hello world")
7 | }
8 |
--------------------------------------------------------------------------------
/config/dbconfig.yml:
--------------------------------------------------------------------------------
1 | production:
2 | dialect: mysql
3 | datasource: gowebskel:gowebskel@tcp(127.0.0.1:3306)/gowebskel?parseTime=true
4 | dir: migrations
5 | table: migrations
6 |
--------------------------------------------------------------------------------
/config/dev.env:
--------------------------------------------------------------------------------
1 | DATABASE_DRIVER=mysql
2 | DATABASE_NAME=gowebskel
3 | DATABASE_USER=gowebskel
4 | DATABASE_PASSWORD=gowebskel
5 | DATABASE_HOST=127.0.0.1
6 | DATABASE_PORT=3306
7 | SERVER_PORT=3000
8 | API_PORT=3001
9 |
--------------------------------------------------------------------------------
/config/production.env:
--------------------------------------------------------------------------------
1 | DATABASE_DRIVER=mysql
2 | DATABASE_NAME=gowebskel
3 | DATABASE_USER=gowebskel
4 | DATABASE_PASSWORD=gowebskel
5 | DATABASE_HOST=127.0.0.1
6 | DATABASE_PORT=3306
7 | SERVER_PORT=3000
8 | API_PORT=3001
9 |
--------------------------------------------------------------------------------
/config/testing.env:
--------------------------------------------------------------------------------
1 | DATABASE_DRIVER=mysql
2 | DATABASE_NAME=gowebskel
3 | DATABASE_USER=gowebskel
4 | DATABASE_PASSWORD=gowebskel
5 | DATABASE_HOST=127.0.0.1
6 | DATABASE_PORT=3306
7 | SERVER_PORT=3000
8 | API_PORT=3001
9 |
--------------------------------------------------------------------------------
/coverage.txt:
--------------------------------------------------------------------------------
1 |
2 | mode: atomic
3 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:16.37,22.2 3 0
4 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:24.52,26.16 2 0
5 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:29.2,29.26 1 0
6 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:26.16,28.3 1 0
7 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:32.49,35.16 3 0
8 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:38.2,38.15 1 0
9 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:35.16,37.3 1 0
10 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:41.60,44.16 3 0
11 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:47.2,47.15 1 0
12 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:44.16,46.3 1 0
13 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:50.52,51.15 1 0
14 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:59.2,60.16 2 0
15 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:63.2,63.18 1 0
16 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:51.15,54.17 3 0
17 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:57.3,57.19 1 0
18 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:54.17,56.4 1 0
19 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:60.16,62.3 1 0
20 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:66.42,69.16 3 0
21 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:72.2,73.12 2 0
22 | github.com/eminetto/go-web-skel/pkg/company/mysql.go:69.16,71.3 1 0
23 | mode: atomic
24 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:16.37,22.2 3 0
25 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:24.49,26.16 2 0
26 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:29.2,29.14 1 0
27 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:32.2,32.23 1 0
28 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:26.16,28.3 1 0
29 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:29.14,31.3 1 0
30 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:35.46,38.16 3 0
31 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:41.2,41.15 1 0
32 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:38.16,40.3 1 0
33 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:44.57,47.16 3 0
34 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:50.2,50.15 1 0
35 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:47.16,49.3 1 0
36 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:53.49,54.15 1 0
37 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:62.2,63.16 2 0
38 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:66.2,66.18 1 0
39 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:54.15,57.17 3 0
40 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:60.3,60.19 1 0
41 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:57.17,59.4 1 0
42 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:63.16,65.3 1 0
43 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:69.42,72.16 3 0
44 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:75.2,76.12 2 0
45 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:72.16,74.3 1 0
46 | github.com/eminetto/go-web-skel/pkg/user/mysql.go:79.51,84.2 4 0
47 |
--------------------------------------------------------------------------------
/doc/README.md:
--------------------------------------------------------------------------------
1 | # Documentação da API
2 |
3 | ## Instalar dependências
4 |
5 | make install
6 |
7 | É preciso ter o npm instalado
8 |
9 | ## Gerar a documentação
10 |
11 | make
12 |
13 | ## Gerar servidor de mock
14 |
15 | make mock
16 |
17 | Um servidor vai ficar ouvindo na porta 4000 e servindo os dados conforme a documentação
18 |
--------------------------------------------------------------------------------
/doc/api.apib:
--------------------------------------------------------------------------------
1 | FORMAT: 1A
2 | HOST: http://app.golang.dev/v1
3 |
4 | # API Web App Skel
5 |
6 | API da Code:Nation
7 |
8 | # Group API
9 |
10 | ## Convenções [/conventions]
11 |
12 | ### Valores padrão
13 |
14 | ### Headers padrão
15 |
16 | | Parâmetro | Significado | Valores |
17 | | ------------- | ------------- | --- |
18 | | Accept | Indica para a API o formato que é esperado no retorno | application/json |
19 | | Content-Type | Indica qual é o formato que está sendo usado para enviar os dados | application/json |
20 | | Authorization | Token JWT usado para autenticação | Um token JWT ou vazio |
21 |
22 |
23 | ## Retornos [/returns]
24 |
25 | ### Tipos de retorno esperado
26 |
27 | | HTTP status code | summary |
28 | | ------------- | ------------- |
29 | | 200 - OK | Everything worked as expected. |
30 | | 400 - Bad Request | The request was unacceptable, often due to missing a required parameter. |
31 | | 401 - Unauthorized | No valid API key provided. |
32 | | 402 - Request Failed | The parameters were valid but the request failed. |
33 | | 404 - Not Found | The requested resource doesn't exist. |
34 | | 500, 502, 503, 504 | Server Errors Something went wrong server. |
35 |
36 |
37 | # Group Auth
38 |
39 | ## Login [/auth]
40 |
41 | ### Do login [POST]
42 |
43 | + Request Do login
44 |
45 | + Headers
46 |
47 | Accept: application/json
48 | Content-Type: application/json
49 | + Attributes
50 | + email (string) - E-mail do usuário
51 | + password (string) - Password
52 |
53 | + Response 200 (application/json)
54 | + Attributes
55 | + user_id (number) - Id do usuário
56 | + token (string) - Token JWT do usuário
57 | + refresh_token (string) - Refrech Token do usuário
58 | + redirect_to: /v1/applicant - Próxima página após o login do usuário
59 | + is_admin (boolean) - Is admin?
60 | + company_id (number) - If isn't an admin an user must have a company
61 |
62 | + Response 401 (application/json)
63 | + Attributes (Error)
64 |
65 | # Group User
66 |
67 | ## User [/user]
68 |
69 | ### List users [GET]
70 |
71 | + Request List users - JSON
72 |
73 | + Headers
74 |
75 | Accept: application/json
76 | Content-Type: application/json
77 | Authorization: JWT
78 |
79 | + Response 200 (application/json)
80 | + Attributes (array[User])
81 |
82 | + Response 404 (application/json)
83 | + Attributes (Error)
84 |
85 | + Response 401 (application/json)
86 | + Attributes (Error)
87 |
88 | + Request List users - HTML
89 |
90 | + Headers
91 |
92 | Accept: text/html
93 | Content-Type: application/json
94 | Authorization: JWT
95 |
96 | + Response 200 (text/html)
97 |
98 | + Response 404 (text/html)
99 |
100 | + Response 401 (text/html)
101 |
102 | ### Create user [POST]
103 |
104 | + Request Create user
105 |
106 | + Headers
107 |
108 | Accept: application/json
109 | Content-Type: application/json
110 | Authorization: JWT
111 |
112 | + Attributes (User)
113 |
114 | + Response 201 (application/json)
115 | + Attributes (Created)
116 |
117 |
118 | ## User [/user/{id}]
119 |
120 | + Parameters
121 | + id: 1 (number, required) - User ID
122 |
123 | ### Get user [GET]
124 |
125 | + Request Get user
126 |
127 | + Headers
128 |
129 | Accept: application/json
130 | Content-Type: application/json
131 | Authorization: JWT
132 |
133 | + Response 200 (application/json)
134 | + Attributes (User)
135 |
136 | + Response 404 (application/json)
137 | + Attributes (Error)
138 |
139 | ### Update user [PUT]
140 |
141 | + Request Update user
142 |
143 | + Headers
144 |
145 | Accept: application/json
146 | Content-Type: application/json
147 | Authorization: JWT
148 |
149 | + Attributes (User)
150 |
151 | + Response 200 (application/json)
152 | + Attributes (User)
153 |
154 | + Response 404 (application/json)
155 | + Attributes (Error)
156 |
157 | ### Delet user [DELETE]
158 |
159 | + Request Delete user
160 |
161 | + Headers
162 |
163 | Accept: application/json
164 | Content-Type: application/json
165 | Authorization: JWT
166 |
167 | + Response 200 (application/json)
168 | + Attributes (Created)
169 |
170 | + Response 404 (application/json)
171 | + Attributes (Error)
172 |
173 |
174 | # Group Company
175 |
176 | ## Company [/company]
177 |
178 | ### List companies [GET]
179 |
180 | + Request List companyies - JSON
181 |
182 | + Headers
183 |
184 | Accept: application/json
185 | Content-Type: application/json
186 | Authorization: JWT
187 |
188 | + Response 200 (application/json)
189 | + Attributes (array[Company])
190 |
191 | + Response 404 (application/json)
192 | + Attributes (Error)
193 |
194 | + Response 401 (application/json)
195 | + Attributes (Error)
196 |
197 | + Request List companies - HTML
198 |
199 | + Headers
200 |
201 | Accept: text/html
202 | Content-Type: application/json
203 | Authorization: JWT
204 |
205 | + Response 200 (text/html)
206 |
207 | + Response 404 (text/html)
208 |
209 | + Response 401 (text/html)
210 |
211 | ### Create company [POST]
212 |
213 | + Request Create company
214 |
215 | + Headers
216 |
217 | Accept: application/json
218 | Content-Type: application/json
219 | Authorization: JWT
220 |
221 | + Attributes (Company)
222 |
223 | + Response 201 (application/json)
224 | + Attributes (Created)
225 |
226 |
227 | ## Company [/company/{id}]
228 |
229 | + Parameters
230 | + id: 1 (number, required) - Company ID
231 |
232 | ### Get company [GET]
233 |
234 | + Request Get company
235 |
236 | + Headers
237 |
238 | Accept: application/json
239 | Content-Type: application/json
240 | Authorization: JWT
241 |
242 | + Response 200 (application/json)
243 | + Attributes (Company)
244 |
245 | + Response 404 (application/json)
246 | + Attributes (Error)
247 |
248 | ### Update company [PUT]
249 |
250 | + Request Update company
251 |
252 | + Headers
253 |
254 | Accept: application/json
255 | Content-Type: application/json
256 | Authorization: JWT
257 |
258 | + Attributes (Company)
259 |
260 | + Response 200 (application/json)
261 | + Attributes (Company)
262 |
263 | + Response 404 (application/json)
264 | + Attributes (Error)
265 |
266 | ### Delete company [DELETE]
267 |
268 | + Request Delete company
269 |
270 | + Headers
271 |
272 | Accept: application/json
273 | Content-Type: application/json
274 | Authorization: JWT
275 |
276 | + Response 200 (application/json)
277 | + Attributes (Created)
278 |
279 | + Response 404 (application/json)
280 | + Attributes (Error)
281 |
282 | # Data Structures
283 |
284 | ## Error (object)
285 | + code: 400 (number) - Status code
286 | + message (string) - Mensagem do status
287 |
288 | ## User (object)
289 | + id (number) - Id
290 | + nickname (string) - Profile
291 | + picture (string) -Picture
292 | + email (string) - E-mail
293 | + name (string) - Name
294 | + password (string) - Password
295 |
296 | ## Company (object)
297 | + id (number) - Id
298 | + email (string) - E-mail
299 | + name (string) - Name
300 | + url (string) - Site
301 |
302 | ## Created (object)
303 | + id (number) - id
304 |
--------------------------------------------------------------------------------
/doc/index.html:
--------------------------------------------------------------------------------
1 |
API Web App Skel Back to topAPI da Code:Nation
2 |
API ¶
Convenções ¶
3 |
4 |
5 |
6 |
7 | | Parâmetro |
8 | Significado |
9 | Valores |
10 |
11 |
12 |
13 |
14 | | Accept |
15 | Indica para a API o formato que é esperado no retorno |
16 | application/json |
17 |
18 |
19 | | Content-Type |
20 | Indica qual é o formato que está sendo usado para enviar os dados |
21 | application/json |
22 |
23 |
24 | | Authorization |
25 | Token JWT usado para autenticação |
26 | Um token JWT ou vazio |
27 |
28 |
29 |
30 |
Retornos ¶
31 |
32 |
33 |
34 | | HTTP status code |
35 | summary |
36 |
37 |
38 |
39 |
40 | | 200 - OK |
41 | Everything worked as expected. |
42 |
43 |
44 | | 400 - Bad Request |
45 | The request was unacceptable, often due to missing a required parameter. |
46 |
47 |
48 | | 401 - Unauthorized |
49 | No valid API key provided. |
50 |
51 |
52 | | 402 - Request Failed |
53 | The parameters were valid but the request failed. |
54 |
55 |
56 | | 404 - Not Found |
57 | The requested resource doesn’t exist. |
58 |
59 |
60 | | 500, 502, 503, 504 |
61 | Server Errors Something went wrong server. |
62 |
63 |
64 |
65 |
Auth ¶
Login ¶
Do login
POST/authExample URI
POST http://app.golang.dev/v1/auth
Headers
Accept: application/json
Content-Type: application/json
Body
{
66 | "email": "Hello, world!",
67 | "password": "Hello, world!"
68 | }
Schema
{
69 | "$schema": "http://json-schema.org/draft-04/schema#",
70 | "type": "object",
71 | "properties": {
72 | "email": {
73 | "type": "string",
74 | "description": "E-mail do usuário"
75 | },
76 | "password": {
77 | "type": "string",
78 | "description": "Password"
79 | }
80 | }
81 | }
Headers
Content-Type: application/json
Body
{
82 | "user_id": 1,
83 | "token": "Hello, world!",
84 | "refresh_token": "Hello, world!",
85 | "redirect_to": "/v1/applicant",
86 | "is_admin": true,
87 | "company_id": 1
88 | }
Schema
{
89 | "$schema": "http://json-schema.org/draft-04/schema#",
90 | "type": "object",
91 | "properties": {
92 | "user_id": {
93 | "type": "number",
94 | "description": "Id do usuário"
95 | },
96 | "token": {
97 | "type": "string",
98 | "description": "Token JWT do usuário"
99 | },
100 | "refresh_token": {
101 | "type": "string",
102 | "description": "Refrech Token do usuário"
103 | },
104 | "redirect_to": {
105 | "type": "string",
106 | "description": "Próxima página após o login do usuário"
107 | },
108 | "is_admin": {
109 | "type": "boolean",
110 | "description": "Is admin?"
111 | },
112 | "company_id": {
113 | "type": "number",
114 | "description": "If isn't an admin an user must have a company"
115 | }
116 | }
117 | }
Headers
Content-Type: application/json
Body
{
118 | "code": 400,
119 | "message": "Hello, world!"
120 | }
Schema
{
121 | "$schema": "http://json-schema.org/draft-04/schema#",
122 | "type": "object",
123 | "properties": {
124 | "code": {
125 | "type": "number",
126 | "description": "Status code"
127 | },
128 | "message": {
129 | "type": "string",
130 | "description": "Mensagem do status"
131 | }
132 | }
133 | }
User ¶
User ¶
List users
GET/userExample URI
GET http://app.golang.dev/v1/user
Request List users - JSONHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
[
134 | {
135 | "id": 1,
136 | "nickname": "Hello, world!",
137 | "picture": "Hello, world!",
138 | "email": "Hello, world!",
139 | "name": "Hello, world!",
140 | "password": "Hello, world!"
141 | }
142 | ]
Schema
{
143 | "$schema": "http://json-schema.org/draft-04/schema#",
144 | "type": "array"
145 | }
Headers
Content-Type: application/json
Body
{
146 | "code": 400,
147 | "message": "Hello, world!"
148 | }
Schema
{
149 | "$schema": "http://json-schema.org/draft-04/schema#",
150 | "type": "object",
151 | "properties": {
152 | "code": {
153 | "type": "number",
154 | "description": "Status code"
155 | },
156 | "message": {
157 | "type": "string",
158 | "description": "Mensagem do status"
159 | }
160 | }
161 | }
Headers
Content-Type: application/json
Body
{
162 | "code": 400,
163 | "message": "Hello, world!"
164 | }
Schema
{
165 | "$schema": "http://json-schema.org/draft-04/schema#",
166 | "type": "object",
167 | "properties": {
168 | "code": {
169 | "type": "number",
170 | "description": "Status code"
171 | },
172 | "message": {
173 | "type": "string",
174 | "description": "Mensagem do status"
175 | }
176 | }
177 | }
Request List users - HTMLHideShow
Headers
Accept: text/html
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: text/html
Headers
Content-Type: text/html
Headers
Content-Type: text/html
Create user
POST/userExample URI
POST http://app.golang.dev/v1/user
Request Create userHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Body
{
178 | "id": 1,
179 | "nickname": "Hello, world!",
180 | "picture": "Hello, world!",
181 | "email": "Hello, world!",
182 | "name": "Hello, world!",
183 | "password": "Hello, world!"
184 | }
Schema
{
185 | "$schema": "http://json-schema.org/draft-04/schema#",
186 | "type": "object",
187 | "properties": {
188 | "id": {
189 | "type": "number",
190 | "description": "Id"
191 | },
192 | "nickname": {
193 | "type": "string",
194 | "description": "Profile"
195 | },
196 | "picture": {
197 | "type": "string",
198 | "description": "Picture"
199 | },
200 | "email": {
201 | "type": "string",
202 | "description": "E-mail"
203 | },
204 | "name": {
205 | "type": "string",
206 | "description": "Name"
207 | },
208 | "password": {
209 | "type": "string",
210 | "description": "Password"
211 | }
212 | }
213 | }
Headers
Content-Type: application/json
Body
{
214 | "id": 1
215 | }
Schema
{
216 | "$schema": "http://json-schema.org/draft-04/schema#",
217 | "type": "object",
218 | "properties": {
219 | "id": {
220 | "type": "number",
221 | "description": "id"
222 | }
223 | }
224 | }
User ¶
Get user
GET/user/{id}Example URI
GET http://app.golang.dev/v1/user/1
- id
number (required) Example: 1User ID
225 |
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
{
226 | "id": 1,
227 | "nickname": "Hello, world!",
228 | "picture": "Hello, world!",
229 | "email": "Hello, world!",
230 | "name": "Hello, world!",
231 | "password": "Hello, world!"
232 | }
Schema
{
233 | "$schema": "http://json-schema.org/draft-04/schema#",
234 | "type": "object",
235 | "properties": {
236 | "id": {
237 | "type": "number",
238 | "description": "Id"
239 | },
240 | "nickname": {
241 | "type": "string",
242 | "description": "Profile"
243 | },
244 | "picture": {
245 | "type": "string",
246 | "description": "Picture"
247 | },
248 | "email": {
249 | "type": "string",
250 | "description": "E-mail"
251 | },
252 | "name": {
253 | "type": "string",
254 | "description": "Name"
255 | },
256 | "password": {
257 | "type": "string",
258 | "description": "Password"
259 | }
260 | }
261 | }
Headers
Content-Type: application/json
Body
{
262 | "code": 400,
263 | "message": "Hello, world!"
264 | }
Schema
{
265 | "$schema": "http://json-schema.org/draft-04/schema#",
266 | "type": "object",
267 | "properties": {
268 | "code": {
269 | "type": "number",
270 | "description": "Status code"
271 | },
272 | "message": {
273 | "type": "string",
274 | "description": "Mensagem do status"
275 | }
276 | }
277 | }
Update user
PUT/user/{id}Example URI
PUT http://app.golang.dev/v1/user/1
- id
number (required) Example: 1User ID
278 |
Request Update userHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Body
{
279 | "id": 1,
280 | "nickname": "Hello, world!",
281 | "picture": "Hello, world!",
282 | "email": "Hello, world!",
283 | "name": "Hello, world!",
284 | "password": "Hello, world!"
285 | }
Schema
{
286 | "$schema": "http://json-schema.org/draft-04/schema#",
287 | "type": "object",
288 | "properties": {
289 | "id": {
290 | "type": "number",
291 | "description": "Id"
292 | },
293 | "nickname": {
294 | "type": "string",
295 | "description": "Profile"
296 | },
297 | "picture": {
298 | "type": "string",
299 | "description": "Picture"
300 | },
301 | "email": {
302 | "type": "string",
303 | "description": "E-mail"
304 | },
305 | "name": {
306 | "type": "string",
307 | "description": "Name"
308 | },
309 | "password": {
310 | "type": "string",
311 | "description": "Password"
312 | }
313 | }
314 | }
Headers
Content-Type: application/json
Body
{
315 | "id": 1,
316 | "nickname": "Hello, world!",
317 | "picture": "Hello, world!",
318 | "email": "Hello, world!",
319 | "name": "Hello, world!",
320 | "password": "Hello, world!"
321 | }
Schema
{
322 | "$schema": "http://json-schema.org/draft-04/schema#",
323 | "type": "object",
324 | "properties": {
325 | "id": {
326 | "type": "number",
327 | "description": "Id"
328 | },
329 | "nickname": {
330 | "type": "string",
331 | "description": "Profile"
332 | },
333 | "picture": {
334 | "type": "string",
335 | "description": "Picture"
336 | },
337 | "email": {
338 | "type": "string",
339 | "description": "E-mail"
340 | },
341 | "name": {
342 | "type": "string",
343 | "description": "Name"
344 | },
345 | "password": {
346 | "type": "string",
347 | "description": "Password"
348 | }
349 | }
350 | }
Headers
Content-Type: application/json
Body
{
351 | "code": 400,
352 | "message": "Hello, world!"
353 | }
Schema
{
354 | "$schema": "http://json-schema.org/draft-04/schema#",
355 | "type": "object",
356 | "properties": {
357 | "code": {
358 | "type": "number",
359 | "description": "Status code"
360 | },
361 | "message": {
362 | "type": "string",
363 | "description": "Mensagem do status"
364 | }
365 | }
366 | }
Delet user
DELETE/user/{id}Example URI
DELETE http://app.golang.dev/v1/user/1
- id
number (required) Example: 1User ID
367 |
Request Delete userHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
{
368 | "id": 1
369 | }
Schema
{
370 | "$schema": "http://json-schema.org/draft-04/schema#",
371 | "type": "object",
372 | "properties": {
373 | "id": {
374 | "type": "number",
375 | "description": "id"
376 | }
377 | }
378 | }
Headers
Content-Type: application/json
Body
{
379 | "code": 400,
380 | "message": "Hello, world!"
381 | }
Schema
{
382 | "$schema": "http://json-schema.org/draft-04/schema#",
383 | "type": "object",
384 | "properties": {
385 | "code": {
386 | "type": "number",
387 | "description": "Status code"
388 | },
389 | "message": {
390 | "type": "string",
391 | "description": "Mensagem do status"
392 | }
393 | }
394 | }
Company ¶
Company ¶
List companies
GET/companyExample URI
GET http://app.golang.dev/v1/company
Request List companyies - JSONHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
[
395 | {
396 | "id": 1,
397 | "email": "Hello, world!",
398 | "name": "Hello, world!",
399 | "url": "Hello, world!"
400 | }
401 | ]
Schema
{
402 | "$schema": "http://json-schema.org/draft-04/schema#",
403 | "type": "array"
404 | }
Headers
Content-Type: application/json
Body
{
405 | "code": 400,
406 | "message": "Hello, world!"
407 | }
Schema
{
408 | "$schema": "http://json-schema.org/draft-04/schema#",
409 | "type": "object",
410 | "properties": {
411 | "code": {
412 | "type": "number",
413 | "description": "Status code"
414 | },
415 | "message": {
416 | "type": "string",
417 | "description": "Mensagem do status"
418 | }
419 | }
420 | }
Headers
Content-Type: application/json
Body
{
421 | "code": 400,
422 | "message": "Hello, world!"
423 | }
Schema
{
424 | "$schema": "http://json-schema.org/draft-04/schema#",
425 | "type": "object",
426 | "properties": {
427 | "code": {
428 | "type": "number",
429 | "description": "Status code"
430 | },
431 | "message": {
432 | "type": "string",
433 | "description": "Mensagem do status"
434 | }
435 | }
436 | }
Request List companies - HTMLHideShow
Headers
Accept: text/html
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: text/html
Headers
Content-Type: text/html
Headers
Content-Type: text/html
Create company
POST/companyExample URI
POST http://app.golang.dev/v1/company
Request Create companyHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Body
{
437 | "id": 1,
438 | "email": "Hello, world!",
439 | "name": "Hello, world!",
440 | "url": "Hello, world!"
441 | }
Schema
{
442 | "$schema": "http://json-schema.org/draft-04/schema#",
443 | "type": "object",
444 | "properties": {
445 | "id": {
446 | "type": "number",
447 | "description": "Id"
448 | },
449 | "email": {
450 | "type": "string",
451 | "description": "E-mail"
452 | },
453 | "name": {
454 | "type": "string",
455 | "description": "Name"
456 | },
457 | "url": {
458 | "type": "string",
459 | "description": "Site"
460 | }
461 | }
462 | }
Headers
Content-Type: application/json
Body
{
463 | "id": 1
464 | }
Schema
{
465 | "$schema": "http://json-schema.org/draft-04/schema#",
466 | "type": "object",
467 | "properties": {
468 | "id": {
469 | "type": "number",
470 | "description": "id"
471 | }
472 | }
473 | }
Company ¶
Get company
GET/company/{id}Example URI
GET http://app.golang.dev/v1/company/1
- id
number (required) Example: 1Company ID
474 |
Request Get companyHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
{
475 | "id": 1,
476 | "email": "Hello, world!",
477 | "name": "Hello, world!",
478 | "url": "Hello, world!"
479 | }
Schema
{
480 | "$schema": "http://json-schema.org/draft-04/schema#",
481 | "type": "object",
482 | "properties": {
483 | "id": {
484 | "type": "number",
485 | "description": "Id"
486 | },
487 | "email": {
488 | "type": "string",
489 | "description": "E-mail"
490 | },
491 | "name": {
492 | "type": "string",
493 | "description": "Name"
494 | },
495 | "url": {
496 | "type": "string",
497 | "description": "Site"
498 | }
499 | }
500 | }
Headers
Content-Type: application/json
Body
{
501 | "code": 400,
502 | "message": "Hello, world!"
503 | }
Schema
{
504 | "$schema": "http://json-schema.org/draft-04/schema#",
505 | "type": "object",
506 | "properties": {
507 | "code": {
508 | "type": "number",
509 | "description": "Status code"
510 | },
511 | "message": {
512 | "type": "string",
513 | "description": "Mensagem do status"
514 | }
515 | }
516 | }
Update company
PUT/company/{id}Example URI
PUT http://app.golang.dev/v1/company/1
- id
number (required) Example: 1Company ID
517 |
Request Update companyHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Body
{
518 | "id": 1,
519 | "email": "Hello, world!",
520 | "name": "Hello, world!",
521 | "url": "Hello, world!"
522 | }
Schema
{
523 | "$schema": "http://json-schema.org/draft-04/schema#",
524 | "type": "object",
525 | "properties": {
526 | "id": {
527 | "type": "number",
528 | "description": "Id"
529 | },
530 | "email": {
531 | "type": "string",
532 | "description": "E-mail"
533 | },
534 | "name": {
535 | "type": "string",
536 | "description": "Name"
537 | },
538 | "url": {
539 | "type": "string",
540 | "description": "Site"
541 | }
542 | }
543 | }
Headers
Content-Type: application/json
Body
{
544 | "id": 1,
545 | "email": "Hello, world!",
546 | "name": "Hello, world!",
547 | "url": "Hello, world!"
548 | }
Schema
{
549 | "$schema": "http://json-schema.org/draft-04/schema#",
550 | "type": "object",
551 | "properties": {
552 | "id": {
553 | "type": "number",
554 | "description": "Id"
555 | },
556 | "email": {
557 | "type": "string",
558 | "description": "E-mail"
559 | },
560 | "name": {
561 | "type": "string",
562 | "description": "Name"
563 | },
564 | "url": {
565 | "type": "string",
566 | "description": "Site"
567 | }
568 | }
569 | }
Headers
Content-Type: application/json
Body
{
570 | "code": 400,
571 | "message": "Hello, world!"
572 | }
Schema
{
573 | "$schema": "http://json-schema.org/draft-04/schema#",
574 | "type": "object",
575 | "properties": {
576 | "code": {
577 | "type": "number",
578 | "description": "Status code"
579 | },
580 | "message": {
581 | "type": "string",
582 | "description": "Mensagem do status"
583 | }
584 | }
585 | }
Delete company
DELETE/company/{id}Example URI
DELETE http://app.golang.dev/v1/company/1
- id
number (required) Example: 1Company ID
586 |
Request Delete companyHideShow
Headers
Accept: application/json
Content-Type: application/json
Authorization: JWT
Headers
Content-Type: application/json
Body
{
587 | "id": 1
588 | }
Schema
{
589 | "$schema": "http://json-schema.org/draft-04/schema#",
590 | "type": "object",
591 | "properties": {
592 | "id": {
593 | "type": "number",
594 | "description": "id"
595 | }
596 | }
597 | }
Headers
Content-Type: application/json
Body
{
598 | "code": 400,
599 | "message": "Hello, world!"
600 | }
Schema
{
601 | "$schema": "http://json-schema.org/draft-04/schema#",
602 | "type": "object",
603 | "properties": {
604 | "code": {
605 | "type": "number",
606 | "description": "Status code"
607 | },
608 | "message": {
609 | "type": "string",
610 | "description": "Mensagem do status"
611 | }
612 | }
613 | }
Generated by aglio on 03 Nov 2017
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3"
2 | services:
3 | mysql:
4 | environment:
5 | DATABASE_DRIVER:
6 | DATABASE_NAME:
7 | DATABASE_USER:
8 | DATABASE_PASSWORD:
9 | DATABASE_HOST:
10 | DATABASE_PORT:
11 | image: mysql
12 | ports:
13 | - "3306:3306"
14 | environment:
15 | - MYSQL_ROOT_PASSWORD=root
16 | - MYSQL_DATABASE=gowebskel
17 | - MYSQL_USER=gowebskel
18 | - MYSQL_PASSWORD=gowebskel
19 |
--------------------------------------------------------------------------------
/migrations/20171103095219-user.sql:
--------------------------------------------------------------------------------
1 |
2 | -- +migrate Up
3 | CREATE TABLE `user` (
4 | `id` int(11) NOT NULL AUTO_INCREMENT,
5 | `name` varchar(100) NOT NULL,
6 | `picture` varchar(100) NOT NULL,
7 | `email` varchar(255) NOT NULL,
8 | `password` varchar(255) NOT NULL,
9 | `created_at` datetime not NULL,
10 | PRIMARY KEY (`id`)
11 | ) ENGINE=InnoDB;
12 | -- +migrate Down
13 |
--------------------------------------------------------------------------------
/migrations/20171103095223-company.sql:
--------------------------------------------------------------------------------
1 |
2 | -- +migrate Up
3 | CREATE TABLE `company` (
4 | `id` int(11) NOT NULL AUTO_INCREMENT,
5 | `name` varchar(100) NOT NULL,
6 | `email` varchar(255) NOT NULL,
7 | `url` varchar(255) NOT NULL,
8 | `created_at` datetime not NULL,
9 | PRIMARY KEY (`id`)
10 | ) ENGINE=InnoDB;
11 | -- +migrate Down
12 |
--------------------------------------------------------------------------------
/pkg/company/company.go:
--------------------------------------------------------------------------------
1 | package company
2 |
3 | import "time"
4 |
5 | //Company dados da empresa
6 | type Company struct {
7 | ID int64 `valid:"-" json:"id" db:"id"`
8 | Name string `valid:"required" json:"name" db:"name"`
9 | Email string `valid:"email,required" json:"email" db:"email"`
10 | URL string `valid:"url,required" json:"url" db:"url"`
11 | CreatedAt time.Time `valid:"-" db:"created_at" json:"created_at"`
12 | }
13 |
14 | //Service service interface
15 | type Service interface {
16 | Find(id int64) (*Company, error)
17 | Search(query string) ([]*Company, error)
18 | FindAll() ([]*Company, error)
19 | Remove(ID int64) error
20 | Store(company *Company) (int64, error)
21 | }
22 |
--------------------------------------------------------------------------------
/pkg/company/company_test.go:
--------------------------------------------------------------------------------
1 | package company
2 |
3 | import (
4 | valid "github.com/asaskevich/govalidator"
5 | "testing"
6 | )
7 | func TestCompanyValidation(t *testing.T) {
8 | c := Company{
9 | Email: "eminetto@email.com",
10 | Name: "Big Co",
11 | URL: "http://bigco.com",
12 | }
13 | _, err := valid.ValidateStruct(c)
14 | if err != nil {
15 | t.Errorf("expected %s result %s", nil, err)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/pkg/company/mysql.go:
--------------------------------------------------------------------------------
1 | package company
2 |
3 | import (
4 | "database/sql"
5 | "errors"
6 | "time"
7 |
8 | gorp "gopkg.in/gorp.v1"
9 | )
10 |
11 | type service struct {
12 | dbmap *gorp.DbMap
13 | }
14 |
15 | //NewService create new service
16 | func NewService(db *sql.DB) Service {
17 | dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
18 | dbmap.AddTableWithName(Company{}, "company").SetKeys(true, "id")
19 | return &service{
20 | dbmap: dbmap,
21 | }
22 | }
23 |
24 | func (s *service) Find(id int64) (*Company, error) {
25 | d, err := s.dbmap.Get(Company{}, id)
26 | if err != nil {
27 | return nil, err
28 | }
29 | return d.(*Company), nil
30 | }
31 |
32 | func (s *service) FindAll() ([]*Company, error) {
33 | var d []*Company
34 | _, err := s.dbmap.Select(&d, "select * from company order by id")
35 | if err != nil {
36 | return nil, err
37 | }
38 | return d, nil
39 | }
40 |
41 | func (s *service) Search(query string) ([]*Company, error) {
42 | var d []*Company
43 | _, err := s.dbmap.Select(&d, "select * from company where name like ? order by name", "%"+query+"%")
44 | if err != nil {
45 | return nil, err
46 | }
47 | return d, nil
48 | }
49 |
50 | func (s *service) Store(p *Company) (int64, error) {
51 | if p.ID == 0 {
52 | p.CreatedAt = time.Now()
53 | err := s.dbmap.Insert(p)
54 | if err != nil {
55 | return 0, err
56 | }
57 | return p.ID, nil
58 | }
59 | _, err := s.dbmap.Update(p)
60 | if err != nil {
61 | return 0, err
62 | }
63 | return p.ID, nil
64 | }
65 |
66 | func (s *service) Remove(ID int64) error {
67 | var err error
68 | company, err := s.Find(ID)
69 | if err != nil {
70 | return errors.New("Error reading company")
71 | }
72 | _, err = s.dbmap.Delete(company)
73 | return err
74 | }
75 |
--------------------------------------------------------------------------------
/pkg/middleware/Cors.go:
--------------------------------------------------------------------------------
1 | package middleware
2 |
3 | import (
4 | "net/http"
5 | )
6 |
7 | //Cors adiciona os headers para suportar o CORS nos navegadores
8 | func Cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
9 | w.Header().Set("Access-Control-Allow-Origin", "*")
10 | w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
11 | w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type")
12 | w.Header().Set("Content-Type", "application/json")
13 | if r.Method == "OPTIONS" {
14 | return
15 | }
16 | next(w, r)
17 | }
18 |
--------------------------------------------------------------------------------
/pkg/middleware/isAuthenticated.go:
--------------------------------------------------------------------------------
1 | package middleware
2 |
3 | import (
4 | "errors"
5 | "net/http"
6 | )
7 |
8 | //IsAuthenticated verifica se o usuário tem uma sessão
9 | func IsAuthenticated(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
10 | tokenString := r.Header.Get("Authorization")
11 | if tokenString == "" {
12 | err := errors.New("Unauthorized")
13 | http.Error(w, err.Error(), http.StatusUnauthorized)
14 | return
15 | }
16 | next(w, r)
17 | }
18 |
--------------------------------------------------------------------------------
/pkg/mysql/mysql.go:
--------------------------------------------------------------------------------
1 | package mysql
2 |
3 | import (
4 | "database/sql"
5 |
6 | "fmt"
7 | "os"
8 | //blank import required by sql
9 | _ "github.com/go-sql-driver/mysql"
10 | )
11 |
12 | type mDB struct {
13 | db *sql.DB
14 | }
15 |
16 | //DBConfig database config
17 | type DBConfig struct {
18 | User string
19 | Passwd string
20 | DBName string
21 | DBHost string
22 | DBPort string
23 | }
24 |
25 | //InitDb faz a conexão com o banco
26 | func InitDb(dbConfig DBConfig) (*sql.DB, error) {
27 | dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", dbConfig.User, dbConfig.Passwd, os.Getenv("DATABASE_HOST"), os.Getenv("DATABASE_PORT"), dbConfig.DBName)
28 | db, err := sql.Open("mysql", dsn)
29 | if err != nil {
30 | return nil, err
31 | }
32 | err = db.Ping()
33 | if err != nil {
34 | return nil, err
35 | }
36 | return db, nil
37 | }
38 |
--------------------------------------------------------------------------------
/pkg/user/mysql.go:
--------------------------------------------------------------------------------
1 | package user
2 |
3 | import (
4 | "database/sql"
5 | "errors"
6 | "time"
7 |
8 | gorp "gopkg.in/gorp.v1"
9 | )
10 |
11 | type service struct {
12 | dbmap *gorp.DbMap
13 | }
14 |
15 | //NewService create new service
16 | func NewService(db *sql.DB) Service {
17 | dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
18 | dbmap.AddTableWithName(User{}, "user").SetKeys(true, "id")
19 | return &service{
20 | dbmap: dbmap,
21 | }
22 | }
23 |
24 | func (s *service) Find(id int64) (*User, error) {
25 | d, err := s.dbmap.Get(User{}, id)
26 | if err != nil {
27 | return nil, err
28 | }
29 | if d == nil {
30 | return nil, nil
31 | }
32 | return d.(*User), nil
33 | }
34 |
35 | func (s *service) FindAll() ([]*User, error) {
36 | var d []*User
37 | _, err := s.dbmap.Select(&d, "select * from user order by id")
38 | if err != nil {
39 | return nil, err
40 | }
41 | return d, nil
42 | }
43 |
44 | func (s *service) Search(query string) ([]*User, error) {
45 | var d []*User
46 | _, err := s.dbmap.Select(&d, "select * from user where name like ? order by name", "%"+query+"%")
47 | if err != nil {
48 | return nil, err
49 | }
50 | return d, nil
51 | }
52 |
53 | func (s *service) Store(p *User) (int64, error) {
54 | if p.ID == 0 {
55 | p.CreatedAt = time.Now()
56 | err := s.dbmap.Insert(p)
57 | if err != nil {
58 | return 0, err
59 | }
60 | return p.ID, nil
61 | }
62 | _, err := s.dbmap.Update(p)
63 | if err != nil {
64 | return 0, err
65 | }
66 | return p.ID, nil
67 | }
68 |
69 | func (s *service) Remove(ID int64) error {
70 | var err error
71 | user, err := s.Find(ID)
72 | if err != nil {
73 | return errors.New("Error reading user")
74 | }
75 | _, err = s.dbmap.Delete(user)
76 | return err
77 | }
78 |
79 | func (s *service) ToJSON(u *User) (ToJSON, error) {
80 | var d ToJSON
81 | d.User = u
82 | d.Type = "Regular User"
83 | return d, nil
84 | }
85 |
--------------------------------------------------------------------------------
/pkg/user/user.go:
--------------------------------------------------------------------------------
1 | package user
2 |
3 | import "time"
4 |
5 | //User dados dos usuários que podem logar no sistema
6 | type User struct {
7 | ID int64 `valid:"-" json:"id" db:"id"`
8 | Name string `valid:"required" json:"name" db:"name"`
9 | Picture string `valid:"url,optional" json:"picture" db:"picture"`
10 | Email string `valid:"email,required" json:"email" db:"email"`
11 | Password string `valid:"required" json:"password" db:"password"`
12 | CreatedAt time.Time `valid:"-" db:"created_at" json:"created_at"`
13 | }
14 |
15 | //ToJSON extra information used when translating to json
16 | type ToJSON struct {
17 | *User
18 | Type string `json:"type"`
19 | }
20 |
21 | //Service service interface
22 | type Service interface {
23 | Find(id int64) (*User, error)
24 | Search(query string) ([]*User, error)
25 | FindAll() ([]*User, error)
26 | Remove(ID int64) error
27 | Store(user *User) (int64, error)
28 | ToJSON(u *User) (ToJSON, error)
29 | }
30 |
--------------------------------------------------------------------------------
/pkg/user/user_test.go:
--------------------------------------------------------------------------------
1 | package user
2 |
3 | import (
4 | valid "github.com/asaskevich/govalidator"
5 | "testing"
6 | )
7 |
8 | func TestUserValidation(t *testing.T) {
9 | u := User{
10 | Picture: "https://avatars0.githubusercontent.com/u/19712121939?:3",
11 | Email: "eminetto@email.com",
12 | Name: "Elton Minetto",
13 | Password: "sfsdfdsdsf",
14 | }
15 | _, err := valid.ValidateStruct(u)
16 | if err != nil {
17 | t.Errorf("expected %s result %s", nil, err)
18 | }
19 | }
20 |
21 |
--------------------------------------------------------------------------------