2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2018-07-24 14:45:25 +08:00
|
|
|
func routesServe() error {
|
2018-05-27 22:40:42 +08:00
|
|
|
router := mux.NewRouter()
|
|
|
|
|
2018-08-08 14:46:27 +08:00
|
|
|
subdir := pathStrip(os.Getenv("ORIGIN"))
|
|
|
|
if subdir != "" {
|
|
|
|
router = router.PathPrefix(subdir).Subrouter()
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:46:02 +08:00
|
|
|
if err := apiRouterInit(router); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:46:30 +08:00
|
|
|
if err := staticRouterInit(router); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
origins := handlers.AllowedOrigins([]string{"*"})
|
|
|
|
headers := handlers.AllowedHeaders([]string{"X-Requested-With"})
|
|
|
|
methods := handlers.AllowedMethods([]string{"GET", "POST"})
|
|
|
|
|
2018-06-10 05:19:19 +08:00
|
|
|
addrPort := os.Getenv("BIND_ADDRESS") + ":" + os.Getenv("PORT")
|
|
|
|
|
|
|
|
logger.Infof("starting server on %s\n", addrPort)
|
|
|
|
if err := http.ListenAndServe(addrPort, handlers.CORS(origins, headers, methods)(router)); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
logger.Errorf("cannot start server: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|