-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (38 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"TestApi/Services"
_ "TestApi/docs"
"database/sql"
"fmt"
"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
"log"
"net/http"
"os"
)
// @title Test API
// @version 1.0
// @description This is a sample API for testing Swagger integration
// @host localhost:5555
// @BasePath /
func main() {
var connStr = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_USER"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_NAME"))
db, err := sql.Open("postgres", connStr)
router := mux.NewRouter()
if err != nil {
log.Printf("Error while connecting to DB: %v", err)
}
_, _ = db.Exec("create table if not exists users (name varchar(255) not null, surname varchar(255), email varchar(255) not null, id serial constraint users_pk primary key ); ")
router.HandleFunc("/user/all", Services.GetAllUsers).Methods("GET")
router.HandleFunc("/user/getById/{id}", Services.GetUserById).Methods("GET")
router.HandleFunc("/user/create", Services.CreateUser).Methods("POST")
router.HandleFunc("/user/delete/{id}", Services.RemoveUserById).Methods("DELETE")
router.HandleFunc("/user/update", Services.UpdateUserById).Methods("PATCH")
router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
_ = http.ListenAndServe(":8080", router)
}