Guest User

Untitled

a guest
Oct 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "net/http"
  5. "os"
  6. "strings"
  7.  
  8. "github.com/gorilla/context"
  9. "github.com/justinas/alice"
  10. "gopkg.in/mgo.v2"
  11.  
  12. "gitlab.com/myapp/api-auth/middlewares"
  13. )
  14.  
  15. func main() {
  16. privateKey := []byte(strings.Replace(os.Getenv("JWT_KEY"), "\n", "n", -1))
  17.  
  18. conn, err := mgo.Dial(os.Getenv("MONGO_CONN"))
  19.  
  20. if err != nil {
  21. panic(err)
  22. }
  23.  
  24. defer conn.Close()
  25. conn.SetMode(mgo.Monotonic, true)
  26.  
  27. ctx := appContext{
  28. conn.DB(os.Getenv("MONGO_DB")),
  29. privateKey,
  30. }
  31.  
  32. err = ctx.db.C("users").EnsureIndex(mgo.Index{
  33. Key: []string{"username"},
  34. Unique: true,
  35. Background: true,
  36. Sparse: false,
  37. })
  38.  
  39. if err != nil {
  40. panic(err)
  41. }
  42.  
  43. commonHandlers := alice.New(LoggingHandler, context.ClearHandler, RecoveryHandler, AcceptHandler, ContentTypeHandler)
  44.  
  45. router := NewRouter()
  46. router.Post("/users", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.userCreationHandler))
  47. router.Post("/sessions", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.sessionCreationHandler))
  48.  
  49. http.ListenAndServe(":8080", router)
  50. }
  51.  
  52. type appContext struct {
  53. db *mgo.Database
  54. privateKey []byte
  55. }
  56.  
  57. package middlewares
  58.  
  59. import "net/http"
  60.  
  61. // AcceptHandler ensures proper accept headers in requests
  62. func AcceptHandler(next http.Handler) http.Handler {
  63. fn := func(w http.ResponseWriter, r *http.Request) {
  64. if r.Header.Get("Accept") != "application/vnd.api+json" {
  65. writeError(w, errNotAcceptable)
  66. return
  67. }
  68.  
  69. next.ServeHTTP(w, r)
  70. }
  71.  
  72. return http.HandlerFunc(fn)
  73. }
  74.  
  75. # gitlab.com/utiliti.es/api-auth
  76. ./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
  77. ./main.go:42: undefined: LoggingHandler
  78. ./main.go:42: undefined: RecoveryHandler
  79. ./main.go:42: undefined: AcceptHandler
  80. ./main.go:42: undefined: ContentTypeHandler
  81. ./main.go:45: undefined: BodyParserHandler
  82. ./main.go:46: undefined: BodyParserHandler
  83.  
  84. QualifiedIdent = PackageName "." identifier .
  85.  
  86. math.Sin // denotes the Sin function in package math
  87.  
  88. ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
  89. ImportSpec = [ "." | PackageName ] ImportPath .
  90. ImportPath = string_lit .
  91.  
  92. Import declaration Local name of Sin
  93.  
  94. import "lib/math" math.Sin
  95. import m "lib/math" m.Sin
  96. import . "lib/math" Sin
  97.  
  98. import _ "lib/math"
  99.  
  100. ./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
  101.  
  102. ./main.go:42: undefined: AcceptHandler
Add Comment
Please, Sign In to add comment