-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (63 loc) · 3.22 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/rubhiauliatirta/rublog/config"
"github.com/rubhiauliatirta/rublog/controllers"
"github.com/rubhiauliatirta/rublog/middlewares"
"github.com/rubhiauliatirta/rublog/models"
)
var PORT string
func init() {
value := os.Getenv("PORT")
if value == "" {
PORT = "9090"
} else {
PORT = value
}
fmt.Println(PORT, "ini portttt")
}
func main() {
router := setRouter()
config.Db.AutoMigrate(&models.User{})
config.Db.AutoMigrate(&models.Article{})
server := &http.Server{
Handler: middlewares.SessionContext(router),
Addr: ":" + PORT,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(server.ListenAndServe())
}
func setRouter() *mux.Router {
var dir string
flag.StringVar(&dir, "dir", "./static", "the directory to serve files from. Defaults to the current dir")
flag.Parse()
router := mux.NewRouter()
router.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir(dir))))
router.HandleFunc("/login", controllers.LoginPage).Methods("GET")
router.HandleFunc("/login", controllers.Login).Methods("POST")
router.HandleFunc("/logout", controllers.Logout).Methods("GET")
router.HandleFunc("/register", controllers.RegisterPage).Methods("GET")
router.HandleFunc("/register", controllers.Register).Methods("POST")
router.Handle("/articles/delete/{id}", middlewares.StrictAuthentication(middlewares.ArticleAuthorization(http.HandlerFunc(controllers.ArticleDelete)))).Methods("GET")
router.Handle("/articles/edit/{id}", middlewares.StrictAuthentication(middlewares.ArticleAuthorization(http.HandlerFunc(controllers.ArticleEditPage)))).Methods("GET")
router.Handle("/articles/edit/{id}", middlewares.StrictAuthentication(middlewares.ArticleAuthorization(http.HandlerFunc(controllers.ArticleUpdate)))).Methods("POST")
router.Handle("/articles/publish/{id}", middlewares.StrictAuthentication(middlewares.ArticleAuthorization(http.HandlerFunc(controllers.ArticlePublish)))).Methods("GET")
router.Handle("/articles/unpublish/{id}", middlewares.StrictAuthentication(middlewares.ArticleAuthorization(http.HandlerFunc(controllers.ArticleUnpublish)))).Methods("GET")
router.Handle("/articles/create", middlewares.StrictAuthentication(http.HandlerFunc(controllers.ArticleCreatePage))).Methods("GET")
router.Handle("/articles/create", middlewares.StrictAuthentication(http.HandlerFunc(controllers.ArticleCreate))).Methods("POST")
router.Handle("/articles/detail/{id}", middlewares.DetailAuthorization(http.HandlerFunc(controllers.ArticleViewPage))).Methods("GET")
router.Handle("/articles", middlewares.StrictAuthentication(http.HandlerFunc(controllers.ArticleListPage))).Methods("GET")
router.Handle("/about", middlewares.Authentication(http.HandlerFunc(controllers.AboutPage))).Methods("GET")
router.Handle("/contact", middlewares.Authentication(http.HandlerFunc(controllers.SendEmail))).Methods("POST")
router.Handle("/contact", middlewares.Authentication(http.HandlerFunc(controllers.ContactPage))).Methods("GET")
router.Handle("/", middlewares.Authentication(http.HandlerFunc(controllers.HomePage))).Methods("GET")
return router
}