Skip to content
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

kadai4 by xlune #59

Open
wants to merge 1 commit 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
12 changes: 12 additions & 0 deletions kadai4/xlune/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"net/http"

"github.com/xlune/dojo1/kadai4/xlune/omikuji"
)

func main() {
http.HandleFunc("/", omikuji.Handler)
http.ListenAndServe(":8080", nil)
}
115 changes: 115 additions & 0 deletions kadai4/xlune/omikuji/omikuji.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package omikuji

import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strings"
"time"
)

// Fortune おみくじ結果タイプ
type Fortune int

// JSONTime JSON出力時間
type JSONTime struct {
time.Time
}

const (
// Kyo 凶
Kyo Fortune = iota
// Kichi 吉
Kichi
// Chukichi 中吉
Chukichi
// Daikichi 大吉
Daikichi
)

// Result 返却データ
type Result struct {
Label string `json:"label"`
Type Fortune `json:"type"`
Date JSONTime `json:"date"`
}

// MarshalJSON JSON文字列生成
func (t JSONTime) MarshalJSON() ([]byte, error) {
timeStr := fmt.Sprintf("\"%s\"", t.Time.Format("2006-01-02 15:04:05"))
return []byte(timeStr), nil
}

// UnmarshalJSON JSON文字列生成パース
func (t *JSONTime) UnmarshalJSON(data []byte) (err error) {
timeObj, err := strToTime(strings.Trim(string(data), "\""))
if err != nil {
return err
}
t.Time = timeObj
return nil
}

// Handler ハンドラ実装
func Handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")

dateStr, ok := r.URL.Query()["date"]
dateTime := time.Now()

if ok && len(dateStr) > 0 {
t, err := strToTime(dateStr[0])
if err == nil {
dateTime = t
}
}

fortune := getFortune(dateTime)
result := Result{
Type: fortune,
Label: getLabel(fortune),
Date: JSONTime{dateTime},
}

if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println("Error:", err)
}
}

func getFortune(t time.Time) Fortune {
// 1/1 - 1/3 は大吉
if t.Month() == time.January && t.Day() <= 3 {
return Daikichi
}
// Seed指定でランダム選択
rand.Seed(t.UnixNano())
n := rand.Intn(4)
return Fortune(n)
}

func getLabel(f Fortune) string {
switch f {
case Kichi:
return "吉"
case Chukichi:
return "中吉"
case Daikichi:
return "大吉"
default:
return "凶"
}
}

func strToTime(str string) (time.Time, error) {
t, err := time.Parse("2006-01-02 15:04:05", str)
if err != nil {
t, err = time.Parse("2006/01/02 15:04:05", str)
if err != nil {
return time.Time{}, err
}
}
return t, nil
}
50 changes: 50 additions & 0 deletions kadai4/xlune/omikuji/omikuji_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package omikuji_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/xlune/dojo1/kadai4/xlune/omikuji"
)

var handlerTests = []struct {
date string
fortune omikuji.Fortune
}{
{"2017-01-01 00:00:00", omikuji.Daikichi},
{"2017-01-01 23:59:59", omikuji.Daikichi},
{"2017-01-02 00:00:00", omikuji.Daikichi},
{"2017-01-02 23:59:59", omikuji.Daikichi},
{"2017-01-03 00:00:00", omikuji.Daikichi},
{"2017-01-03 23:59:59", omikuji.Daikichi},
{"2018-04-01 01:23:45", omikuji.Chukichi},
{"2019-08-08 22:23:45", omikuji.Kyo},
{"2020-10-06 10:10:10", omikuji.Daikichi},
{"2021-12-22 11:11:09", omikuji.Kichi},
}

func TestHandler(t *testing.T) {
for _, h := range handlerTests {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
q := r.URL.Query()
q.Add("date", h.date)
r.URL.RawQuery = q.Encode()
omikuji.Handler(w, r)
rw := w.Result()
defer rw.Body.Close()
if rw.StatusCode != http.StatusOK {
t.Fatal("unexpected status code")
}
var res omikuji.Result
dec := json.NewDecoder(rw.Body)
if err := dec.Decode(&res); err != nil {
t.Fatal("json decode error")
}
if res.Type != h.fortune {
t.Fatal("unexpected error")
}
}
}