-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (48 loc) · 1.84 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
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/raulcv/goapiws/handlers"
"github.com/raulcv/goapiws/middleware"
"github.com/raulcv/goapiws/server"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error Loading .env file variables")
}
SERVER_HOST := os.Getenv("SERVER_HOST")
SERVER_PORT := os.Getenv("SERVER_PORT")
JWT_SECRET_KEY := os.Getenv("JWT_SECRET_KEY")
DATABASE_URL := os.Getenv("DATABASE_URL_SUPA")
s, err := server.NewServer(context.Background(), &server.Config{
Host: SERVER_HOST,
Port: SERVER_PORT,
JWTSecret: JWT_SECRET_KEY,
DatabaseUrl: DATABASE_URL,
})
if err != nil {
log.Fatalf("Error creating server %v\n", err)
}
s.StartServer(BindRoutes)
}
func BindRoutes(s server.Server, r *mux.Router) {
api := r.PathPrefix("/api/v1").Subrouter()
api.Use(middleware.CheckAuthMiddleware(s))
r.HandleFunc("/", handlers.HomeHandler(s)).Methods(http.MethodGet)
r.HandleFunc("/signup", handlers.SignUpHandler(s)).Methods(http.MethodPost)
r.HandleFunc("/login", handlers.LoginHandler(s)).Methods(http.MethodPost)
api.HandleFunc("/me", handlers.MeHandler(s)).Methods(http.MethodGet)
api.HandleFunc("/posts", handlers.AddPostHandler(s)).Methods(http.MethodPost)
r.HandleFunc("/posts/{id}", handlers.GetPostHandler(s)).Methods(http.MethodGet)
api.HandleFunc("/posts/{id}", handlers.UpdatePostHandler(s)).Methods(http.MethodPut)
api.HandleFunc("/posts/{id}", handlers.DeletePostHandler(s)).Methods(http.MethodDelete)
api.HandleFunc("/posts/{id}", handlers.ActivatePostHandler(s)).Methods(http.MethodPatch)
r.HandleFunc("/posts", handlers.ListPostHandler(s)).Methods(http.MethodGet)
r.HandleFunc("/postsall", handlers.ListPostTwoHandler(s)).Methods(http.MethodGet)
r.HandleFunc("/websoscket", s.Hub().HandleWebSocket)
}