Skip to content

Commit 69b735f

Browse files
committed
v1 base
0 parents  commit 69b735f

File tree

34 files changed

+1372
-0
lines changed

34 files changed

+1372
-0
lines changed

.gitignore

Whitespace-only changes.

Dockerfile

Whitespace-only changes.

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2024 0xmmq
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h1 align="center">GoInk</h1>
2+
<p align="center">
3+
<img src="./assets/goink.png" height="300px"></img>
4+
</p>
5+
<p align="center">
6+
<img alt="app version" src="https://img.shields.io/github/v/release/mmqnym/GoInk"></img>
7+
<a href="https://goreportcard.com/report/github.com/mmqnym/GoInk"><img src="https://goreportcard.com/badge/github.com/mmqnym/GoInk"></a>
8+
<img alt="license" src="https://img.shields.io/github/license/mmqnym/GoInk"></img>
9+
</p>
10+
11+
## Introduction
12+
13+
This project is based on the following open source libraries:
14+
15+
- [Gin](https://github.com/gin-gonic/gin)
16+
- [swaggo](https://github.com/swaggo/swag)
17+
- [zap](https://github.com/uber-go/zap)
18+
- [lumberjack](https://github.com/natefinch/lumberjack)
19+
- [golang-jwt](https://github.com/golang-jwt/jwt)
20+
21+
It' s used to quickly build a web server with mainstream infrastructure to provide an environment for quickly developing API routes, middleware, and database operations, and to save the time of file hierarchy planning and log customization.
22+
23+
## License
24+
25+
[MIT](./LICENSE.md)

assets/goink.png

498 KB
Loading

cache/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cache
2+
3+
/*
4+
Discription: You can write down the control codes associated with the cache here.
5+
*/

config/config.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package config
2+
3+
/*
4+
Discription: You can add your own config here.
5+
*/
6+
7+
import (
8+
"errors"
9+
"os"
10+
"path/filepath"
11+
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
type serverConfig struct {
16+
Mode string `yaml:"mode"`
17+
Port string `yaml:"port"`
18+
Http2 bool `yaml:"http2"`
19+
}
20+
21+
type logConfig struct {
22+
Level string `yaml:"level"`
23+
Output struct {
24+
Stdout bool `yaml:"stdout"`
25+
File bool `yaml:"file"`
26+
} `yaml:"output"`
27+
FileName string `yaml:"fileName"`
28+
MaxSize int `yaml:"maxSize"`
29+
MaxAge int `yaml:"maxAge"`
30+
LocalTime bool `yaml:"localTime"`
31+
Compress bool `yaml:"compress"`
32+
}
33+
34+
type databaseConfig struct {
35+
Name string `yaml:"name"`
36+
Host string `yaml:"host"`
37+
Port int `yaml:"port"`
38+
User string `yaml:"user"`
39+
Password string `yaml:"password"`
40+
}
41+
42+
type middlewareConfig struct {
43+
Cors struct {
44+
Enabled bool `yaml:"enabled"`
45+
AllowOrigins []string `yaml:"allowOrigins"`
46+
AllowMethods string `yaml:"allowMethods"`
47+
AllowHeaders string `yaml:"allowHeaders"`
48+
ExposeHeaders string `yaml:"exposeHeaders"`
49+
MaxAge string `yaml:"maxAge"`
50+
AllowCredentials string `yaml:"allowCredentials"`
51+
} `yaml:"cors"`
52+
53+
Auth struct {
54+
JwtSecret string `yaml:"jwtSecret"`
55+
JwtSigningMethod string `yaml:"jwtSigningMethod"`
56+
JwtExpires int64 `yaml:"jwtExpires"`
57+
XAuthToken string `yaml:"xAuthToken"`
58+
} `yaml:"auth"`
59+
}
60+
61+
type gatewayConfig struct {
62+
Example struct {
63+
Url string `yaml:"url"`
64+
ApiKey string `yaml:"apiKey"`
65+
} `yaml:"example"`
66+
}
67+
68+
var rootPath string
69+
70+
var Server serverConfig
71+
var Log logConfig
72+
var Database databaseConfig
73+
var Middleware middlewareConfig
74+
var Gateway gatewayConfig
75+
76+
func init() {
77+
setProjectRoot()
78+
79+
loadConfig("server.yml", &Server)
80+
loadConfig("log.yml", &Log)
81+
loadConfig("database.yml", &Database)
82+
loadConfig("middleware.yml", &Middleware)
83+
loadConfig("gateway.yml", &Gateway)
84+
// add more config here
85+
86+
validateConfig()
87+
}
88+
89+
func loadConfig(fileName string, target interface{}) {
90+
file, err := os.ReadFile(rootPath + "/config/" + fileName)
91+
checkError(err)
92+
93+
err = yaml.Unmarshal(file, target)
94+
checkError(err)
95+
}
96+
97+
// setProjectRoot() will finds the root of this project and sets it to rootPath
98+
func setProjectRoot() {
99+
currentDir, err := os.Getwd()
100+
if err != nil {
101+
panic(err)
102+
}
103+
104+
for {
105+
gomod := filepath.Join(currentDir, "go.mod")
106+
if _, err := os.Stat(gomod); err == nil {
107+
rootPath = currentDir
108+
break
109+
}
110+
111+
nextDir := filepath.Dir(currentDir)
112+
if nextDir == currentDir {
113+
panic(errors.New("RootNotFoundError"))
114+
}
115+
116+
currentDir = nextDir
117+
}
118+
}
119+
120+
func validateConfig() {
121+
validateServerConfig()
122+
validateLogConfig()
123+
124+
// add more validation here
125+
}
126+
127+
func validateServerConfig() {
128+
if Server.Mode != "dev" && Server.Mode != "release" {
129+
panic(errors.New("InvalidModeError"))
130+
}
131+
132+
if Server.Port == "" || Server.Port[0] != ':' || len(Server.Port) < 2 {
133+
panic(errors.New("InvalidPortError"))
134+
}
135+
}
136+
137+
func validateLogConfig() {
138+
if Log.Level != "debug" &&
139+
Log.Level != "info" &&
140+
Log.Level != "warn" &&
141+
Log.Level != "error" &&
142+
Log.Level != "fatal" {
143+
panic(errors.New("InvalidLevelError"))
144+
}
145+
}
146+
147+
func checkError(err error) {
148+
if err != nil {
149+
panic(err)
150+
}
151+
}

config/database.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: "psql"
2+
host: "psql"
3+
port: 5432
4+
user: "root"
5+
password: "password"

config/gateway.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example: { url: "https://example.com", apiKey: "EXAMPLE_API_KEY" }

config/log.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
level: "info"
2+
output:
3+
stdout: true
4+
file: true
5+
fileName: "TEMP.log"
6+
maxSize: 10
7+
maxAge: 5
8+
localTime: true
9+
compress: true

0 commit comments

Comments
 (0)