-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
57 lines (45 loc) · 1.59 KB
/
server.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
package main
import (
"log"
"net/http"
"time"
"BlogsAPI/db"
"BlogsAPI/handlers"
"BlogsAPI/middlewares"
"BlogsAPI/views"
"github.com/gorilla/mux"
)
func main() {
db.DBCon, _ = db.CreateDatabase() // initialising the database
r := mux.NewRouter().StrictSlash(true)
// Serving Static files
fs := http.FileServer(http.Dir("./static"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
// Views
r.HandleFunc("/", views.BlogsView).Methods("GET")
r.HandleFunc("/b/{id}", views.BlogView).Methods("GET")
// API
r.HandleFunc("/admin", handlers.AuthHandler) // GET /admin -u admin:password
r.HandleFunc("/login", handlers.Login).Methods("POST") // POST /login
r.HandleFunc("/register", handlers.Register).Methods("POST") // POST /register
r.HandleFunc("/blog/{id}", handlers.GetBlog).Methods("GET") // GET /blog/<id>
r.HandleFunc("/blogs", handlers.GetAllBlogs).Methods("GET") // GET /blogs
// Auth routes
r.Handle("/blog/{id}", middlewares.AuthMiddleware(
http.HandlerFunc(handlers.UpdateBlog),
)).Methods("PATCH") // DELETE /blog/<id> Auth: Bearer <Token>
r.Handle("/blog/{id}", middlewares.AuthMiddleware(
http.HandlerFunc(handlers.DeleteBlog),
)).Methods("DELETE") // DELETE /blog/<id> Auth: Bearer <Token>
r.Handle("/blogs", middlewares.AuthMiddleware(
http.HandlerFunc(handlers.CreateBlog),
)).Methods("POST") // POST /blogs Auth: Bearer <Token>
srv := &http.Server{
Addr: ":8080",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
Handler: r,
}
log.Fatal(srv.ListenAndServe())
}