-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
158 lines (130 loc) · 4.19 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/slaveofcode/hansip/repository"
"github.com/slaveofcode/hansip/repository/pg"
"github.com/slaveofcode/hansip/repository/sqlite"
appRoutes "github.com/slaveofcode/hansip/routes"
appConfig "github.com/slaveofcode/hansip/utils/config"
"github.com/spf13/viper"
)
var awsConfig aws.Config
func readConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".") // working directory
viper.AddConfigPath("$HOME/.hansip") // hansip app directory
viper.AddConfigPath("/etc/hansip") // system directory
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("please create the config file [config.yaml]"))
} else {
panic(fmt.Errorf("unable to read config file [config.yaml]: %w", err))
}
}
if appConfig.IsUsingS3Storage() {
if viper.GetString("aws.region") == "" {
panic("please set AWS region [config.yaml]")
}
if viper.GetString("aws.s3.access_key") == "" || viper.GetString("aws.s3.secret_key") == "" {
panic("you choose to use S3 storage, please set S3 credentials at the config file [config.yaml]")
}
if viper.GetString("aws.s3.bucket_name") == "" {
panic("please set S3 bucket name [config.yaml]")
}
awsCfg, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(viper.GetString("aws.region")),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
viper.GetString("aws.s3.access_key"),
viper.GetString("aws.s3.secret_key"),
"",
),
))
if err != nil {
panic(fmt.Errorf("unable to load AWS config: %w", err))
}
awsConfig = awsCfg
}
// Set default config keys
viper.SetDefault("sqlite.path", "./hansip.db")
viper.SetDefault("server_api.host", "localhost")
viper.SetDefault("site.shortlink_path", "/d")
}
func prepareDirs(dirList []string) error {
for _, path := range dirList {
var err error
if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println("Unable create TMP dir:", err.Error())
return err
}
}
}
return nil
}
func getRepo() repository.Repository {
if viper.GetString("db.type") == "postgresql" {
return pg.NewRepository(&pg.ConnectionOption{
DBName: viper.GetString("postgresql.name"),
Host: viper.GetString("postgresql.host"),
Port: viper.GetString("postgresql.port"),
User: viper.GetString("postgresql.user"),
Pass: viper.GetString("postgresql.password"),
}, time.UTC)
}
return sqlite.NewRepository(&sqlite.ConnectionOption{
Path: viper.GetString("sqlite.path"),
}, time.UTC)
}
func main() {
readConfig()
var s3Client *s3.Client
if appConfig.IsUsingS3Storage() {
s3Client = s3.NewFromConfig(awsConfig, func(o *s3.Options) {})
}
repo := getRepo()
if err := repo.Connect(context.Background()); err != nil {
panic(err.Error())
}
repo.Migrate()
defer repo.Close()
if err := prepareDirs([]string{
viper.GetString("dirpaths.upload"),
viper.GetString("dirpaths.bundle"),
}); err != nil {
panic("Unable to prepare temp. directories:" + err.Error())
}
gin.SetMode(gin.ReleaseMode)
routes := gin.Default()
corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
corsConfig.ExposeHeaders = []string{"Content-Disposition"}
routes.Use(cors.New(corsConfig))
routes.Use(gin.Recovery())
routes.MaxMultipartMemory = viper.GetInt64("upload.max_mb") << 20
appRoutes.Routes(routes, repo, s3Client)
serverAddr := viper.GetString("server_api.host") + ":" + viper.GetString("server_api.port")
server := &http.Server{
Addr: serverAddr,
Handler: routes,
}
log.Println("Server Started at:", fmt.Sprintf("http://%s", serverAddr))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}