-
Notifications
You must be signed in to change notification settings - Fork 0
/
imf-web-api.go
81 lines (59 loc) · 1.5 KB
/
imf-web-api.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
package main
import (
"imf-api/imfdb"
"github.com/gorilla/mux"
"net/http"
"encoding/json"
"io/ioutil"
"strconv"
"log"
)
func (appCtx *AppContext) Config(configFile string) error {
file, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
var conf Config
if json.Unmarshal(file, &conf) != nil {
return err
}
appCtx.conf = conf
return nil
}
func main() {
// Create an application context
appCtx := &AppContext{}
// Create database connection
appCtx.db = &imfdb.ImfDB {}
// Read config file
err := appCtx.Config("./resources/config.json")
check(err)
// Configure database connection
err = appCtx.db.Init(appCtx.conf.Db)
check(err)
defer appCtx.db.Close()
// Create a router
router := mux.NewRouter()
router.HandleFunc(appCtx.conf.Net.QueryPath, appCtx.GetImgUrls).Methods(appCtx.conf.Net.QueryMethod)
log.Fatal(http.ListenAndServe(appCtx.conf.Net.Port, router))
}
func (app *AppContext) GetImgUrls(w http.ResponseWriter, r *http.Request) {
queries := r.URL.Query()
// Check params validities
character := queries.Get(app.conf.Query.Character)
nbImgs, convErr := strconv.Atoi(queries.Get(app.conf.Query.NbImgs))
// In case nbimgs is not number, return only one image url
if convErr != nil {
nbImgs = 1
}
w.Header().Add("Content-Type", "application/json")
// Query!!!
imgUrls, _ := app.db.GetRandomImgs(character, nbImgs)
jsonRet := &RestResult{ Data : imgUrls }
json.NewEncoder(w).Encode(jsonRet)
}
func check(err error) {
if err != nil {
panic(err)
}
}