-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (123 loc) · 4.18 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/weihesdlegend/mini_twitter/controller"
"github.com/weihesdlegend/mini_twitter/database"
_ "github.com/weihesdlegend/mini_twitter/docs"
"github.com/weihesdlegend/mini_twitter/tweet"
"github.com/weihesdlegend/mini_twitter/util"
"net/http"
"os"
"path"
"sort"
)
const (
UserDoesNotExist = "user %s does not exist"
DefaultConfigPath = "."
DefaultConfigFileName = "config.yaml"
)
type Config struct {
Admin []string `mapstructure:"admin"`
}
// @title User API documentation
// @version 1.0.0
// @host localhost:8800
// @BasePath /
func main() {
router := gin.Default()
if _, err := os.Stat(path.Join(DefaultConfigPath, DefaultConfigFileName)); err != nil {
log.Infof("Creating default config file at %s", path.Join(DefaultConfigPath, DefaultConfigFileName))
if os.IsNotExist(err) {
_, err = os.Create(path.Join(DefaultConfigPath, DefaultConfigFileName))
if err != nil {
log.Fatalf("failed to create config file: %s", DefaultConfigFileName)
}
}
}
viper.AddConfigPath(DefaultConfigPath)
viper.SetConfigType("yaml")
viper.SetConfigName(DefaultConfigFileName)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("failed to read config: %s", err.Error())
}
var config Config
if err = viper.Unmarshal(&config); err != nil {
log.Fatalf("failed to unmarshal into config: %s", err.Error())
}
log.Debugf("admin users are: %+v", config.Admin)
db, dbSetupErr := database.SetupDatabase(config.Admin)
util.CheckFatal(dbSetupErr)
database.DB = db
log.Info("starting server")
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
userRouterGroup := router.Group("/users")
{
userRouterGroup.POST("/follow", controller.FollowUser)
userRouterGroup.POST("/unfollow", controller.UnfollowUser)
userRouterGroup.POST("/create", controller.CreateUser)
userRouterGroup.GET("/names", controller.GetAllUsernames)
userRouterGroup.GET("/following", controller.GetUserFollowing)
}
// create new tweet post
router.POST("/tweets", controller.CreateTweet)
router.DELETE("/tweets/:id", func(c *gin.Context) {
id := c.Param("id")
u := c.Query("user")
if u == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "user name cannot be empty"})
}
if _, ok := database.Tweets[u].Tweets[id]; !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "only tweet owner can delete the tweet"})
}
if err := database.DeleteTweet(db, id, database.Tweets, u); err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "error while deleting tweet: " + err.Error()})
}
c.JSON(http.StatusOK, gin.H{})
})
// get all the tweets from a specific user in reversed order of post creation
router.GET("/tweets/:username", func(c *gin.Context) {
username := c.Param("username")
if username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "user name not specified"})
} else if _, ok := database.Users[username]; !ok {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf(UserDoesNotExist, username)})
} else {
ts := make([]*tweet.Tweet, 0)
for _, t := range database.Tweets[username].Tweets {
ts = append(ts, t)
}
sort.Slice(ts, func(i, j int) bool { return ts[i].CreatedAt.After(ts[j].CreatedAt) })
c.JSON(http.StatusOK, gin.H{"tweets": ts})
}
})
// get timeline for a specific user in reversed order of post creation
router.GET("/timeline/:username", func(c *gin.Context) {
username := c.Param("username")
if username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "user name not specified"})
} else if _, ok := database.Users[username]; !ok {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf(UserDoesNotExist, username)})
} else {
timeline := GetTimeLine(username)
c.JSON(http.StatusOK, gin.H{"result": timeline})
}
})
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Welcome to use the Mini Twitter")
})
port := os.Getenv("PORT")
if port == "" {
port = "8800"
}
if err := router.Run(":" + port); err != nil {
log.Fatal(err)
}
}