package main | |||||
import ( | |||||
"io" | |||||
"net/http" | |||||
_ "git.thevis.us/skepto/env" | |||||
_ "git.thevis.us/skepto/rex" | |||||
) | |||||
func Index(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "pehpeh-api") | |||||
} |
require ( | require ( | ||||
git.thevis.us/skepto/env v0.0.0-20200712040037-f8a5c3d1b0fa | git.thevis.us/skepto/env v0.0.0-20200712040037-f8a5c3d1b0fa | ||||
git.thevis.us/skepto/rex v0.10.0 | git.thevis.us/skepto/rex v0.10.0 | ||||
github.com/gorilla/mux v1.7.4 | |||||
github.com/stretchr/testify v1.2.2 | |||||
) | ) |
package main | package main | ||||
import ( | import ( | ||||
"io" | |||||
"net/http" | "net/http" | ||||
"git.thevis.us/skepto/env" | "git.thevis.us/skepto/env" | ||||
"git.thevis.us/skepto/rex" | "git.thevis.us/skepto/rex" | ||||
) | ) | ||||
type Server interface { | |||||
Run() | |||||
Get(pattern string, handler interface{}) | |||||
ServeHTTP(w http.ResponseWriter, r *http.Request) | |||||
} | |||||
func main() { | func main() { | ||||
srv := Configure() | |||||
srv.Run() | |||||
} | |||||
func Configure() Server { | |||||
env.Set("PORT", 7777) | env.Set("PORT", 7777) | ||||
app := rex.New() | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "pehpeh-api") | |||||
}) | |||||
app.Run() | |||||
server := rex.New() | |||||
server.Get("/", Index) | |||||
return server | |||||
} | } |
package main | |||||
import ( | |||||
"net/http" | |||||
"net/http/httptest" | |||||
"testing" | |||||
"github.com/stretchr/testify/assert" | |||||
) | |||||
func TestConfigure(t *testing.T) { | |||||
srv := Configure() | |||||
request, _ := http.NewRequest("GET", "/", nil) | |||||
response := httptest.NewRecorder() | |||||
srv.ServeHTTP(response, request) | |||||
assert.Equal(t, "pehpeh-api", response.Body.String(), "response body is expected") | |||||
assert.Equal(t, 200, response.Code, "OK response is expected") | |||||
} |