-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (71 loc) · 2.05 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path"
"github.com/gin-gonic/gin"
"github.com/northbright/pathhelper"
"github.com/shchnmz/ming"
"github.com/shchnmz/zb"
)
// Config represents the app settings.
type Config struct {
ServerAddr string `json:"server_addr"`
RedisServer string `json:"redis_server"`
RedisPassword string `json:"redis_password"`
}
var (
currentDir, configFile string
config Config
db zb.DB
)
func main() {
var (
err error
)
defer func() {
if err != nil {
log.Printf("error: %v\n", err)
}
}()
if err = loadConfig(configFile, &config); err != nil {
err = fmt.Errorf("loadConfig() error: %v", err)
return
}
// Init DB.
db = zb.DB{ming.DB{config.RedisServer, config.RedisPassword}}
r := gin.Default()
// Core APIs.
// Get student names by phone num.
r.GET("/api/get-names-by-phone-num/:phone_num", getNamesByPhoneNum)
// Get classes by name and phone num.
r.GET("/api/get-classes-by-name-and-phone-num/:name/:phone_num", getClassesByNameAndPhoneNum)
// Get teachers by class(with campus and category).
// Use "*" for wildcard param when class contains "/". e.g.(一年级(1)班,11/10日开学).
r.GET("/api/get-teachers-by-class/*class", getTeachersByClass)
// Get available periods for the category of the class.
// Use "*" for wildcard param when class contains "/". e.g.(一年级(1)班,11/10日开学).
r.GET("/api/get-available-periods/*class", getAvailablePeriods)
// Post request.
r.POST("/api/request", postRequest)
r.Run(config.ServerAddr)
}
// init initializes path variables.
func init() {
currentDir, _ = pathhelper.GetCurrentExecDir()
configFile = path.Join(currentDir, "config.json")
}
// loadConfig loads app config.
func loadConfig(configFile string, config *Config) error {
// Load Conifg
buf, err := ioutil.ReadFile(configFile)
if err != nil {
return fmt.Errorf("load config file error: %v", err)
}
if err = json.Unmarshal(buf, config); err != nil {
return fmt.Errorf("parse config err: %v", err)
}
return nil
}