arnaucube
arnaucube/decentralized-blogging-platformPublic Self-hosted repository
decentralized-blogging-platform/main.go
main.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"log"
 6	"net/http"
 7	"os"
 8
 9	"github.com/fatih/color"
10	"github.com/gorilla/handlers"
11	mgo "gopkg.in/mgo.v2"
12)
13
14const keysDir = "keys"
15const keysize = 2048
16const hashize = 1536
17
18var userCollection *mgo.Collection
19var postCollection *mgo.Collection
20
21func main() {
22	color.Blue("Starting ipfs-ai-models-market")
23
24	readConfig("config.json")
25	fmt.Println(config)
26
27	//create models directory
28	_ = os.Mkdir(keysDir, os.ModePerm)
29	//create models directory
30	_ = os.Mkdir("ownposts", os.ModePerm)
31
32	initializeToken()
33
34	//mongodb
35	session, err := getSession()
36	check(err)
37	userCollection = getCollection(session, "users")
38	postCollection = getCollection(session, "posts")
39
40	//run thw webserver
41	go GUI()
42
43	//run API
44	log.Println("api server running")
45	log.Print("port: ")
46	log.Println(config.APIPort)
47	router := NewRouter()
48	headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin", "Authorization"})
49	originsOk := handlers.AllowedOrigins([]string{"*"})
50	methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
51	log.Fatal(http.ListenAndServe(":"+config.APIPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
52}
53
54func GUI() {
55	//here, run webserver
56	log.Println("webserver in port " + config.WebPort)
57	http.Handle("/", http.FileServer(http.Dir("./webapp")))
58	http.ListenAndServe(":"+config.WebPort, nil)
59}