-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (47 loc) · 1.51 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gotodo/controllers/authcontroller"
"github.com/gotodo/controllers/todocontroller"
"github.com/gotodo/middlewares"
"github.com/gotodo/models"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
)
var (
PORT string
)
const (
TODOS = "/todos"
TODOS_ID = "/todos/{id}"
BATCH_TODOS = "/batch/todos"
)
func init() {
PORT = os.Getenv("PORT")
if PORT == "" {
PORT = "8080"
}
log.SetFormatter(&log.TextFormatter{})
log.SetReportCaller(true)
}
func main() {
models.ConnectPostgreSQL()
log.Info("GoTodo API is up and running!")
router := mux.NewRouter()
router.HandleFunc("/", todocontroller.Home)
router.HandleFunc("/api/v1/login", authcontroller.Login).Methods(http.MethodPost)
router.HandleFunc("/api/v1/register", authcontroller.Register).Methods(http.MethodPost)
api := router.PathPrefix("/api/v1").Subrouter()
api.Use(middlewares.JWTMiddleware)
api.HandleFunc(TODOS, todocontroller.AddTodoItem).Methods(http.MethodPost)
api.HandleFunc(TODOS, todocontroller.GetAllTodos).Methods(http.MethodGet)
api.HandleFunc(TODOS_ID, todocontroller.DeleteTodoById).Methods(http.MethodDelete)
api.HandleFunc(TODOS_ID, todocontroller.UpdateTodoById).Methods(http.MethodPut)
api.HandleFunc(BATCH_TODOS, todocontroller.BatchDeleteByIds).Methods(http.MethodDelete)
handler := cors.AllowAll().Handler(router)
fmt.Printf("Server up and running on port %s\n", PORT)
http.ListenAndServe(fmt.Sprintf("127.0.0.1:%s", PORT), handler)
}