I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details

preface

About the Mid-Autumn festival essay content, I really thought for a long time, because as a back-end r&d engineer, do not want to make a cool interface, feel that is the front of the cake of the students. I want to take advantage of my skills and start a wave of things. At the same time, I want to be innovative and do something that others have never done. I finally decided the content of the article yesterday — to develop a Mid-Autumn Festival calendar reminder service. Today, I began to work formally.

The body of the

This article introduces the Mid-Autumn Festival calendar reminder service, mainly has two functions, one provides the active query ability, the other is the callback notification of the registration festival. The following are introduced respectively.

Take the initiative to query

The Mid-Autumn Festival calendar reminder service provides a festival query interface. By invoking the query interface, the calendar service returns whether the current time is a Mid-Autumn Festival holiday according to the current query time. Note that the service does not determine whether it is the Mid-Autumn Festival holiday based on the upload time of the client. It is based on the server time, which avoids the problem of inconsistent query results caused by local clock deviation on different terminals. However, this can lead to another problem, which is that if the server clock is not correct, the query results will not be reliable. Therefore, it is important to ensure that the state of the server clock is accurate, which, of course, is the basis for the calendar reminder service.

1. Prepare holiday data sets

The Mid-Autumn Festival calendar service can provide services on the premise of having festival data, which is simply the specific date of the Mid-Autumn Festival. We store the data set in the file data.json as follows:

{
    "Mid-Autumn Festival": [
        "2021-9-19"]."Mid-Autumn festival": [
        "2021-9-19"]}Copy the code

Here only lists the Mid-Autumn Festival time set, later we can also continue to expand the query ability of other festivals, such as National Day, New Year’s day, Dragon Boat Festival, etc..

2. Define the query interface

Mid-Autumn Festival calendar reminder service API: /: Festival /:callback

Interface type: Get

When the server processes the request, it determines the type of festival to query or register based on the parameter Festival. This article only discusses Mid-Autumn Festival. Callback is the target address of the callback notification. The logic is as follows:

        r.GET("/:festival/:callback".func(ctx *gin.Context) {
		fest := ctx.Param("festival")
		cb := ctx.Param("callback")

		iferr := holiday.SetCallback(cb); err ! =nil {
			ctx.String(http.StatusBadRequest, "%s", err.Error())
		}

		data, err := holiday.Query(fest)
		iferr ! =nil {
			ctx.String(http.StatusBadRequest, "%s", err.Error())
		} else {
			ctx.JSON(http.StatusOK, data)
		}
	})
	log.Fatal(r.Run(": 3000"))
Copy the code

3. Query service

When the server receives the Mid-Autumn Festival query service, it will first go to the festival data set to query, if there is no specific prompt given. If a holiday data set exists, the system verifies the difference between the current query time and the holiday time to determine the holiday status.

The specific code logic is as follows:

func Query(date string) (res []string, err error) {
	// Query the specific holiday type. Currently, you can query only the Mid-Autumn Festival
	day, ok := holidayCache[date]
	if! ok {return nil, errors.New("Sorry, this holiday query is not supported.")
	}
	da := strings.Split(day[0]."-")
	y, _ := strconv.Atoi(da[0])
	m, _ := strconv.Atoi(da[1])
	d, _ := strconv.Atoi(da[2])

	// Calculate the difference between the current time and Mid-Autumn Festival days
	now := time.Now()
	dt1 := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 100, time.Local)
	dt2 := time.Date(y, time.Month(m), d, 0.0.0.100, time.Local)
	sub := int(math.Ceil(dt2.Sub(dt1).Hours() / 24))

	// There will be three values
	if sub == 0 || sub == - 1 || sub == 2 - {
		msg := "Today is Mid-Autumn Festival, enjoy your holiday!"
		res = append(res, msg)
		return res, nil
	} else if sub > 0 {
		msg := "And" + strconv.Itoa(sub) + "Mid-Autumn Festival is coming. Look forward to the holiday!"
		res = append(res, msg)
		return res, nil
	} else {
		msg := "This year's Mid-Autumn Festival has passed, look forward to see you next Mid-Autumn Festival!"
		res = append(res, msg)
		return res, nil}}Copy the code

4. Interface demonstration

Let’s verify the availability of the calendar reminder service with different requests.

1) Normal request

Get request: http://127.0.0.1:3000/%E4%B8%AD%E7%A7%8B%E8%8A%82/192.168.166.6:3001

Change the server time to September 19, 2021 (Mid-Autumn Festival day), because the service is deployed locally, that is, change the local time. Then call the query interface and return the result as shown in the figure below:

If the server time is adjusted to October 1, 2021 (the Mid-Autumn Festival has passed), then call the query interface, and the returned result is as follows:

2) Abnormal request

Get request: http://127.0.0.1:3000/%E5%9B%BD%E5%BA%86%E8%8A%82/192.168.166.6:3001

The callback notifications

To register the Mid-Autumn Festival callback notification, start a timer that automatically triggers the HTTP callback notification when Mid-Autumn Festival time arrives. The specific address is the callback parameter in the request. The principle and query calendar service interface is the same, the content is not expanded.

At the end

Well, a mid – Autumn Festival calendar reminder service is developed. After deploying the Mid-Autumn Festival reminder service to the public network, you can wait patiently for the “Mid-Autumn Festival moon appreciation” notification. Give it a thumbs up, 3ks!