Skip to content

Kadai4 yuuyamad #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions kadai4/yuuyamad/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"net/http"
"time"
"log"
)

func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

inner.ServeHTTP(w, r)

log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}
90 changes: 90 additions & 0 deletions kadai4/yuuyamad/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"log"
"net/http"
"github.com/gorilla/mux"
"time"
"math/rand"
"encoding/json"
)

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Responce struct {
Omikuji string `json:"omikuji"`
}

type Routes []Route

var routes = Routes{
Route{
"Getomikuji",
"GET",
"/api/v1/omikuji",
GetOmikuji,
},
}

var timeNowFunc = time.Now // 関数をグローバル変数に代入


func main() {
router := NewRouter()

log.Fatal(http.ListenAndServe(":8080", router))

}

func GetOmikuji(w http.ResponseWriter, r *http.Request){
var responce Responce
t := timeNowFunc()

// 1/1 ~ 1/3は絶対大吉
if (t.Month() == 1 && t.Day() >= 1 && t.Day() <= 3){
responce.Omikuji = "大吉"
} else {
t := timeNowFunc().UnixNano()
rand.Seed(t)
s := rand.Intn(6)
switch s + 1 {
case 1:
responce.Omikuji = "凶"
case 2, 3:
responce.Omikuji = "吉"
case 4, 5:
responce.Omikuji = "中吉"
case 6:
responce.Omikuji = "大吉"
}
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(responce); err != nil {
panic(err)
}

}
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler

handler = route.HandlerFunc
handler = Logger(handler, route.Name)

router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}

return router
}
56 changes: 56 additions & 0 deletions kadai4/yuuyamad/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"testing"
"time"
"net/http/httptest"
"io/ioutil"
"github.com/stretchr/testify/assert"
"net/http"
)

const timeformat = "2006-01-02 15:04:06"

func setNow(t time.Time) {
timeNowFunc = func() time.Time { return t }
}


func TestGetOmikujiain(t *testing.T) {
syogatsu , _ := time.Parse(timeformat, "2018-01-03 14:10:00")
heijitu , _ := time.Parse(timeformat, "2018-02-03 14:10:00")
heijitu2 , _ := time.Parse(timeformat, "2018-03-03 18:11:11")

cases := []struct {
date time.Time
responce string
}{
{date: syogatsu, responce: "{\"omikuji\":\"大吉\"}"},
{date: heijitu, responce: "{\"omikuji\":\"吉\"}"},
{date: heijitu2, responce: "{\"omikuji\":\"中吉\"}"},

}

for _, c := range cases {
setNow(c.date)

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/omikuji", nil)
GetOmikuji(w, r)
rw := w.Result()
defer rw.Body.Close()

if rw.StatusCode != http.StatusOK {
t.Fatal("unexpected status code")
}
b, err := ioutil.ReadAll(rw.Body)
if err != nil {
t.Fatal("unexpected error")
}

if s := string(b); !assert.JSONEq(t, s, c.responce) {
t.Fatalf("unexpected response: %s", s)
}

}
}
Binary file added kadai4/yuuyamad/yuuyamad
Binary file not shown.