You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. mgo "gopkg.in/mgo.v2"
  7. "github.com/fatih/color"
  8. "github.com/gorilla/handlers"
  9. ownrsa "./ownrsa"
  10. )
  11. var userCollection *mgo.Collection
  12. var serverRSA ownrsa.RSA
  13. func main() {
  14. color.Blue("Starting serverIDsigner")
  15. //read configuration file
  16. readConfig("config.json")
  17. initializeToken()
  18. //initialize RSA
  19. serverRSA = ownrsa.GenerateKeyPair()
  20. color.Blue("Public Key:")
  21. fmt.Println(serverRSA.PubK)
  22. color.Green("Private Key:")
  23. fmt.Println(serverRSA.PrivK)
  24. //mongodb
  25. session, err := getSession()
  26. check(err)
  27. userCollection = getCollection(session, "users")
  28. //run thw webserver
  29. go webserver()
  30. //run API
  31. log.Println("api server running")
  32. log.Print("port: ")
  33. log.Println(config.Port)
  34. router := NewRouter()
  35. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  36. originsOk := handlers.AllowedOrigins([]string{"*"})
  37. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  38. log.Fatal(http.ListenAndServe(":"+config.Port, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  39. }
  40. func webserver() {
  41. log.Println("webserver in port " + config.WebServerPort)
  42. http.Handle("/", http.FileServer(http.Dir("./webapp")))
  43. http.ListenAndServe(":"+config.WebServerPort, nil)
  44. }