-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest-api.go
69 lines (52 loc) · 1.59 KB
/
rest-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
package module
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/cjlapao/common-go/helper"
"github.com/cjlapao/deployment-tools-go/controllers"
"github.com/cjlapao/deployment-tools-go/database"
"github.com/cjlapao/deployment-tools-go/entities"
"net/http"
"github.com/cjlapao/deployment-tools-go/repositories"
"github.com/gorilla/mux"
)
type article = entities.Article
var router mux.Router
var databaseContext database.MongoFactory
var articlesRepo repositories.Repository
func RestApiModuleProcessor() {
fmt.Println("Testing after")
logger.Info("this would be the info %v", versionSvc.String())
logger.Notice("Starting Go Rest API v0.1")
databaseContext = database.NewFactory()
articlesRepo = repositories.NewRepo(&databaseContext)
pushTestData()
handleRequests()
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome to the Homepage!")
fmt.Println("endpoint Hit: homepage")
}
func handleRequests() {
router := mux.NewRouter().StrictSlash(true)
router.Use(commonMiddleware)
router.HandleFunc("/", homePage)
_ = controllers.NewAPIController(router, articlesRepo)
logger.Success("Finished Init")
log.Fatal(http.ListenAndServe(":10000", router))
}
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}
func pushTestData() {
articles := []article{}
data, err := ioutil.ReadFile("demo.json")
helper.CheckError(err)
json.Unmarshal(data, &articles)
articlesRepo.UpsertManyArticles(articles)
}