This repository has been archived on 2023-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
sudoscientist-go-backend/main.go

66 lines
1.5 KiB
Go
Raw Normal View History

2019-02-03 06:57:08 +00:00
package main
import (
"fmt"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
"git.minhas.io/asara/sudoscientist/packages/auth"
"git.minhas.io/asara/sudoscientist/packages/database"
2019-02-07 04:46:30 +00:00
"git.minhas.io/asara/sudoscientist/packages/users"
2019-02-03 06:57:08 +00:00
"github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"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-02-03 06:57:08 +00:00
// initiate jwt token
auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
_, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123})
log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
// 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()
router.Use(
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())
})
return router
}