2019-02-03 06:57:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2019-05-28 01:17:09 +00:00
|
|
|
"git.minhas.io/asara/sudoscientist-go-backend/packages/auth"
|
|
|
|
"git.minhas.io/asara/sudoscientist-go-backend/packages/blog"
|
|
|
|
"git.minhas.io/asara/sudoscientist-go-backend/packages/database"
|
|
|
|
"git.minhas.io/asara/sudoscientist-go-backend/packages/users"
|
2019-02-03 06:57:08 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2019-05-28 01:17:09 +00:00
|
|
|
"github.com/go-chi/cors"
|
2019-02-03 06:57:08 +00:00
|
|
|
"github.com/go-chi/jwtauth"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// initiate the database
|
|
|
|
db, _ := database.NewDB()
|
|
|
|
defer db.Close()
|
|
|
|
auth.DB = db
|
|
|
|
auth.Init()
|
2019-02-07 04:46:30 +00:00
|
|
|
users.DB = db
|
|
|
|
users.Init()
|
2019-04-20 21:17:20 +00:00
|
|
|
blog.DB = db
|
|
|
|
blog.Init()
|
2019-02-03 06:57:08 +00:00
|
|
|
|
|
|
|
// initiate jwt token
|
2019-02-08 23:16:10 +00:00
|
|
|
auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil)
|
2019-02-08 04:43:26 +00:00
|
|
|
users.TokenAuth = auth.TokenAuth
|
2019-04-20 21:17:20 +00:00
|
|
|
blog.TokenAuth = auth.TokenAuth
|
2019-02-03 06:57:08 +00:00
|
|
|
// initiate the routes
|
|
|
|
router := Routes()
|
|
|
|
|
|
|
|
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
|
|
|
fmt.Printf("%s %s\n", method, route)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chi.Walk(router, walkFunc); err != nil {
|
|
|
|
log.Panicf("Logging err: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// start server
|
|
|
|
apiPort := fmt.Sprintf(":%s", os.Getenv("API_PORT"))
|
|
|
|
log.Fatal(http.ListenAndServe(apiPort, router))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Routes() *chi.Mux {
|
|
|
|
router := chi.NewRouter()
|
2019-09-28 12:35:49 +00:00
|
|
|
// enable cors testing
|
|
|
|
// LOCK THIS DOWN FOR PRODUCTION
|
|
|
|
cors := cors.New(cors.Options{
|
2019-10-06 02:57:54 +00:00
|
|
|
AllowedOrigins: []string{"https//sudoscientist.com", "https://www.sudoscientist.com"},
|
2019-10-06 02:35:14 +00:00
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
|
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
2019-10-06 01:22:56 +00:00
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 360,
|
2019-09-28 12:35:49 +00:00
|
|
|
})
|
2019-05-28 01:17:09 +00:00
|
|
|
|
2019-02-03 06:57:08 +00:00
|
|
|
router.Use(
|
2019-09-28 12:35:49 +00:00
|
|
|
cors.Handler,
|
2019-02-03 06:57:08 +00:00
|
|
|
render.SetContentType(render.ContentTypeJSON),
|
|
|
|
middleware.Logger,
|
|
|
|
middleware.DefaultCompress,
|
|
|
|
middleware.RedirectSlashes,
|
|
|
|
middleware.Recoverer,
|
|
|
|
)
|
|
|
|
|
|
|
|
router.Route("/v1", func(r chi.Router) {
|
|
|
|
r.Mount("/api/auth", auth.Routes())
|
2019-04-15 22:40:50 +00:00
|
|
|
r.Mount("/api/blog", blog.Routes())
|
2019-02-08 04:43:26 +00:00
|
|
|
r.Mount("/api/users", users.Routes())
|
2019-02-03 06:57:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|