forked from rdegges/ipify-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (34 loc) · 1.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// ipify-api
//
// This is the main package which starts up and runs our REST API service.
//
// ipify is a simple API service which returns a user's public IP address (it
// supports handling both IPv4 and IPv6 addresses).
package main
import (
"github.com/julienschmidt/httprouter"
"github.com/rdegges/ipify-api/api"
"github.com/rs/cors"
"log"
"net/http"
"os"
)
// main launches our web server which runs indefinitely.
func main() {
// Setup all routes. We only service API requests, so this is basic.
router := httprouter.New()
router.GET("/", api.GetIP)
// Setup 404 / 405 handlers.
router.NotFound = http.HandlerFunc(api.NotFound)
router.MethodNotAllowed = http.HandlerFunc(api.MethodNotAllowed)
// Setup middlewares. For this we're basically adding:
// - Support for CORS to make JSONP work.
handler := cors.Default().Handler(router)
// Start the server.
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Println("Starting HTTP server on port:", port)
log.Fatal(http.ListenAndServe(":"+port, handler))
}