-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (46 loc) · 1.79 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
package main
import (
"backend-rest/controllers"
"backend-rest/middlewares"
"io"
log "log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
const logPath = "logs.log"
var Logger *log.Logger
func main() {
InitLogger()
router := mux.NewRouter()
router.HandleFunc("/api/user/new", controllers.CreateAccount).Methods("POST")
router.HandleFunc("/api/user/login", controllers.Authenticate).Methods("POST")
router.HandleFunc("/api/workers/new_scraped", controllers.AddNew).Methods("POST")
router.HandleFunc("/api/workers/new_scraped_many", controllers.AddMany).Methods("POST")
router.HandleFunc("/api/workers/scraping_index", controllers.SaveScrapingIndex).Methods("POST")
router.HandleFunc("/api/workers/scraping_index", controllers.GetScrapingIndex).Methods("GET")
router.HandleFunc("/api/workers/scraping_index_newspaper", controllers.GetScrapingIndexNewsPaper).Methods("GET")
// router.Use(middlewares.JwtAuthentication) //attach JWT auth middleware
router.Use(middlewares.MiddlewareLogger) //attach JWT auth middleware
//router.NotFoundHandler = app.NotFoundHandler
port := os.Getenv("PORT")
if port == "" {
port = "8000" //localhost
}
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
// start server listen
// with error handling
log.Println("Starting server on port " + port)
log.Println(http.ListenAndServe(":"+port, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
}
func InitLogger() {
file, err := os.OpenFile("log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file")
}
mw := io.MultiWriter(os.Stdout, file)
log.SetOutput(mw)
}